3

I am building a Safari browser extension

I would like to call a function in a web page from an injected script

For instance if I created a script on a page from an injected script:

var newElement = document.createElement("script"); 
newElement.textContent = "function test(){alert(test);}"; 
document.body.insertBefore(newElement, document.body.firstChild);

Is it possible to then call that function from the injected script?

alternatively is it possible to do the reverse and call a function in an injected script from a webpage?

Thanks in advance.

EDIT:

For anyone who is interested I have currently fixed this issue by adding/removing a self executing function:

if(script!=null)
  script.parentNode.removeChild(script); 

script = document.createElement("script"); 
script.textContent = "(function(){test();})();";      
document.body.insertBefore(script, document.body.firstChild);

Not sure if there is a better why to achieve this?

1 Answers1

0

It appears this should be done with postMessage