Javascript 在后台和内容脚本之间通过promise传递响应回调?

Javascript 在后台和内容脚本之间通过promise传递响应回调?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我试图通过内容脚本将html模板注入到网页的DOM中。我更喜欢将html文件保存在扩展名中自己的文件夹中,以便易于维护。这意味着为了注入模板,内容脚本必须向后台发送消息,然后后台将向相应的文件夹发送get请求,并通过回调返回检索到的模板。但是,内容脚本没有收到来自后台页面的响应 contentscript.js chrome.runtime.sendMessage({action: "template request"}, function(response) { //this shou

我试图通过内容脚本将html模板注入到网页的DOM中。我更喜欢将html文件保存在扩展名中自己的文件夹中,以便易于维护。这意味着为了注入模板,内容脚本必须向后台发送消息,然后后台将向相应的文件夹发送get请求,并通过回调返回检索到的模板。但是,内容脚本没有收到来自后台页面的响应

contentscript.js

chrome.runtime.sendMessage({action: "template request"}, function(response) {
    //this should be html
    console.log(response) //hi, <div>template!</div>
});
chrome.runtime.onMessage.addListener(function(request, sender, response) {

    //ignore all non-template requests
    if (request.action !== "template request") return;

    //start the ajax promise
    getTemplate('templates/foobar.html')
        .then(function(data) {
            //we're still in the scope of the onMessage callback
            response(data) //but this does nothing, content script logs no errors
            console.log(data) //<div>foobar</div>
        })
        .catch(function(err) {
            //...
        });

    //however...
    response('hi') //the content script successfully receives this response callback
    response('<div>template!</div>') //the content script can also successfully receive this and inject it with jquery.
});

function getTemplate(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({ 
            type: "GET",
            url: url,
            success: resolve
        })
    });
}
getTemplate('templates/template.html')
    .then(function(html) {
        var el = $(html);
        $('body').append(el);
    });

function getTemplate(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({
            type: "GET",
            url: chrome.extension.getURL(url),
            success: resolve
        });
    }
}

我已将模板文件夹包含在
web\u accessible\u resources
中,因此我认为这不是一个明显的问题。有什么建议吗?

没关系……您甚至不需要调用后台脚本,因为内容脚本可以访问
chrome.extension.getURL
。所以你可以这样做:

contentsript.js

chrome.runtime.sendMessage({action: "template request"}, function(response) {
    //this should be html
    console.log(response) //hi, <div>template!</div>
});
chrome.runtime.onMessage.addListener(function(request, sender, response) {

    //ignore all non-template requests
    if (request.action !== "template request") return;

    //start the ajax promise
    getTemplate('templates/foobar.html')
        .then(function(data) {
            //we're still in the scope of the onMessage callback
            response(data) //but this does nothing, content script logs no errors
            console.log(data) //<div>foobar</div>
        })
        .catch(function(err) {
            //...
        });

    //however...
    response('hi') //the content script successfully receives this response callback
    response('<div>template!</div>') //the content script can also successfully receive this and inject it with jquery.
});

function getTemplate(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({ 
            type: "GET",
            url: url,
            success: resolve
        })
    });
}
getTemplate('templates/template.html')
    .then(function(html) {
        var el = $(html);
        $('body').append(el);
    });

function getTemplate(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({
            type: "GET",
            url: chrome.extension.getURL(url),
            success: resolve
        });
    }
}