Chrome扩展资源必须列在web_accessible_resources清单键中

use*_*593 0 javascript google-chrome-extension

我正在尝试在chrome中发送httpget请求,但是我收到此错误 必须在web_accessible_resources清单键中列出资源

这是我的按钮代码

contentInput.onclick = function(){
    var assetid = $('.thumbnail-span').attr("data-3d-url")
    var baseurl = 'http://www.roblox.com'
    var xhr = new XMLHttpRequest();
    xhr.open("GET", chrome.extension.getURL(baseurl + assetid), true);
    var result = xhr.responseText;
    xhr.send();
    console.log(result)
        chrome.extension.sendRequest({
            action: "EditContent",
            type: assetType,
            name: assetName, 
            content: contentData
        })
Run Code Online (Sandbox Code Playgroud)

和我的清单文件

{

        "name": "ROBLOX Object Downloader .obj",
        "short_name": "OBJDownloader",
        "description": "Allows you to quickly download assets from the browser as a .obj ",
        "version": "1.0.0",
        "icons": {"128":"icon.png"},
        "permissions": [
            "http://*.roblox.com/*",
            "http://*.rbxcdn.com/*",
            "downloads",
            "downloads.open"
        ],
        "background": {"scripts":["background.js"]},
           "content_scripts": [
                    {
                            "matches": ["http://*.roblox.com/*-item?id=*"],
                            "js": ["item.js","jquery.js"]
                    },
                    {
                            "matches": ["http://*.roblox.com/*ser.aspx*"],
                            "js": ["user.js","jquery.js"]
                    },
                    {
                            "matches": ["http://*.roblox.com/*atalog/*"],
                            "js": ["cataloginsert.js","jquery.js"]
                    }
            ],
        "manifest_version": 2
    }
Run Code Online (Sandbox Code Playgroud)

aps*_*ers 5

chrome.extension.getURL函数用于从位于扩展程序目录内的您自己的计算机获取文件:

string chrome.extension.getURL(string path):将扩展安装目录中的相对路径转换为完全限定的URL.

这意味着您的Ajax请求正在尝试访问类似的URL

chrome-extension://aaaaaabbbbbccccccdddddd/http://www.roblox.com/some-asset-id
Run Code Online (Sandbox Code Playgroud)

为了访问文件chrome-extension://,您必须通过web_accessible_resource清单字段使网页可以显式访问它们.

但是,您可能只想获取Web URL http://www.roblox.com/some-asset-id.万一,getURL完全不合适.只需执行以下操作:

xhr.open("GET", baseurl + assetid, true);
Run Code Online (Sandbox Code Playgroud)

你的代码中有一个额外的问题,这是它不等待异步Ajax调用来完成.你应该等待这个load事件,然后阅读responseText:

contentInput.onclick = function(){
    ...
    var xhr = new XMLHttpRequest();
    xhr.open("GET", chrome.extension.getURL(baseurl + assetid), true);

    xhr.addEventListener("load", function() {
        var result = xhr.responseText;
        console.log(result);
        doSomethingElseWith(result);
        // all uses of `result` must be inside this function
    });

    xhr.send();

    ...
Run Code Online (Sandbox Code Playgroud)