簡體   English   中英

如何在Chrome擴展程序中更改活動的標簽網址

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

我發現如何開發Chrome擴展程序,卻找不到實際如何更改活動網址。 我想從網站上獲取信息並打包。

實際上,此代碼的第一行有效。 我嘗試了不同的方法來更改網址,找不到正確的網址...

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

我的清單是:

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

有人能幫我嗎?

讓我們在docs中查看tabs.update方法的簽名:

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

整數(可選) tabId
默認為當前窗口的選定選項卡。

對象updateProperties
[...]

由於您需要當前(選定)標簽,因此可以省略 tabId-將其標記為可選。

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

從內容腳本來看,情況有所不同。 無權訪問chrome.tabs API

但是您只需更改document的屬性即可,因為您已經處於活動頁面的上下文中:

document.location = myNewUrl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM