Run js webpage from contents of chrome script extension

I have this js function in my html code of a webpage.

function update() {
    document.getElementById( "textbox ").value = updatetext;
}

When I execute "update ()" from the Chrome console, it works.
But if I do the chrome extension,

chrome.tabs.executeScript(tab.id, {code: "update();"}, function(result){});

It says that the update is undefined. But if I replace with "alert (" ok "), it works.
Then I do

eval("update()")

in the contents of the Chrome script extension. It also states that "the update is undefined."

So what can I do to call the js function on a web page?

+5
source share
1 answer

Chrome , script -:

var injectedCode = 'update()';
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ injectedCode +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
+7

All Articles