0

In Chrome it is possible to run content scripts after an extension is installed, that is, the code below works and the words 'content script' are shown in the console log of all open tabs after the installation.

In Edge, however, this does not work. It seems to be a bug in Edge. Any ideas how to work around so that content-script.js is executed after extension installation in all tabs?

background.js

browser.runtime.onInstalled.addListener(function(details) { 
    var scripts = browser.runtime.getManifest().content_scripts[0].js;
    browser.tabs.query({}, function(tabs) {
        tabs.forEach(function(tab){
            for(var k=0; k<scripts.length; k++) {
                browser.tabs.executeScript(tab.id, {
                    file: scripts[k]
                });
            }
        });
    });
});

content-script.js

console.log("content script");
cnmuc
  • 6,025
  • 2
  • 24
  • 29
  • If you use a persistent background script you can simply perform a check at the start by sending a test message to any matching tab (your content script would respond with anything like "ok" or `true`) so if there's no response, proceed with reinjection. For a non-persistent script, well, you can use any other event and perform the same check. – wOxxOm Mar 13 '20 at 17:08
  • It is not about checking if the content scripts are active, in Edge the (re)injection does not work at all. – cnmuc Mar 13 '20 at 17:13
  • FWIW executeScript works for me in the current Chromium-based Edge. Did you debug the code to see at which point it fails? Is it onInstalled itself, query(), or executeScript? – wOxxOm Mar 13 '20 at 17:27
  • executeScript in general works, but not in the context of onInstalled. executeScript seems to be called (query works, for-loop running, there are no error messages), but without effect. – cnmuc Mar 14 '20 at 09:03
  • I see. Try adding `runAt: 'document_start'` as maybe it thinks that tabs still load. – wOxxOm Mar 14 '20 at 09:04
  • Another idea: try delaying the process by like 1 second in setTimeout. – wOxxOm Mar 14 '20 at 09:06
  • Can you please inform us of which version of Edge you test this issue? Please check the extensions errors and let us know about it may help to get a proper idea about the issue. If possible you can host your sample code on GitHub. So that we can test it Chrome and Edge to see the difference. – Deepak-MSFT Mar 16 '20 at 07:31
  • @Deepak-MSFT Microsoft Edge 42.17134.1098.0, Microsoft EdgeHTML 17.17134 https://github.com/thunvogel/so-60674405 – cnmuc Mar 16 '20 at 08:34
  • @wOxxOm runAt and timeout did not work. reloading all tabs should work, but is not a practical option as hundreds of tabs may be open and the browser would slow down significantly, or even crash – cnmuc Mar 16 '20 at 08:42
  • FWIW You can reload the tabs sequentially... – wOxxOm Mar 16 '20 at 08:46
  • @wOxxOm just tried your reload idea. this won't work as the content script gets only active in new tabs (after the extension is installed) – cnmuc Mar 16 '20 at 10:49
  • So the bug is even worse? Tabs that had the old content script become kinda dead to the extension. – wOxxOm Mar 16 '20 at 10:50
  • @cnmuc, I can see that you are testing the extension with the MS Edge legacy version. Microsoft is no longer accepting the new extensions for the MS Edge legacy version. I suggest you develop an extension for the new MS Edge Chromium browser. You can encourage your users to move to the new Edge Chromium browser. As the new MS Edge uses the Chromium browser engine, it may also help to fix this issue. – Deepak-MSFT Mar 17 '20 at 05:52
  • @Deepak-MSFT you are right, with the latest edge version everthing works fine. i'll stop trying to support the lecacy version. thanks! – cnmuc Mar 17 '20 at 08:17
  • I suggest you mark the helpful suggestion as an answer to this question. Thanks for your understanding. – Deepak-MSFT Mar 17 '20 at 08:58

1 Answers1

0

I can see that you are testing the extension with the MS Edge legacy version.

Microsoft is no longer accepting the new extensions for the MS Edge legacy version.

I suggest you develop an extension for the new MS Edge Chromium browser. You can encourage your users to move to the new Edge Chromium browser. As the new MS Edge uses the Chromium browser engine, it may also help to fix this issue.

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19