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

Chrome Extension development error : Resources must be listed in the web_accessible_resources

本文关键字:accessible web resources 程序开发 扩展 错误 资源 Chrome      更新时间:2023-09-26

我是写chrome扩展的新手。我正在尝试在注入的内容脚本中打开一个 ejs 模板,但是当我执行新的 EJS({}) 时,content_script.js出现以下错误

"拒绝铬 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"]
}

背景.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"}); 
    });

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

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

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

修复了此问题。多亏@Xan需要另一双眼睛才能看到那:)