Chrome浏览器标签页网址重定向 [英] Chrome Tabs URL Redirecting

查看:171
本文介绍了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.
     });
});

这篇关于Chrome浏览器标签页网址重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆