Most grammar checkers work by reading a text box. Google Docs doesn't have one. It draws every letter on a canvas, so there is no text in the page for an extension to read, no word to underline and nothing to replace. This is how we got Rewordly to underline mistakes in Google Docs and apply fixes with one click, and what we are not sure will last.
The problem: a document with no text in the page
In a normal text field, an extension reads the value, finds where a word sits on screen and draws an underline there. Fixing a mistake means changing the value through the field's own input path, so the page and undo stay happy.
Google Docs breaks every one of those steps:
- Reading: the document text is not in the page's DOM. It is painted onto a canvas.
- Placing: there are no word elements to measure, only pixels.
- Fixing: there is no field to write to. Typing happens in a hidden element that Docs watches for keyboard and clipboard events.
Step 1: getting the text
Docs has a page function meant for tools that need to read a document, _docs_annotate_getAnnotatedText. Called with an identifier, it returns an object that can give you the document text, the current selection and move the selection.
Two details matter:
- The identifier has to be announced before the page loads, by setting
window._docs_annotate_canvas_by_ext. Setting it early also makes Docs add an annotation layer to the canvas. - The function lives in the page's own JavaScript world. A normal extension content script runs in an isolated world and can't see it. Rewordly adds a small script that runs in the page's main world, calls the function, and talks to the rest of the extension through
postMessage.
In our tests the text of a 120-paragraph, two-page document (about 15,000 characters) came back in one call.
Step 2: finding where each word is
With the annotation layer on, Docs adds an invisible svg rect for each line it draws, labelled with that line's text. The rectangle has a position on screen. That gives us the lines, but not the words.
For the words we do some arithmetic:
- Find the line's rectangle and its text.
- Measure the text before the word with the canvas
measureTextfunction, using the same font Docs uses (14.6667px Arialin a default document). - The width of that prefix, as a share of the whole line's width, tells us where the word starts. Do the same for the end of the word.
The rectangle's width matches measureText for the whole line, so the shares line up with what is on screen. The underline is then drawn on a separate layer above the page, where the word is.
Docs only keeps rectangles for the lines near the visible area (about 50 lines in our test) and recycles them as you scroll. Each line also has to be matched to its position in the full document text. We match the visible lines one after another against the text, and when a line's text appears more than once, we take the occurrence the following lines agree with.
Step 3: applying a fix without breaking undo
Replacing text is the part we were most careful with. The approach:
- Check that the document hasn't changed since the check ran. If the text at that position is no longer the same, we stop instead of overwriting something else.
- Select the exact range of the mistake. Docs applies the selection a moment after it is requested, so we wait until it reports the range we asked for.
- Send a paste event with the corrected text to Docs' hidden text input, the same path a real paste takes.
Because Docs sees an ordinary paste, only the selected words change, formatting around them is kept and Ctrl+Z undoes the fix in one step.
What it reads, and what it doesn't
The script reads the document text only so it can be checked, in the same way Rewordly checks text in any other field. Sentences are sent to our servers for checking, and only sentences that changed are sent again. Passwords, payment fields and code editors are skipped, and you can turn live checking off in the extension popup or on any site. The details are in the privacy policy.
What we're not sure about
This approach relies on a function and an annotation layer that Google exposes for extensions but does not document in detail. Google can change or restrict them. We have also not tested every kind of document: tables, suggestion mode and live collaboration can behave differently, and documents that mix fonts and sizes need the font to be measured per line.
If something looks wrong in your document, tell us at the address on our site and we'll look at it.
Try it
Install the extension, open a Google Doc and start writing. Mistakes are underlined as you type and you fix them in one click. It is free and needs no account. For a broader look at where it works, see grammar checker that works as you type.
Try Rewordly
Write in your languages, and let Rewordly catch the mistakes. Free to start, no sign-up required.