Les*_* Wu 5 javascript google-chrome google-chrome-extension
我在我的网页html代码中有这个js函数.
function update() {
document.getElementById( "textbox ").value = updatetext;
}
Run Code Online (Sandbox Code Playgroud)
当我从chrome控制台执行"update()"时,它可以工作.
但是如果我从chrome扩展程序执行,
chrome.tabs.executeScript(tab.id, {code: "update();"}, function(result){});
Run Code Online (Sandbox Code Playgroud)
它说没有定义更新.但如果我用"alert('ok')替换",它就有效.
然后我执行
eval("update()")
Run Code Online (Sandbox Code Playgroud)
在Chrome扩展程序内容脚本中.它还说"未定义更新".
那么我该怎么做才能在网页上调用js函数呢?
Chrome在沙箱中执行内容脚本,因此您需要注入内联脚本以与网页进行交互:
var injectedCode = 'update()';
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ injectedCode +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5118 次 |
| 最近记录: |