Chrome的tab.create和tab.getSelected [英] Chrome tab.create and tab.getSelected

查看:220
本文介绍了Chrome的tab.create和tab.getSelected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题需要将消息从后台页面传递到content_script.js。我希望有人能指出我错在哪里。

background.html

 //在函数
函数中myFunction(){
chrome.tabs.create({url:myurl,selected:true},function标签){
updateTab(tab.id);
});
}

//更新创建新选项卡
函数updateTab(tabId){
chrome.tabs.getSelected(null,function(tab){
makeRequest(tab.id);
});}
//发出请求
函数makeRequest(tabId){
chrome.tabs.sendRequest(tabId,{greeting:hello },function(response){
console.log(response.farewell);
});
}

content_script.js

 
chrome.extension.onRequest.addListener(
函数(request,sender,sendResponse){
console.log(sender.tab?
from a content脚本:+ sender.tab.url:
from the extension);
if(request.greeting ==hello)
sendResponse({farewell:goodbye}) ;
else
sendResponse({}); //删除它们
});

manifest.json

 
permissions:[
tabs,notifications,http:// * / *
],
content_scripts:[
{
matches:[http:// * / *,https:// * / *],
js:[content_script.js]
} ],

我的问题是background.html的请求从未传递给content_script.js。我认为创建新选项卡并选择该选项卡的顺序必定存在一些问题,但我不知道如何解决此问题。
Thanks。



编辑:
到目前为止我已经完成了。



background.html

 
chrome.browserAction.onClicked.addListener(function(tab){
var tabId = tab.id;
chrome.tabs.getSelected(null,function(tab){
validate(tab.url,tabId);
});
}) ;
函数validate(url,tabId){
var filter = support(url);
if(filter!= null){
getHTML(tabId,url,filter);
}
else {
var notification = webkitNotifications.createHTMLNotification(
'notification.html'// html url - can be relative
);
notification.show();
setTimeout(function(){
notification.cancel();
},10000); // 10秒
}
}
函数getHTML(tabId,url,filter){
console.log(request);
chrome.tabs.sendRequest(tabId,{action:getHTML},function(response){
var html = response.html;
console.log(html);
var taburl =(服务器上的一些东西);
chrome.tabs.create({url:taburl,selected:true},function(tab){
var tabId = tab .id;
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo){
if(changeInfo.status ==loading){
console.log(loading) ;
}
if(changeInfo.status ==complete){
chrome.tabs.onUpdated。removeListene(arguments.callee);
updateTab(tabId,url,filter ,html);

}
});
});
});
}
函数updateTab(tabId,url,filter,html){
chrome.tabs.sendRequest(tabId,{action:updateTab},function(response){
//提交表单
chrome.tabs.executeScript(tabId,{code:'document.getElementById(\hiddenbutton \)。click();'});
});
}

content_script.js

 
chrome.extension.onRequest.addListener(
function(request,sender,sendResponse){
var action = request.action;
console.log(action );
if(action ==getHTML){
var html = document.body.innerHTML;
console.log(html);
sendResponse({html:document .body.innerHTML});
}
else {
//在服务器上更新页面
sendResponse({});
}
} );

它可以像我预期的那样工作,但仍有一些我不明白的地方,特别是删除监听器 chrome.tabs。 onUpdated.removeListene(arguments.callee的); 的。我希望如果有人能够有机会看看并纠正我是否有任何错误。谢谢。

解决方案

background.html可以简化为:

<$在函数
函数myFunction(){
chrome.tabs.create({url:myurl,selected:true}中,p $ p> 函数(标签){
makeRequest(tab.id);
});

$ b $ //请求
函数makeRequest(tabId){
chrome.tabs.sendRequest(tabId,{greeting:hello},function(response ){
console.log(response.farewell);
});





$ b

如果它仍然无法正常工作,那么可能是因为制表符hasn未完成加载(日志 tab.status 。 tabs.create回调来检查这是否为真)。有两种解决方案,或者向 chrome.tabs添加侦听器。 onUpdated ,同时过滤此标签ID,或者您标签发送请求而不是background.html。


I have some problems to pass message from background page to my content_script.js. I hope someone can point out where i am wrong.

background.html

//in a function
function myFunction() {
    chrome.tabs.create({"url":"myurl","selected":true},function(tab){
    updateTab(tab.id);
    });
}

//update Created new tab
function updateTab(tabId){
    chrome.tabs.getSelected(null, function(tab) {
        makeRequest(tab.id);  
    });}
//make request
function makeRequest(tabId) {
    chrome.tabs.sendRequest(tabId, {greeting: "hello"}, function(response) {
        console.log(response.farewell); 
    });
}

content_script.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });

manifest.json

"permissions": [
        "tabs","notifications","http://*/*"
    ],
    "content_scripts": [
    {
        "matches": ["http://*/*","https://*/*"],
        "js": ["content_script.js"]
    }],

My problem is the request from background.html has never been passed to the content_script.js. I think there must be some problems about the sequence of creating new tab and selecting that tab, but i do not know how to fix this. Thanks.

EDIT: There is what i have done so far.

background.html

chrome.browserAction.onClicked.addListener(function(tab) {
        var tabId = tab.id;
        chrome.tabs.getSelected(null, function(tab) {   
           validate(tab.url,tabId);
        });
    });
    function validate(url,tabId) {
        var filter = support(url);
        if(filter!=null) {
            getHTML(tabId,url,filter);
        }
        else{
            var notification = webkitNotifications.createHTMLNotification(
              'notification.html'  // html url - can be relative
            );
            notification.show();
            setTimeout(function(){
                notification.cancel();
            }, 10000);  //10 sec
        }
    }
function getHTML(tabId,url,filter) {
        console.log("request"); 
            chrome.tabs.sendRequest(tabId, {action:"getHTML"}, function(response) {     
            var html = response.html;
            console.log(html);
            var taburl = ("some thing on server");  
            chrome.tabs.create({"url":taburl,"selected":true}, function(tab){
                var tabId = tab.id;
                chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){
                    if(changeInfo.status == "loading"){
                            console.log("loading");
                  }
                  if(changeInfo.status == "complete"){
                          chrome.tabs.onUpdated.    removeListene(arguments.callee);                    
                        updateTab(tabId,url,filter,html);

                  }
                });
              }); 
            });
    }
function updateTab(tabId,url,filter,html) {
        chrome.tabs.sendRequest(tabId, {action:"updateTab"}, function(response) {
                //submit form
            chrome.tabs.executeScript(tabId, {code: 'document.getElementById(\"hiddenbutton\").click();'});
        });
    }

content_script.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    var action = request.action;
    console.log(action);
    if(action == "getHTML") {
        var html = document.body.innerHTML;
        console.log(html);
        sendResponse({html:document.body.innerHTML});
    }
    else{
    //do update on page from server
    sendResponse({});
    }
});

It works as i expected, but there are still some points that i do not understand, espically removing listener chrome.tabs.onUpdated.removeListene(arguments.callee);. I hope if someone can have a chance to have a look and correct me if any thing is wrong. Thanks.

解决方案

The background.html can be simplified to:

//in a function
function myFunction() {
    chrome.tabs.create({"url":"myurl","selected":true}, function(tab){
        makeRequest(tab.id);
    });
}

//make request
function makeRequest(tabId) {
    chrome.tabs.sendRequest(tabId, {greeting: "hello"}, function(response) {
        console.log(response.farewell); 
    });
}

If it still doesn't work correctly then it might be because the tab hasn't finished loading (log tab.status in the chrome.tabs.create callback to check if this is true). There are two solutions for this, or you add an listener to chrome.tabs.onUpdated while filtering for this tab id or you make the tab send the request instead of background.html.

这篇关于Chrome的tab.create和tab.getSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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