我有一个chrome扩展程序,其工作方式如下:
后台每隔几秒检查一次站点x是否有变化,如果出现这些变化,则后台打开带有页面Y的新选项卡,并进行一些页面自动化(填写基于ajax的页面中的调查)。页面自动化由内容脚本完成。内容脚本需要向Background询问是否允许开始工作。它通过发送消息并等待回复来实现。当内容脚本完成它的工作时,它会向Background发送消息,其中包含有关成功与否的信息。当应该使用新链接更新失败选项卡时,应该关闭成功 - 选项卡。问题是,有时 - 并非总是如此,bacground获取两次消息。一次 - 当tab失败时它的工作,第二次就在使用新链接更新选项卡之后。我希望我能知道为什么......
这是脚本:
Background.js侦听器创建
function create_listerner(){
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.greeting){
case "site_opened":
if (sender.tab.id == tabId) {
sendResponse({
farewell : "go"
});
} else {
sendResponse({
farewell : "stop"
});
}
break;
case "imDoneWithSuccess":
//update tab logic
break;
case "imDoneWithFail":
//close tab logic
actNgo(url,arr);
break;
}
});
}
当用户从上下文菜单启动脚本时,会进行一次监听器调用:background.js
chrome.contextMenus.create({
title : "Start",
contexts : ["browser_action"],
onclick : function () {
create_listerner();
}
});
标签更新看起来很简单:background.js
function actNgo(url,arr){
chrome.storage.local.set({
array : arr,
ndz : 1
}, function () {
chrome.tabs.update(
tabId,
{
"url" : url,
"active" : false
}, function (tab) {
console.log("tabId "+tabId);
});
});
}
内容脚本被注入网站,网站不会像基于ajax那样重新加载,即使 - 如果没有适当的条件也无法发送消息
content.js
function SR(st) {
var status ;
if(st){
status = "imDoneWithSuccess";
} else {
status = "imDoneWithFail";
}
chrome.runtime.sendMessage({
greeting : status
}, function (response) {
});
}
内容脚本应仅适用于bacground.js打开的标签
function askForPermission() {
chrome.runtime.sendMessage({
greeting : "site_opened"
}, function (response) {
if (response.farewell == 'go') {
start();
} else {
console.log("bad tab");
}
});
}
再次 - 他们不可能自己开火。
日志文件如下所示:
Background: tab created;
content script: askForPermission(); <-sends message asking for permission to start
Bacground: go <- allows.
content script: (content script logs, working as intended)
Background listener: imDoneWithFail;
Background update tab;
background listener: imDoneWithFail; <- shouldn't happen, doesn't look like it conten't script even started to work as it didn't ask for permission. Looks like listener is fired up twice...
Background update tab;
content script: askForPermission(); <-sends message asking for permission to start
Bacground: go <- allows.
编辑:清单
{
"name": "Test script",
"version": "0.0.78",
"manifest_version": 2,
"background": {
"scripts": ["src/background/background.js"]
},
"icons": {
"19": "icons/icon19.png"
},
"default_locale": "en",
"options_ui": {
"page": "src/options/index.html",
"chrome_style": true
},
"browser_action": {
"default_icon": "icons/icon19.png",
"default_title": "Test",
"default_popup": "src/browser_action/browser_action.html"
},
"permissions": [
"http://localhost/survey/*",
"storage",
"contextMenus",
"tabs",
"webRequest"
],
"content_scripts": [
{
"matches": [
"http://localhost/survey/*",
"https://localhost/survey/*"
],
"js": [
"js/jquery/jquery.min.js",
"src/inject/content.js"
]
}
]
}
在有人问之前 - 我试图使用长期连接,但我有同样的问题。为什么会这样?
答案 0 :(得分:1)
好的我找到了答案,问题在于功能更改标签的网址。
function actNgo(url,arr){
chrome.storage.local.set({
array : arr,
ndz : 1
}, function () {
chrome.tabs.update(
tabId,
{
"url" : url,
"active" : false
}, function (tab) {
console.log("tabId "+tabId);
});
});
}
函数,当给出与当前没有刷新标签相同的url时,内容脚本无法启动为新...仍然不知道为什么监听器两次启动消息,但是因为我的更改已停止。
我不知道这个修复程序是否会对任何人有所帮助,但我通过先将标签网址更改为空白,然后更改为新标记来修复此问题。
chrome.tabs.update(
tabId,
{
"url" : "about:blank",
"active" : false
}, function (tab) {
console.log("bcgr: aktualizujStroneIObstaw: pusta: tabId "+tabId);
chrome.tabs.update(
tabId,
{
"url" : url_beta,
"active" : false
}, function (tab) {
console.log("bcgr: aktualizujStroneIObstaw: wlasciwa: tabId "+tabId);
}
);