繁体   English   中英

chrome.tabs.sendMessage将无法在异步方式下正常运行

[英]chrome.tabs.sendMessage won't work in async then function

在后台脚本中

function new_url_listener(tabId, info, tab) {

    if(info.url){
        get_template(info.url).then(function(template){
            console.log(template);
            if (template){
                chrome.tabs.sendMessage(tab.id, {test:"test"}, function(response) {    
                    console.log(response);
                });
            }        
        })
    }
}



chrome.tabs.onUpdated.addListener(new_url_listener);

在content.js中

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      console.log(sender.tab ?
                  "from a content script:" + sender.tab.url :
                  "from the extension");
      if (request.greeting == "hello")
        sendResponse({farewell: "goodbye"});
});

当chrome.tabs.sendMessage不包含在get_template.then()调用中时,它将发送一条消息,但是我需要能够将模板变量发送到内容脚本

为什么不在then呼叫中发送?

get_template调用api,并以json模板形式返回json

"content_scripts": [
    {
        "matches": ["http://*/*", "https://*/*"],
        "js": [
            "contentscript.js"
        ],
        "run_at": "document_start"

    }
],

我必须添加“ run_at”:“ document_start”,现在它可以工作了

暂无
暂无

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

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