Google chrome extension 文档加载完成后,有没有办法替换文档js功能?
加载的html页面包含一个js函数:Google chrome extension 文档加载完成后,有没有办法替换文档js功能?,google-chrome-extension,Google Chrome Extension,加载的html页面包含一个js函数: <html> .... <body> <script> function test(){ alert("I'm master"); //here is the page original funciton } </script> </body> </html> 我该怎么办?Chrome会立即执行内联脚本,您将无法阻止它们运行。在解析html之前,我们无法修
<html>
....
<body>
<script>
function test(){
alert("I'm master");
//here is the page original funciton
}
</script>
</body>
</html>
我该怎么办?Chrome会立即执行内联脚本,您将无法阻止它们运行。在解析html之前,我们无法修改它: 您唯一的机会是只声明函数,但页面脚本不会立即调用函数。在这种情况下,存在有效的解决方案 声明一个内容脚本,该脚本将使用新函数声明注入
元素:
- manifest.json
“内容脚本”:[{ “匹配项”:[“http://somedomain.com/blabla*"], “js”:[“content.js”] }],
- content.js
document.body.appendChild(document.createElement('script')).text= getFunctionCode(函数(){ 功能测试(){ 警报(“我是我的chrome扩展的新功能”); } }); 函数getFunctionCode(fn){ 返回fn.toString().match(/\{([\s\s]*?)\}$/)[1]; }
另请参见:。如果Chrome立即执行内联脚本,您将无法阻止它们运行。在解析html之前,我们无法修改它: 您唯一的机会是只声明函数,但页面脚本不会立即调用函数。在这种情况下,存在有效的解决方案 声明一个内容脚本,该脚本将使用新函数声明注入
元素:
- manifest.json
“内容脚本”:[{ “匹配项”:[“http://somedomain.com/blabla*"], “js”:[“content.js”] }],
- content.js
document.body.appendChild(document.createElement('script')).text= getFunctionCode(函数(){ 功能测试(){ 警报(“我是我的chrome扩展的新功能”); } }); 函数getFunctionCode(fn){ 返回fn.toString().match(/\{([\s\s]*?)\}$/)[1]; }
new function name =>
function test() {
alert("I'm a new function come my chrome extension ");
}