简体   繁体   English

如何在Chrome扩展程序中更改活动的标签网址

[英]How to change active tab url in chrome extension

I'm discovering how to develop a Chrome extension and I can't find actually how to change the active url. 我发现如何开发Chrome扩展程序,却找不到实际如何更改活动网址。 I would like to get informations from websites and pack them. 我想从网站上获取信息并打包。

Actually the first lines of this code work. 实际上,此代码的第一行有效。 I've tried different ways to change the url, can't find the right one... 我尝试了不同的方法来更改网址,找不到正确的网址...

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

My manifest is : 我的清单是:

{
  "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'"
}

Can someone help me? 有人能帮我吗?

Let's see the signature of the tabs.update method in the docs : 让我们在docs中查看tabs.update方法的签名:

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

integer (optional) tabId 整数(可选) tabId
Defaults to the selected tab of the current window. 默认为当前窗口的选定选项卡。

object updateProperties 对象updateProperties
[...] [...]

Since you want the current (selected) tab, you can just omit the tabId - it's marked as optional. 由于您需要当前(选定)标签,因此可以省略 tabId-将其标记为可选。

chrome.tabs.update({url: myNewUrl});

From a content script, the situation is different. 从内容脚本来看,情况有所不同。 You don't have access to chrome.tabs API . 无权访问chrome.tabs API

But you can simply change the document 's properties, as you're in the context of the active page already: 但是您只需更改document的属性即可,因为您已经处于活动页面的上下文中:

document.location = myNewUrl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM