简体   繁体   English

Safari扩展注入了脚本/网页交互

[英]Safari extension injected script/webpage interaction

I am building a Safari browser extension 我正在构建Safari浏览器扩展

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? 不确定是否有更好的原因来实现这一目标?

看来这应该用postMessage来完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM