多功能的呼吁。运行时间。图()同步的行为

0

的问题

我连接content.js 与background.js 要做到2种不同的任务:注入当地HTML和获取数据从另一个网页。 目前, createContainer() 之后开始 fetchweb() 是的,我不知道为什么(我需要的 createContainer() 首先运行). 我试图变换这两种功能进入承诺,但仍然同样的结果

Content.js

function createContainer1() {
    // call html file container
    chrome.runtime.sendMessage({ cmd: "read_cont1" }, function (html) {
        $("#container1").html(html);
    });
    // more code
}
    
function fetchWeb() {
    chrome.runtime.sendMessage(
        { cmd: "send_url", url: window.location.href},
        function (response) {
            console.log(JSON.stringify(response));
        }
    );
}

createContainer1()
fetchWeb()

background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.cmd == "read_cont1") {
        $.ajax({
            url: chrome.extension.getURL("container1.html"),
            dataType: "html",
            success: sendResponse,
        });
        return true;
    } else if (request.cmd == "send_url") {
        sendResponse({ review_url: fetchData(request.url) });
        return true;    
    }
});
google-chrome-extension javascript
2021-11-18 10:43:15
1

最好的答案

1

你的两个sendMessages都是异步的功能以及--除非另有具体处理异步编码,通过回调的承诺,或异步/等待着-我不认为有任何其他方式保证其解决的第一个。

如果fetchWeb应该运行的每一次之后createContainer发送消息,你可以添加fetchWeb到图的callback(然后把它从你的主体):

...chrome.runtime.sendMessage({ cmd: "read_cont1" }, function (html) {
    $("#container1").html(html);
    fetchWeb();
});...

如果fetchWeb应该只有时运行,您可以通过数据进createContainer功能回答这个问题:

function createContainer1(executeFetchWeb) {
    // call html file container
    chrome.runtime.sendMessage({ cmd: "read_cont1" }, function (html) {
        $("#container1").html(html);
        if (executeFetchWeb) {fetchWeb()}
    });
    // more code
}

如果有别的事情发生在"//more code"需要发生之前fetchWeb运行,这将是有益的。 但除非代码是异步的同时,我想像那个代码已经执行的第一次。 这可以完成所有承诺,但图已经安装工作以及与回调。 从 文件:

chrome.runtime.sendMessage(
  extensionId?: string,
  message: any,
  options?: object,
  responseCallback?: function,
)
2021-11-21 02:51:53

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................