gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:43:29 25 4
gpt4 key购买 nike

我正在研究如何开发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com