druidAAAA2000 2016-06-02 10:50 采纳率: 50%
浏览 1859
已结题

(chrome 扩展)javascript的异步嵌套函数调用, 请教前辈!!!!

背景:
小弟开发了一个在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"); // 就是这里开始任何代码都不执行 , 也不报错。 之前的所有功能都正常。
                    });
                }


            });

        });
  }
  • 写回答

2条回答 默认 最新

  • druidAAAA2000 2016-06-02 11:03
    关注

    漏了一个文件popup.html, 除了这些其他的文件都没用的可以在manifest中去掉。

     <!DOCTYPE html>
    <html>
    <head><title>My Test Extension Options</title></head>
    <body>
    
    <label>
        enter bloggerID
        <input type="text" value="" id="bloggerID"/>
        enter handleQty
        <input type="text" value="" id="handleQty"/>
    </label>
    
    <div id="status"></div>
    <button id="start">Start</button>
    
    <script src="popup.js"></script>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题