javascript - 如何更改 Chrome 扩展中的事件标签网址

标签 javascript google-chrome-extension

我正在研究如何开发 Chrome 扩展程序,但实际上我找不到如何更改事件 url 的方法。我想从网站上获取信息并打包。

实际上,此代码的第一行有效。我尝试了不同的方法来更改网址,但找不到正确的方法...

$("body").css('background-color', 'pink');
$("body").html('COUCOU');
setTimeout(function(){ 
      var myNewUrl = "http://www.google.com";
      chrome.tabs.update(active, {url: myNewUrl});
}, 2000);;

我的 list 是:
{
  "name": "A browser action with a popup that changes the page color",
  "description": "Change the current page color",
  "version": "1.0",
  "permissions": [
    "activeTab", "tabs"
  ],
  "browser_action": {
      "default_title": "Set this page's color.",
      "default_icon": "icon.png",
      "default_popup": "popup.html"
  },
   "content_scripts": [ {
    "js": [ "jquery.js", "background.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }],
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}

有人能帮我吗?

最佳答案

让我们看看 tabs.update 的签名方法 in the docs :

chrome.tabs.update(integer tabId, object updateProperties, function callback)

integer (optional) tabId
Defaults to the selected tab of the current window.

object updateProperties
[...]


由于您需要当前(选定的)选项卡,您可以省略 tabId - 它被标记为可选。
chrome.tabs.update({url: myNewUrl});

从内容脚本来看,情况有所不同。您don't have access to chrome.tabs API .
但是您可以简单地更改 document的属性,因为您已经在事件页面的上下文中:
document.location = myNewUrl;

关于javascript - 如何更改 Chrome 扩展中的事件标签网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303551/

相关文章:

javascript - 如何使用 csv 数据在 Chart.js 中制作不同颜色的条形图?

javascript - 继承 - 实例变量

google-chrome-extension - Chrome devtools 扩展控制台

javascript - 将图像文件添加到现有的 javascript

javascript - 使用 filter() 而不是 map() 来修改对象数组?

javascript - 无法在react-bootstrap中调用Carousel函数

javascript - Chrome 扩展程序 : Is there a means of running JavaScript to procure inner html on a page and saving it to a variable in the extension?

jquery - Chrome 显示错误为 : Refused to execute inline script because of Content-Security-Policy

google-chrome - 每次单击 Chrome 扩展程序图标时运行脚本

javascript - Chrome 扩展中的弹出脚本多次发送消息