背景:
小弟开发了一个在content Script 和 Background Page 之间不断互相嵌套调用的Chrome Extension
在进行了2次嵌套调用后出错。 而我根本无法知道为何里面的代码不执行。
出错的地方:
background.js 的第九行;
前辈们帮忙啊!!!
所有代码如下(option page是空的, 我就没放上来):
<manifest.js>
{
"manifest_version": 2,
"name": "Getting started example",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"options_page": "options.html",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage",
"tabs",
"*://*/*",
"background"
],
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["content.js"],
"run_at": "document_start"
}
],
"background": {
"scripts": ["background.js"]
}
}
<popup.js>
function start_action() {
var bloggerID = document.getElementById('bloggerID').value;
var handleQty = document.getElementById('handleQty').value;
var bkg = chrome.extension.getBackgroundPage();
bkg.doSomeThing(function(result) {
alert( result );
});
}
function restore_options() {
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('start').addEventListener('click',
start_action);
<content.js>
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if (request.action == "findCandidates") {
var oCandidateList = candidateList();
var nameList = new Array();
for (i = 0;i < oCandidateList.length; i++) {
nameList[i] = oCandidateList[i].innerHTML;
}
sendResponse( nameList );
}
}
);
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if (request.action == "openCandidatePage") {
var oCandidateList = candidateList();
for (i = 0;i < oCandidateList.length; i++) {
if ( oCandidateList[i].innerHTML == request.candidateName ) {
oCandidateList[i].click();
break;
}
}
sendResponse();
}
}
);
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if (request.action == "doFinalJobs") {
alert("do the final jobs");
}
}
);
function candidateList() {
var oTags = document.getElementsByTagName("a");
var oResultTags = new Array;
for (i=0; i<oTags.length; i++){
if ( oTags[i].hasAttribute("usercard") && oTags[i].hasAttribute("node-type") ) {
oResultTags[oResultTags.length] = oTags[i];
}
}
return oResultTags;
}
<background.js>
function doSomeThing (callback) {
chrome.tabs.getSelected(null, function (fatherTab) {
chrome.tabs.sendRequest(fatherTab.id, { action: "findCandidates" }, function (candidates) {
for (i = 0;i < 1; i++) {
chrome.tabs.sendRequest( fatherTab.id, { action: "openCandidatePage", candidateName: candidates[i] }, function(doNothing) {
callback("WHY HERE GOSE NO RESPOND"); // 就是这里开始任何代码都不执行 , 也不报错。 之前的所有功能都正常。
});
}
});
});
}