执行通过 bookmarklet 作为脚本元素注入的 javascript?
我正在使用 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 标记了这个问题。
I am using bookmarklet to inject a element in document with a custom JS script file. I did it like this:
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);
But I can't figure out how to actually execute it. Can someone tell me how can I execute that JS file?
Note: Since this can be a Greasemonkey script as well, I am tagging this question for Greasemonkey as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
脚本标签添加到文档后会自动下载并执行。但请注意,如果您要注入的文档尚未包含任何
标记,则您使用的脚本可能会失败,因为
oldscript
将未定义。Script tags are automatically downloaded and executed when they're added to the document. Note, however, that the script you're using may fail if the document you're injecting into doesn't already contain any
<script>
tags, asoldscript
will be undefined.