Google chrome extension 如何让executeScript在安装扩展之前加载的选项卡上工作?

Google chrome extension 如何让executeScript在安装扩展之前加载的选项卡上工作?,google-chrome-extension,Google Chrome Extension,我的扩展通过单击pageAction按钮将JavaScript注入HTML文档 一旦安装了扩展,当用户导航到某个页面时,这种方法就可以正常工作 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.leng

我的扩展通过单击pageAction按钮将JavaScript注入HTML文档

一旦安装了扩展,当用户导航到某个页面时,这种方法就可以正常工作

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 });
      }
    }
  }
});
第一次安装扩展时,我可以显示pageAction按钮并侦听单击,但executeScript调用会以静默方式失败

似乎executeScript函数对安装扩展之前加载的选项卡没有权限

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 });
      }
    }
  }
});

有办法解决这个问题吗?

我的分机也有同样的问题。基本上,当扩展第一次安装时,用户不能使用它,除非他们重新加载选项卡。正如您所知,自动重新加载用户选项卡是一种非常糟糕的用户体验,因为他们可能正在做某件事,并且会失去它。我最终使用了
executeScript
,因为它对用户来说是不可见的。您可以在GitHub上看到源代码:

基本上,我使用了这个问题中的目标安装方法,并使用
chrome.windows.getAll
chrome.tabs.getAllInWindow
chrome.tabs.executeScript
在扩展首次安装时准备扩展

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 });
      }
    }
  }
});
chrome.windows.getAll({populate:true},函数(windows){ 对于(var w=0;w 如果executeScript不起作用,请在上提交一个bug,开发人员可以查看它。就我而言,它工作得很好


希望有帮助

我的分机也有同样的问题。基本上,当扩展第一次安装时,用户不能使用它,除非他们重新加载选项卡。正如您所知,自动重新加载用户选项卡是一种非常糟糕的用户体验,因为他们可能正在做某件事,并且会失去它。我最终使用了
executeScript
,因为它对用户来说是不可见的。您可以在GitHub上看到源代码:

基本上,我使用了这个问题中的目标安装方法,并使用
chrome.windows.getAll
chrome.tabs.getAllInWindow
chrome.tabs.executeScript
在扩展首次安装时准备扩展

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 });
      }
    }
  }
});
chrome.windows.getAll({populate:true},函数(windows){ 对于(var w=0;w 如果executeScript不起作用,请在上提交一个bug,开发人员可以查看它。就我而言,它工作得很好


希望有帮助

除了以编程方式刷新所有选项卡之外,可能不会。除了以编程方式刷新所有选项卡之外,可能不会。已更新代码以使其在当前版本上工作。现在刚刚再次尝试。更新了代码以使其在当前版本上工作。现在又试了一次。