Chrome扩展-使用tabId更新某些选项卡

Chrome extension - update certain tab using tabId

本文关键字:选项 更新 tabId 扩展 使用 Chrome      更新时间:2023-09-26

我有一个chrome扩展,当用户按下图标时,它会将当前tab.id存储在变量中,然后在2分钟后运行chrome.tabs.executeScript函数。据我所知,我的代码应该可以工作,但我得到了一个错误:

Uncaught Error: Invalid value for argument 1. Property 'tabId': Unexpected property

来自铬显影剂网站

更新

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

修改选项卡的属性。

这是我到目前为止的代码:

   //Up here is the logic for handling the icon press
   chrome.tabs.query({ active: true, currentWindow: true }, function(arrayOfTabs) {
        var activeTab = arrayOfTabs[0].id; //this is the active tab id.
            setInterval(function() { //Interval for every 2 minutes
                chrome.tabs.update({
                    tabId: activeTab, //this is where I get an error... What's wrong with this?
                    active: true,
                    url: "https://mywebsite.com/" + myarray[count]
                }, function(tab) {
                    chrome.tabs.executeScript(tab.id, {
                        file: "function/jquery.min.js",
                        runAt: "document_end"
                    });
                    chrome.tabs.executeScript(tab.id, {
                        file: "function/code.js",
                        runAt: "document_end"
                    });
                })
                count++;
            }, timer);
    });

关于我的代码出了什么问题,有什么想法吗?我试过tabId: activeTabactiveTab,但总是出错。如何向tabs.update指定要更新的选项卡?非常感谢。

您应该使用

chrome.tabs.update(activeTab, {
  active: true,
  url: "https://mywebsite.com/" + myarray[count]
}, function(tab){});`

CCD_ 8和CCD_。