admin管理员组文章数量:1356265
I have this js function in my web page html code.
function update() {
document.getElementById( "textbox ").value = updatetext;
}
When I execute "update()" from chrome console, it works.
But if I execute from chrome extension,
chrome.tabs.executeScript(tab.id, {code: "update();"}, function(result){});
It says update is not defined. But if I replace with "alert('ok')", It works.
Then I execute
eval("update()")
in Chrome extension content script. It also says "update is not defined."
So what can i do to call js function on web page?
I have this js function in my web page html code.
function update() {
document.getElementById( "textbox ").value = updatetext;
}
When I execute "update()" from chrome console, it works.
But if I execute from chrome extension,
chrome.tabs.executeScript(tab.id, {code: "update();"}, function(result){});
It says update is not defined. But if I replace with "alert('ok')", It works.
Then I execute
eval("update()")
in Chrome extension content script. It also says "update is not defined."
So what can i do to call js function on web page?
Share Improve this question edited Mar 20, 2013 at 8:23 Leslie Wu asked Mar 20, 2013 at 7:40 Leslie WuLeslie Wu 7703 gold badges14 silver badges29 bronze badges 2- possible duplicate of Building a Chrome Extension - Inject code in a page using a Content script – Xan Commented Dec 12, 2014 at 10:52
- Or even better with regards to your questions in ments: Executing code at page-level from Background.js and returning the value – Xan Commented Dec 12, 2014 at 10:53
1 Answer
Reset to default 7Chrome executes content scripts in a sandbox, so you need to inject an inline script to interact with the webpage:
var injectedCode = 'update()';
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ injectedCode +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
本文标签: javascriptExecute web page js from chrome extension content scriptStack Overflow
版权声明:本文标题:javascript - Execute web page js from chrome extension content script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743985683a2571266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论