我正在使用bookmarklet通过自定义JS脚本文件在文档中注入元素。我是这样做的:
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src = 'http://www.myurl.com/my_js_script.js';
var oldscript = document.getElementsByTagName('script')[0];
oldscript.parentNode.insertBefore(newscript, oldscript);
但我无法弄清楚如何实际执行它。有人能告诉我如何执行该JS文件?
注意:由于这也可以是Greasemonkey脚本,我也在为Greasemonkey标记这个问题。
答案 0 :(得分:2)
脚本标记在添加到文档时会自动下载并执行。但请注意,如果您注入的文档尚未包含任何<script>
标记,则您正在使用的脚本可能会失败,因为oldscript
将是未定义的。