Chrome extension - inject template through content

2019-08-12 11:02发布

问题:

I'm trying to inject a handlebars template in a page using content_scripts from the Chrome extension, using the following code in the page called from content_scripts :

var xhr = new XMLHttpRequest();
xhr.open('GET', chrome.extension.getURL('app/templates/tpl.handlebars'), true);
xhr.onreadystatechange = function() {
    if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
        var tplScript = document.createElement('script');
        tplScript.text = xhr.responseText;
        tplScript.id = "mytpl";
        tplScript.type = "text/x-handlebars-template";
        (document.head || document.documentElement).appendChild(toolbarScript);
    }
};
xhr.send();

But in the console then I see :

Denying load of chrome-extension://hnblpfiofigbkdcdfohdaeifopkjkngj/app/templates/tpl.handlebars. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

I have it in the relevant part of the manifest :

"web_accessible_resources": [
    "lib/jquery/jquery-2.1.4.min.js",
    "lib/handlebars/handlebars-v3.0.3.js",
    "main.js",
    "app/templates/tpl.handlebars"
  ]

The other scripts run without any problem. Did I miss something ?

[EDIT] fixed typo in web_accessible_resources, sorry

Thanks !