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。删除Listene(arguments.callee);。我希望有人能有机会看看并纠正我,如果有什么问题。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
background.html 可以简化为:
如果仍然无法正常工作,则可能是因为选项卡尚未完成加载(日志 tab.status 在 chrome.tabs.create 回调中检查这是否为真)。有两种解决方案,或者您向 chrome.tabs 添加侦听器。 onUpdated 过滤此标签 ID 或您使标签发送请求而不是background.html。
The background.html can be simplified to:
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.