需要帮助chrome扩展

时间:2017-11-27 07:47:19

标签: javascript google-chrome-extension

Content.js

function callbacknumberoflinks(callback, document_root, obj) {
    var obj
    chrome.storage.local.get("fiterlink", function (filterlist, obj) {
        alert("sync");
        callback(filterlist, document_root, obj);
    });
    return obj;

}

function test(document_root)
{
   var obj;
   obj=callbacknumberoflinks(GetFilterfromStore3, document, obj)
   alert(obj + " : test");
   return obj;
}

function GetFilterfromStore3(filterlist, document_root,obj) {
    debugger;
    var numberdata = 0;
    var links = [];
    var number = 0;
    node = document_root.getElementsByTagName("a");

    for (var i = 0; i < filterlist.fiterlink.length; i++) {
        if (filterlist.fiterlink[i].data.selected == true) {
            links[i] = filterlist.fiterlink[i].data.link;
        }
    }
    for (var i = 0; i < node.length; i++) {
        for (var j = 0; j < links.length; j++) {
            if (node[i].getAttribute("href") != null) {
                if (node[i].getAttribute("href").indexOf(links[j]) != -1) {
                    number++;
                }
            }
        }
    }
    //alert(number + " :1");
    obj = number;
    return (numberdata);
};

chrome.runtime.sendMessage({
    //number1:alert("sendmsg"),
    action: "getnumberoflink",
    number: test(document),
});

Event.js

chrome.tabs.onSelectionChanged.addListener(function (activeInfo) {

    chrome.tabs.executeScript(null, { file: 'Getnumberoflinks.js' });
    // Perform the callback when a message is received from the content script

    chrome.runtime.onMessage.addListener(function (message) {
        if (message.action == "getnumberoflink") {
            //alert("hello world")
            chrome.browserAction.setBadgeBackgroundColor({ color: [255, 0, 0, 255] });
            alert(message.number + " : event");
           // alert(message.number1.toString() +"number1");
            chrome.browserAction.setBadgeText({ text: message.number.toString() });
        }
    });
});

我在chrome扩展中需要帮助.Event.js在后台运行我想从GetFilterfromStore3函数传递数字。但是我从测试函数得到第一个警报然后callbacknumberoflinks函数和event.js但我不知道如何传递任何东西GetFilterfromStore3函数来测试。请帮帮我。

1 个答案:

答案 0 :(得分:0)

这里的问题是你对callbacknumberoflinks()的异步调用。在从回调中接收值之前,您的警告语句正在执行。

我不确定,但你可以试试这个 -

function callbacknumberoflinks(document_root, obj,callback) {
    /* your code */
    chrome.storage.local.get("fiterlink", function (filterlist, obj) {
        /* your code */
        callback(filterlist, document_root, obj);
    });

}

function test(){
   var obj;
   callbacknumberoflinks(document, obj, function(filterlist, document_root, obj){
      GetFilterfromStore3(filterlist, document_root,obj, function(numdata){
         obj=numdata;
         alert(obj + " : test");
         /* your code */
      });
   });
}

function GetFilterfromStore3(filterlist, document_root,obj,callback) {
    /* your code */
    obj = number;
    callback(numberdata);
};

这应该是从GetFilterfromStore3()test()获取数据的方式。