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

编程入门 行业动态 更新时间:2024-10-25 14:35:54
本文介绍了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中! 假设我尝试将标签重定向到"www.google",就会发生这种情况:

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

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

我不能动摇它!我尝试过先重置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浏览器标签页网址重定向

本文发布于:2023-10-31 19:12:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   浏览器   标签   网址   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!