Tho*_*ard 3 javascript greasemonkey
是否可以使用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 );
})();
Run Code Online (Sandbox Code Playgroud)
如果您只是希望脚本运行,那么这就足够了.如果你想在你的用户脚本中使用像jQuery这样的库,它会变得棘手.我知道有两种方法:
scriptElement.onload = function () {}需要你必须使用unsafeWindow来访问你的库中的变量.如果这是一个纯的greasemonkey脚本,我建议使用第一种方法,因为只有脚本是从站点封装的.