如何在Chrome扩展中重新加载特定标签页

5
我正在尝试从弹出窗口(特别是popup.js)重新加载用户可能在其中一个标签页中打开的页面。我看了这个答案,但它只适用于当前选项卡 https://dev59.com/Qmsy5IYBdhLWcg3wvgr7#25246060
此外,当它运行时,我会收到以下错误:
popup.html:1 Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "thewebpageiamtryingtoaccess.com".
Extension manifest must request permission to access this host at Object.callback (chrome-extension://mjckbfpnokoplldjpijdfhffbbbfflah/popup.js:3:17)

我已经添加了

"permissions": [
    "tabs"
  ],

在我的清单中,仍然没有运气。我如何能够为用户打开的特定选项卡执行此操作。我希望在第一次加载扩展程序时重新加载网站,以便用户无需手动重新加载网页即可使内容脚本生效。谢谢!

1个回答

11

要修复执行错误,您需要更改清单文件中的权限为:

  "permissions": [
     "tabs",
     "http://*/",
     "https://*/"
  ]
您可以通过此类URL查询标签页的ID:

chrome.tabs.query({url: "http://thewebpageiamtryingtoaccess.com/*"}, function(tab) {
   // reload tab with one of the methods from linked answer
   chrome.tabs.reload(tab[0].id) 
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接