即使在堆栈溢出中搜索了很多主题后,也没有任何帮助我修复此错误...
我正在尝试创建一个扩展,目前其中有简单的代码,但不幸的是,控制台没有记录“Hello, world!”来自 content_scripts 文件。
manifest.json
{
"manifest_version": 2,
"name": "Example",
"shortname": "exmpl",
"description": "__MSG_appDesc__",
"version": "0.0.1",
"default_locale": "en",
"author": "Mateus Akino",
"icons": {
"16": "i16x.png",
"48": "i48x.png",
"128": "i128x.png"
},
"homepage_url": "http://example.com/",
"browser_action": {
"default_icon": "i32x.png",
"default_popup": "popup.html"
},
"update_url": "http://example.com/update.xml",
"chrome_url_overrides": {
"newtab": "newtab.html"
},
"content_scripts": [{
"matches": ["*://*.youtube.com/*"],
"js": ["execute.js"],
"run_at": "document_end"
}],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"activeTab", "tabs", "i18n", "management", "webNavigation", "<all_urls>"
]
}
execute.js
console.log("Hello, world!");
背景.js
chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) {
chrome.tabs.executeScript(null, {
file: "execute.js"
});
});
最佳答案
我解决了这个问题,所以如果其他人也有同样的问题,我会将其发布在这里。
看起来代码没问题,问题是我加载扩展的方式......
由于我使用“加载解压的扩展程序”,我的 manifest.json
并未仅通过禁用和启用它来更新(也不是通过使用立即刷新扩展程序)。
所以我删除了扩展程序,重新加载它,现在它可以正常工作了。
关于javascript - Google Chrome content_script 未在匹配的 URL 上加载 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41293997/