Fra*_*rez 22 sandbox google-chrome-extension content-script
我有一个Chrome扩展程序,如果window.my_variable_name存在变量,它会尝试查找每个浏览过的网址(以及每个浏览器网址的每个iframe).
所以我写了一小段内容脚本:
function detectVariable(){
if(window.my_variable_name || typeof my_variable_name !== "undefined") return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
在尝试太久之后,似乎Content Scripts在一些沙箱中运行.
有没有办法window从Chrome内容脚本访问该元素?
Fra*_*rez 54
要知道的一件事是,内容脚本与当前页面共享相同的DOM,但它们不共享对变量的访问权限.处理这种情况的最好方法是,从内容脚本中,将脚本标记注入到将读取页面中变量的当前DOM中.
在manifest.json中:
"web_accessible_resources" : ["/js/my_file.js"],
Run Code Online (Sandbox Code Playgroud)
在contentScript.js中:
function injectScript(file, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', file);
th.appendChild(s);
}
injectScript( chrome.extension.getURL('/js/my_file.js'), 'body');
Run Code Online (Sandbox Code Playgroud)
在my_file.js中:
// Read your variable from here and do stuff with it
console.log(window.my_variable);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18669 次 |
| 最近记录: |