首页 文章

Chrome标签网址重定向

提问于
浏览
1

各位晚上好 ,

我正在开始镀铬扩展,在某种情况下我需要重定向(更改URL)用户的选项卡 .

这是我的代码

function changeTabURL(tabName,addr) {
var tabId=parseInt(localStorage.getItem(tabName)); //fetch tab ID

chrome.tabs.update(tabId,{"url":addr});

}

现在正是这里发生的事情,Chrome:// ......我的网址正在添加!假设我尝试将标签重定向到“http://www.google.com”,会发生以下情况:

"No webpage was found for the web address: chrome-extension://oihdngeahhchnacpilhnmaknneooabbc/http://www.google.com"

我不能动摇这个!我先尝试重置网址

chrome.tabs.get(tabId,function(tab) {
tab.url='';
alert(tab.url);
});
chrome.tabs.update(tabId,{"url":addr});
}

我没有做什么动摇这个 .

有什么想法吗?

2 回答

  • 2

    由于您已经在使用chrome.tabs API,因此您可能需要尝试使用chrome.tabs.query来查找活动选项卡并以此方式获取它的ID . 这是一个例子:

    queryInfo = new Object();
    queryInfo.active = true;
    chrome.tabs.query(queryInfo, function(result) {
         var activeTab = result[1].id;
         updateProperties = new Object();
         updateProperties.url = 'YOUR_URL_HERE';
         chrome.tabs.update(activeTab, updateProperties, function() {
              // Anything else you want to do after the tab has been updated.
         });
    });
    
  • 0

    你有没有在manifest.json中设置权限:

    "permissions": [
    "notifications",
    "contextMenus",
    "tabs",
    "contentSettings",
    "http://*/*",
    "https://*/*"
    

    ]

相关问题