Google chrome extension 如果后台脚本尚未准备好,内容脚本中的sendRequest()是否可能丢失?

Google chrome extension 如果后台脚本尚未准备好,内容脚本中的sendRequest()是否可能丢失?,google-chrome-extension,Google Chrome Extension,有时从内容脚本发送的请求似乎丢失了,我猜这是在后台脚本还没有准备好时发生的。如何防止这种情况 谢谢 编辑: 这是我目前的解决办法: // This method retries to send the request if response is not received during 1 second: function sendWaitForResponse(request) { var response = null; chrome.extension.sendReques

有时从内容脚本发送的请求似乎丢失了,我猜这是在后台脚本还没有准备好时发生的。如何防止这种情况

谢谢

编辑:

这是我目前的解决办法:

// This method retries to send the request if response is not received during 1 second:
function sendWaitForResponse(request) {
    var response = null;
    chrome.extension.sendRequest(request, function(r) {
        response = r;
    });
    setTimeout(function() {
        if (!response) {
            sendWaitForResponse(request);
        }
    }, 1000);
}

背景脚本没有准备好?为什么会发生这种情况?您是否在后台页面中执行同步操作?我正在通过命令行执行chromium,并将URL作为参数传递。在这种情况下,我认为,内容脚本可以在背景之前加载。我很想知道一种重现这种情况的方法。一种缓解策略可能是检查请求是否通过,然后使用指数回退或类似方法稍后重试。这就是我目前解决该问题的方法。请参阅我的编辑。顺便说一句,当从命令行以这种方式调用我的扩展时,这种情况经常发生。