在Chrome扩展程序中获取标签ID时出现问题 [英] Problem with getting the tab ID in Chrome Extension

查看:432
本文介绍了在Chrome扩展程序中获取标签ID时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,要获取标签的标签ID并在其他标签中使用它。

I've a problem getting the tab ID of a tab and using it in other tab.

有一个标签打开(google.com)使用我的内容脚本在google.com上放置了一个按钮。
按钮应该创建一个标签,当点击URL为cricinfo.com时。
contentscript.js

There is one tab open (google.com) i've placed a button on google.com using my content script. The Button should create a tab when clicked with url as "cricinfo.com" . contentscript.js

$(body).prepend('(Open up)

$(body).prepend('(Open up)

</button><textarea id="followup_text"></textarea>');


chrome.extension.sendRequest({"acturl":'http://cricinfo.com',"type":""});   

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
    if(request.greeting=="hello")
{
 alert(sender.tab.url); 
sendresponse({farwell:"thanks"});
} 
else
sendresponse({farwell:"not recieved"});
});

}); 

Background.html

<script type="text/javascript" charset="utf-8">


       chrome.extension.onRequest.addListener(
        function(request, sender, sendResponse) {  

       chrome.tabs.create({"url":request.acturl,"selected":false},function(tab){

       });  

    });   
    chrome.tabs.getSelected(null, function(tab){ 
        chrome.extension.sendRequest(tab.id, {greeting:"hello"},function(response){console.log(response.farwell);}); 
    }
    })

</script>   

现在cricinfo.com重定向到espncricinfo.com,所以我想要这个网址显示在我的原始标签(即在google.com中)并将其显示在textarea#follow_text中。

Now cricinfo.com redirects to "espncricinfo.com" , So i want this url to be displayed in my original tab(i.e. in google.com) and get it displayed in the textarea#follow_text.

为了执行此操作,我希望google.com的tabID用于在espncricinfo.com上发送来自background.html的请求。扩展程序不允许在内容中使用选项卡。我无法在background.html上使用它。

To perform this i want the tabID of google.com for sending request from background.html when on espncricinfo.com. The extensions doesn't allow to use tabs in contentscripts. I'm not able to use it on background.html.

谢谢。让我知道我是否不清楚。

Thanks. Lemme know if i'm not clear.

推荐答案

好的,这是一些代码,但它有点不稳定。诀窍是,当您收听 chrome.tabs.onUpdated 时,它已经收到重定向的url(如果没有重定向,它会收到直接的url)。

Well, here is some code, but it is kind of a shaky solution. The trick is that when you listen to chrome.tabs.onUpdated it receives redirected url already (and if there was no redirect it receives direct url).

var createdTabId = 0;
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(tabId == createdTabId && changeInfo.status == "loading") {
        createdTabId = 0;

        //tab.url contains redirected or direct url, send it to google tab
        var tabUrl = tab.url;
        chrome.tabs.getSelected(null, function(tab){ 
            chrome.tabs.sendRequest(tab.id, {tabUrl: tabUrl}); 
        });

    }
});

chrome.tabs.create({"url":"http://cricinfo.com","selected":false},function(tab){
    createdTabId = tab.id;
});  

这篇关于在Chrome扩展程序中获取标签ID时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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