【发布时间】:2013-11-11 10:57:48
【问题描述】:
根据 stackoverflow 上的另一个问题,我有以下用于 chrome 扩展的代码 contentscript.py:
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
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
document.body.innerHTML = document.body.innerHTML.replace(new RegExp("this", "gi"), "that");
为了简单地替换一些文本。如果我将与 script.js 上的相同表达式放在测试 html 文件上,它可以工作,但它在扩展名中看起来并没有实际注入代码。我终其一生都无法弄清楚为什么。 manifest 和 contentscript 似乎是有序的,所以我不知道在这里做什么。
【问题讨论】:
-
你为什么不从
contentscript.js中做你的 stuuf 而不是注入另一个脚本?内容脚本确实可以访问 DOM。 -
顺便说一句,你的代码对我来说也很好用。您的
scripts.js在哪里?你确定是注入的?再说一遍:为什么还要使用另一个脚本(您必须将其声明为“网络可访问资源”,而不是在内容脚本中做同样的事情? -
显然,内容脚本是在隔离环境中执行的,如下所示:stackoverflow.com/questions/9515704/…
标签: javascript google-chrome google-chrome-extension