简体   繁体   English

Chrome扩展程序消息队列

[英]Chrome extension message queue

I developing chrome extension, and after some additional scripts are almost done, I faced with messaging problem. 我正在开发chrome扩展程序,在完成一些其他脚本后,我遇到了消息传递问题。 I wrote some kind of test code, and noticed, that at a time, onMessage listener can process message only from one script. 我编写了某种测试代码,并注意到onMessage侦听器一次只能处理来自一个脚本的消息。 Here's testing code: 这是测试代码:

background.js background.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log(request.action);
    if (request.action == 'first') {
         sendResponse({ action: 'first_next', arg: 1 });
    } else
    if (request.action == 'second') {
        sendResponse({ action: 'second_next', arg: 1 });
    }

    return true;
});

chrome.tabs.executeScript(active_tab, {file: 'first.js'});
chrome.tabs.executeScript(active_tab, {file: 'second.js'});

This background script listens and sends response for each message. 该后台脚本侦听并发送每个消息的响应。

first.js first.js

function wait() {
    done('OK');
}

function done(result) {
    var msg = { action: 'first', result: result };
    chrome.runtime.sendMessage(msg, function(response) {
        if (response.action === 'first_next') {
            setTimeout(wait, 400);
        } else 
        {
            setTimeout(wait, 2000);
        }
    });
}

wait();

second.js second.js

function wait() {
    done('OK');
}

function done(result) {
    var msg = { action: 'second', result: result };
    chrome.runtime.sendMessage(msg, function(response) {
        if (response.action === 'second_next') {
            setTimeout(wait, 400);
        } else 
        {
            setTimeout(wait, 2000);
        }
    });
}

wait();

So, console log should be like: 因此,控制台日志应类似于:

first
second
first
...
first
first
second
first
second
...

But, it is: 但它是:

first
second
second
second
second
second
...

That says that I can process only one message in time. 那就是说我只能及时处理一条消息。 And i don't know how to solve my problem, maybe something like messaging queue (idk how to do this) or smth else ? 而且我不知道如何解决我的问题,也许是诸如消息队列(idk如何做到)之类的东西? Thanks in advance for any help. 在此先感谢您的帮助。

The problem here isn't with the Chrome API. 这里的问题不在于Chrome API。 You have two functions with the same name injected onto the page. 您在页面上注入了两个具有相同名称的函数。 So basically the following happens: 因此,基本上会发生以下情况:

  1. You inject first.js and it executes done function 您注入first.js并执行done功能
  2. You inject second.js and it executes done function 您注入second.js并执行done功能
  3. done from second.js overwrites the defintion of done from first.js donesecond.js覆盖的确定指标donefirst.js
  4. Each time first.js calls done it will call the done as defined in second.js 每次first.js电话done它会调用done中定义second.js

To see a proof of this, change done in second.js to done2 and then execute the code. 要看到证明了这一点,改donesecond.jsdone2 ,然后执行代码。 You should get the desired results. 您应该得到预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM