Chrome的tab.create和tab.getSelected
我有一些问题需要将消息从后台页面传递到content_script.js。 我希望有人能指出我错在哪里。
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"]
}],
我的问题是来自background.html的请求从未传递给content_script.js。 我认为创建新选项卡并选择该选项卡的顺序必定存在一些问题,但我不知道如何解决此问题。 谢谢。
编辑:到目前为止我已经做了什么。
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({});
}
});
它工作正如我所料,但仍然有一些我不明白,特别是删除监听器chrome.tabs.onUpdated.removeListene(arguments.callee);。 我希望如果有人能够有机会看看并纠正我是否有任何错误。 谢谢。
background.html可以简化为:
//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);
});
}
如果它仍然无法正常工作,那么可能是因为该选项卡尚未完成加载(在chrome.tabs.create回调中检查tab.status以检查它是否为真)。 对此有两种解决方案,或者在为此标签ID过滤时向chrome.tabs.onUpdated添加侦听器,或者使选项卡发送请求而不是background.html。
链接地址: http://www.djcxy.com/p/91159.html上一篇: Chrome tab.create and tab.getSelected
下一篇: Chrome extension message passing, when xhr call fails in jQuery
