Skip to content

fix: Google Docs bridge and replaceText issue for the popup suggestion - #3847

Open
Yuvi-raj-P wants to merge 3 commits into
Automattic:masterfrom
Yuvi-raj-P:fix-replace-suggestion
Open

fix: Google Docs bridge and replaceText issue for the popup suggestion#3847
Yuvi-raj-P wants to merge 3 commits into
Automattic:masterfrom
Yuvi-raj-P:fix-replace-suggestion

Conversation

@Yuvi-raj-P

Copy link
Copy Markdown

Issues

Hopefully should close #2995

Description

Problem: When you click a linted word on google docs, the suggestions and the grammar check works however when you go to the popup to replace the error with harper suggestion the error does not get replaced.

Solution:

  1. From @abhishekDeshmukh74, initially thought race conditions are what causing the issue. So I went towards the direction of race condition and though applySuggestion is called then immediately followed by close() with no waiting is the problem. I developed a fix which fell short and of course did not work. Later I found that popup closes before the replacing even happens and though that was the race condition because if popover has already closed focus will be moved off the docs iframe which resulted in for activeElement therefore insertText and synthetic paste event did nothing (cherry on top, all errors were silently dying as nothing was being reported). Was still thinking about race conditions so continued to develop a promise chain to await all the way until replacement was done. But it was not the solution. However, the promise chain was left as it's good to have and minimizes any race happening making the process more consistent.

  2. With race conditions not the main issue, I later went towards the null issue with the active element. With a decent amount of debugging and Claude, I came to the conclusion that the active element was not the right element we needed for the replacement. This was due to the clicking function of the suggestion button in the popup using the native browser’s button click method. Chrome's default mousedown behavior focuses the clicked button before the onclick handler runs which pulls the page focus off the iframe to popup button (the suggestion). By the time replaceTextRequest reads the activeElement it could not find the right element to dispatch the replacement to as it fell back to the replacement silently died. The execCommand and the paste dispatch however reported success with no errors which led to this whole rabbit hole of a problem from a simple button click.

td;lr:

  1. Used mousedown.preventDefault() to prevent the default focus on click behavior so the focus stays on Docs iframe instead of the button.
  2. Just to be safe, some hardening with suggestion apply logic so it waits out and makes sure each step has its time before any one of it suddenly stops it in between through a promise chain throughout the whole process.

Please let me know if issues presist, it did work on my instance (Chrome/Apple silicon), i am just too lazy to record a demo and convert it and all that. First pr on the repo :) and please let me know of any silly comments on repo files those are for me.

How Has This Been Tested?

Build test. Built the Chrome Extension onto chrome and tested it against some text in google docs.
Since the test was purely done with chrome, I am not sure if this works with, for example, Firefox please someone let me know.

AI Disclosure

  • I am a human and didn't use any AI.
  • [] I used LLM features of my editor, but not an agent.
  • I used an AI agent interactively.
  • I am an agent or I got an agent to do the work autonomously.

Checklist

  • [ x] I have performed a self-review of my own code
  • I have added tests to cover my changes
  • I have considered splitting this into smaller pull requests.

@hippietrail hippietrail added the google-docs Problems for the Chrome extension and/or Firefox plugin specifically in Google Docs. label Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

google-docs Problems for the Chrome extension and/or Firefox plugin specifically in Google Docs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot Click to Apply Suggestions in Google Docs

2 participants