简体   繁体   English

Chrome扩展程序:未加载JavaScript

[英]Chrome extension: Javascript not loading

I have the following code for a chrome extension, the contentscript.py as per another question on stackoverflow: 我有以下代码用于chrome扩展,即关于stackoverflow的另一个问题的contentscript.py:

manifest.js manifest.js

{
"name": "test script",
"version": "0.1",
"content_scripts": [{
    "js": ["contentscript.js"],
    "matches": ["http://*/*"]
}],
"manifest_version": 2,
"web_accessible_resources": ["script.js"]
}

contentscript.js contentscript.js

var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

script.js script.js

document.body.innerHTML = document.body.innerHTML.replace(new RegExp("this", "gi"), "that");

In order to simply replace some text. 为了简单地替换一些文本。 If I put the same expression that is on script.js on a test html file, it works, but it in the extension it doesn't appear as though the code is actually injected. 如果我将test.js上的相同表达式放在测试html文件上,则它可以工作,但在扩展名中却不会像实际注入代码那样出现。 I cannot for the life of me figure out why. 我无法为自己的生活弄清楚原因。 The manifest and the contentscript seem in order, so I don't know what to do here. 清单和目录脚本看起来井井有条,所以我不知道该怎么办。

try adding "https://*/*" to matches in manifest.json. 尝试在manifest.json中的匹配项中添加“ https:// * / *”。 I recommend you to put this first line in your contentscript.js: 我建议您将第一行放在contentscript.js中:

console.log('Content script loaded and started');

So you can inspect the page (F12, console tab) and see if the content script was injected or not. 因此,您可以检查页面(F12,控制台选项卡),然后查看是否已插入内容脚本。 It'll help a lot with your debugging. 这将对您的调试有很大帮助。

I don't know what is going on in your script.js, but it worth consider that your removechild in the onload event may occur before your task being done. 我不知道您的script.js中发生了什么,但是值得考虑的是,onload事件中的removechild可能在完成任务之前发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM