Chrome扩展程序开发错误:必须在web_accessible_resources中列出资源

时间:2015-01-26 11:35:23

标签: javascript google-chrome google-chrome-extension ejs

我是编写Chrome扩展程序的新手。 我正在尝试在我注入的内容脚本中打开一个ejs模板,但是当我执行新的EJS({})时,我在content_script.js中得到低于错误

“拒绝加载chrome-extension://eadcfllkpeiejpedposonemnmmpnggie/content.html.ejs。必须在web_accessible_resources清单键中列出资源,才能通过扩展名以外的页面加载。”

注意:我在web_accessible_resource中添加了资源

的manifest.json

    {
    "name"              : "test",
    "description"       : " app",
    "version"           : "1.0",
    "manifest_version"  : 2,
    "browser_action"    :{
                            "default_icon"   : "images/icon.png"
                        },
    "permissions"       : ["tabs","notifications","contextMenus","background","http://*/*","https://*/*","<all_urls>"],
    "options_page"      : "views/options.html",
    "icons"             : {"16"         : "images/icon-16.png"//Icon context menu},
    "background"        : {
                            "page"          : "views/background.html",
                            "js"            : ["scripts/jquery-1.10.2.min.js"]
    },
    "web_accessible_resources": ["views/content.html.ejs"]
}

Background.js

    'use strict';

document.addEventListener("DOMContentLoaded", function(tab){
    chrome.contextMenus.create({
        title : "Add as reminder",
        id    : "rappelSelection",
        contexts: ["selection"],
        onclick: function(info,tab){addContent(info,tab)}
    })
});


function addContent(info, tab){
    var details = {};
    details.text = info.selectionText;
    chrome.tabs.executeScript(tab.id, {
        file : "scripts/jquery-1.10.2.min.js"
    });

    chrome.tabs.executeScript(tab.id, {
        file : "scripts/ejs_min.js"
    });

    chrome.tabs.executeScript(tab.id, {
        file : "scripts/content_script.js"
    });

    chrome.tabs.getSelected(null,function(tab){
        chrome.tabs.sendRequest(tab.id, {details: details}, function(response){
            console.log(response);
        });
    });
}

content_script

chrome.extension.onRequest.addListener(function newContent(request, sender, sendResponse){
        var template_path = chrome.extension.getURL('content.html.ejs');
        var data = {content: request.details.text };
        var template = new EJS({url:template_path}).render(data);
        $("body").append(template);
        sendResponse({contentText: "goodbye"}); 
    });

1 个答案:

答案 0 :(得分:0)

getURL方法需要文件的路径,而不仅仅是文件名的根目录 改变

var template_path = chrome.extension.getURL('content.html.ejs');

 var template_path = chrome.extension.getURL('views/content.html.ejs');

修正了问题。 谢谢@Xan需要另一双眼睛才能看到:)