Safari extension injected by script / webpage interaction

I am creating a Safari browser extension

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

For example, if I created a script on the page from the injected script:

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

      

Can I then call this function from the injected script?

alternatively is it possible to do the opposite and call a function in a nested script from a webpage?

Thanks in advance.

EDIT:

For anyone interested, I 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 an even better one for this?

+3


source to share


1 answer


It looks like it should be done with postMessage



0


source







All Articles