AbhishekTimalsina / Type-Nepali

Chrome extension to type in Nepali
https://chromewebstore.google.com/detail/type-nepali/ljbhdpgfoeebgbchbkminmgecialnnen?pli=1
17 stars 4 forks source link

Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist. #8

Open paudsu01 opened 7 months ago

paudsu01 commented 7 months ago

https://github.com/AbhishekTimalsina/Type-Nepali/blob/e0443ebda6d1daf9cb6856e1bca06b3a6757497f/scripts/popup.js#L26-L34

The line await chrome.tabs.sendMessage(tab.id, { translate: boolean, raises the Could not establish connection. Receiving end does not exist. error on some circumstances. This is most-likely because of the fact that chrome doesn't automatically re-inject the content scripts in the currently open tabs after the extension is updated/installed.

image
paudsu01 commented 7 months ago

I am not sure how one could solve this, but the code would probably need to get all tabs like you do there but inject code again into them using chrome.tabs.executeScript.

paudsu01 commented 7 months ago

Fixed in #7 : Just like I had suggested earlier, the tabs where the content-script doesn't get loaded as it happens with the already open tabs when the extension gets loaded/installed needs the injection of content scripts.

So, chrome.scripting.executeScript is used to execute the content-script (got changed from chrome.tabs.executeScript since manifest v3) if the tab doesn't have the content-script loaded already.

try {
                await chrome.tabs.sendMessage(tab.id, {
                translate: boolean,
                });}
 catch(error) {
               chrome.scripting.executeScript({
                target: {tabId: tab.id, allFrames: true},
                // location relative to root folder(where manifest.json is)
               files: ['scripts/content.js'],
                        });
                        }