Chrome浏览器标签页网址重定向

Chrome浏览器标签页网址重定向

问题描述:

大家晚上好,

我正在启动chrome扩展程序,在某些情况下,我需要重定向(更改URL)用户的tab.

I am beginning a chrome extension and in a certain scenario I need to redirect (change URL) of a user's tab .

这是我的代码

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

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

}

现在这是正在发生的事情,Chrome://...正被添加到我的URL中! 假设我尝试将标签重定向到"http://www.google.com",就会发生这种情况:

Now here's what's happening , The Chrome:// ... thing is being prepended to my URL ! Say I try to redirect the tab to 'http://www.google.com' , this is what happens :

找不到以下网址的网页:chrome-extension://oihdngeahhchnacpilhnmaknneooabbc/http://www.google.com"

我不能动摇它!我尝试过先重置URL

I can't shake this ! I've tried resetting the URL first

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

我没有动摇这件事.

有什么想法吗?

由于您已经在使用chrome.tabs API,因此您可能想尝试使用chrome.tabs.query查找活动标签并以这种方式获取其ID .这是一个示例:

Since you are already using the chrome.tabs API, you may want to try using chrome.tabs.query to find the active tab and get it's id that way. Here's an example:

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.
     });
});