Execute web page js from chrome extension content script
google-chrome, google-chrome-extension, javascript
Solution
Chrome 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);
Problem
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?