能否使用Greasemonkey脚本将其他网站的JS脚本添加到页面中并使其运行?
能否使用Greasemonkey脚本将其他网站的JS脚本添加到页面中并使其运行?
// ==UserScript==
// @name My Fancy New Userscript
// @description enter something useful
// @match http://*/*
// ==/UserScript==
(function () {
var scriptElement = document.createElement( "script" );
scriptElement.type = "text/javascript";
scriptElement.src = "url to your script";
document.body.appendChild( scriptElement );
})();
scriptElement.onload = function() {}
,然后您必须使用unsafeWindow
来访问库中的变量。@require
。你不需要将这段代码包装在匿名函数中。 - Brock AdamsscriptElement
来拉取多个外部 JavaScript 文件吗?(例如 script1.js 和 script2.js) - Thomas WardscriptElement1
、scriptElement2
和scriptElement3
,然后一切都正常了。我也找出了为什么它没有拉取数据的原因——DNS解析出了问题,因为我忘记更新本地DNS服务器信息 xD。 - Thomas Ward是的,你可以像这样做:
let script = document.createElement('script');
script.src = "http://example.com/somescript.js";
document.body.appendChild(script); // execute the script
document.body.removeChild(script);