1

My extension injects JavaScript into an HTML document by clicking the pageAction button.

This works fine when the user navigates to a page once the extension is installed.

When the extension is first installed I am able to display the pageAction button and listen for clicks, but the executeScript call fails silently.

It appears as though the executeScript function doesn't have privileges on a tab that was loaded before the extension was installed.

Is there a way around this problem?

Tim
  • 8,036
  • 2
  • 36
  • 52

1 Answers1

1

I had the exact same problem with my Reload All Tabs extension. Basically, when the extension first gets installed, the user can't use it unless they reload the tabs. As you know, it is really bad user experience automatically reloading users tabs because they might be in a middle of doing something and they will loose it. I ended up using executeScript because it is really invisible to the user. You can see the source code located at GitHub:

https://github.com/mohamedmansour/reload-all-tabs-extension/blob/master/js/reload_controller.js#L96

Basically I used the approach to target installations from this question, Detect Chrome extension first run / update and used the chrome.windows.getAll, chrome.tabs.getAllInWindow, and chrome.tabs.executeScript to prepare the extension when it has first installed.

chrome.windows.getAll({ populate: true }, function(windows) {
  for (var w = 0; w < windows.length; w++) {
    var tabs = windows[w].tabs;
    for (var t = 0; t < tabs.length; t++) {
      var tab = tabs[t];
      // Only inject in web pages.
      if (tab.url.indexOf('http') == 0) {
        chrome.tabs.executeScript(tab.id, { file: 'content_script.js', allFrames: true });
      }
    }
  }
});

If executeScript doesn't work, please file a bug at http://crbug.com/new and the developers can look at it. In my case, it works just fine.

Hope that helps!

Community
  • 1
  • 1
Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90