Google chrome extension Can';t从Chrome扩展文件系统加载html文件
我正在尝试从Chrome扩展名的文件系统加载一个HTML文件。我在清单中添加了“modal.html”作为可通过web访问的资源:Google chrome extension Can';t从Chrome扩展文件系统加载html文件,google-chrome-extension,Google Chrome Extension,我正在尝试从Chrome扩展名的文件系统加载一个HTML文件。我在清单中添加了“modal.html”作为可通过web访问的资源: { "name": "Test Extension", "action": {}, "manifest_version": 3, "version": "0.1", "description": "
{
"name": "Test Extension",
"action": {},
"manifest_version": 3,
"version": "0.1",
"description": "Just learning for now",
"permissions": [
"activeTab",
"scripting",
],
"background": {
"service_worker": "background.js"
},
"web_accessible_resources": [
{
"resources": [ "modal.html" ],
"matches": [ "https://mypage.co.uk/*" ]
}
]
}
My background.js文件尝试加载带有xhr的modal.html:
function initPage() {
let node = document.createElement("div");
node.setAttribute("id", "mySpecialId");
document.body.appendChild(node);
var xhr = new XMLHttpRequest();
xhr.open("GET", chrome.runtime.getURL('modal.html'), true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
document.getElementById("mySpecialId").innerText = xhr.responseText;
}
}
xhr.send();
}
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: initPage
});
});
我得到一个错误:
Denying load of chrome-extension://lmdjgmkmficfccbahcpgnmaplajdljid/modal.html. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
我确信这是一个非常基本的问题,但我就是想不出来。我通过在
web\u可访问资源
声明中添加use\u dynamic\u url
键实现了这一点:
"web_accessible_resources": [
{
"resources": [ "modal.html" ],
"matches": [ "https://www.mypage.co.uk/*" ],
"use_dynamic_url": true
}
]
我还不明白为什么这会奏效
编辑:这是Chrome中的一个bug。原始代码是正确的,并在重新启动后正常工作。听起来您正在将代码注入manifest.json中的
匹配项
未包含的页面。谢谢,这为我指明了正确的方向。唯一的解释是Chrome中的一个bug,因为使用动态url
不能解决最初的问题,这意味着实际的问题是其他问题(很可能是单击了一个选项卡上的扩展图标,该选项卡上的url没有被匹配项所覆盖
)我想这可能是Chrome的一个bug,因为我必须重新启动才能让任何东西正常工作。我已经从清单中删除了use_dynamic_url键,它仍然有效。令人沮丧的东西!