[英]How do I include a remote javascript file in a Greasemonkey script?
I'm trying to write a Greasemonkey script, and would like to use the jQuery library to do so, but I'm not quite sure how I would include jQuery from a web address to get rolling. 我正在尝试编写一个Greasemonkey脚本,并希望使用jQuery库来执行此操作,但我不太确定如何从Web地址中包含jQuery以进行滚动。
How would I include jQuery (from Google's web server) into the greasemonkey script so that I can just go: 我如何将jQuery(来自Google的Web服务器)包含在greasemonkey脚本中,以便我可以去:
$(document).ready(function(){
// Greasemonkey stuff here
});
I'd prefer to get it from this source: 我更愿意从这个来源获得它:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
Update: Thanks for the help, the answers were very informative. 更新:感谢您的帮助,答案非常丰富。 I did, however, utilize my GoogleFu a little more and came across this solution : http://joanpiedra.com/jquery/greasemonkey/
不过,我确实更多地利用了我的GoogleFu并遇到了这个解决方案 : http ://joanpiedra.com/jquery/greasemonkey/
Works like a charm.. just update the source to google's hosted version of jQuery to complete. 像魅力一样工作..只需更新谷歌的托管版jQuery即可完成。
The recommended way in recent versions of greasemonkey is to use the @require comment tag. 最近版本的greasemonkey中推荐的方法是使用@require注释标记。
Eg 例如
// ==UserScript==
// @name Hello jQuery
// @namespace http://www.example.com/
// @description jQuery test script
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
However... be aware that jQuery 1.4.1 and 1.4.2 are incompatible with this method 但是......请注意,jQuery 1.4.1和1.4.2与此方法不兼容
Thanks Paul Tarjan, for pointing this out. 感谢Paul Tarjan指出这一点。 See jQuery forum thread .
请参阅jQuery论坛帖子 。
Also be aware of these @require semantics 还要注意这些@require语义
At the time that the user script is installed, Greasemonkey will download and keep a locally cached copy of the remote file that can be read almost instantly. 在安装用户脚本时,Greasemonkey将下载并保留远程文件的本地缓存副本,该副本几乎可以立即读取。 The cached copy is kept in the same folder as your installed user-script.
缓存的副本与安装的用户脚本保存在同一文件夹中。 The remote file is not monitored for changes.
不监视远程文件的更改。
Please be aware that, at the time of writing this answer, this @require tag is only read at install-time. 请注意,在撰写本答案时,此@require标记仅在安装时读取。 If you edit an existing user script to add this tag, it will be ignored.
如果编辑现有用户脚本以添加此标记,则将忽略该标记。 You need to uninstall and re-install your user script to pick up the change.
您需要卸载并重新安装用户脚本才能获取更改。
// ==UserScript==
// @name jQueryTest
// @namespace http://www.example.com/
// @include *
// ==/UserScript==
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined')
{ window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
// All your GM code must be inside this function
function letsJQuery() {
alert($); // check if the dollar (jquery) function works
// the next call fails
$("<div id='example' class='flora' title='This is my title'>I'm in
a dialog!</div>").dialog({
buttons: {
"Ok": function() {
alert("Ok");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
}
It works perfectly, but you may want to limit the sites it runs on or host the jQuery js file on your own site so as not to steal their bandwidth. 它运行良好,但您可能希望限制它运行的站点或在您自己的站点上托管jQuery js文件,以免窃取他们的带宽。
You could try dynamically creating a script element: 您可以尝试动态创建脚本元素:
var script = document.createElement("script");
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
document.getElementsByTagName("head")[0].appendChild(script);
You may need to delay for a bit while the script loads (setTimeout?) 脚本加载时你可能需要延迟一点(setTimeout?)
Based on Chris's answer , I adjust to my own needs like following. 根据克里斯的回答 ,我调整了自己的需求,如下所示。
// ==UserScript==
// @description require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.jss
// @name Baidu Mask
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.ibm.com/developerworks/cn/opensource/os-cn-greasemonkey/index.html
// @match *://www.baidu.com/*
// @match *://baike.baidu.com/*
// @match *://zhidao.baidu.com/*
// @match *://www.weather.com.cn/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Your code here...
var $j;
function GM_wait() {
if (typeof jQuery === 'undefined') {
window.setTimeout(GM_wait, 100);
}
else {
$j = jQuery.noConflict();
doJob();
}
}
function loadJquery() {
// Check if jQuery's loaded
if (typeof jQuery === 'undefined') {
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'https://code.jquery.com/jquery-1.12.4.min.js';
GM_JQ.type = 'text/javascript';
GM_JQ.id = 'jquery-lyz';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
GM_wait();
} else {
doJob();
}
}
loadJquery();
function doJob() {
if (typeof $j === 'undefined') {
$j = $;
}
var url_arr = [
{'name': "baidu", 'value': "www.baidu.com"},
{'name': "baike", 'value': "baike.baidu.com"},
{'name': "zhidao", 'value': "zhidao.baidu.com"},
{'name': "weather", 'value': "http://www.weather.com.cn"},
];
var url = location.href;
var siteObj = {};
$j(url_arr).each(function (i, item) {
if (url.indexOf(item.value) > -1) {
siteObj = item;
return false;
}
});
var delay_arr = [];
var timerCount = 1;
function hideTimer() {
timerCount++;
if (timerCount > 20) {
return;
}
delay_arr = $j.grep(delay_arr, function (_selector, i) {
var $ele = $j(_selector);
var visible = $ele.is(':visible');
console.log($ele, visible);
if (visible) {
$ele.hide();
return false;
}
return true; // keep the element in the array
});
if (delay_arr.length > 0) {
setTimeout(hideTimer, 500);
}
}
setTimeout(hideTimer, 500);
var $frms;
switch (siteObj.name) {
case 'baidu':
$j('#content_right').hide();
break;
case 'baike':
$j('.topA, .lemmaWgt-promotion-slide, .union-content, .right-ad, .lemmaWgt-promotion-vbaike, .nav-menu').hide();
delay_arr.push('#side_box_unionAd');
break;
case 'zhidao':
$j('.widget-new-graphic, #union-asplu, .jump-top-box').hide();
delay_arr.push('.wgt-daily');
delay_arr.push('.shop-entrance');
delay_arr.push('.cms-slide');
delay_arr.push('.nav-menu');
$frms = $j('iframe');
$frms.hide();
break;
case 'weather':
$j('.right, .hdImgs, .tq_zx, #di_tan, #zu_dui').hide();
//delay_arr.push('.wgt-daily');
$frms = $j('iframe');
$frms.hide();
break;
}
}
})();
My script need to work on different sites, while some has jquery included and some not, so I have to test out. 我的脚本需要在不同的站点上工作,有些包含jquery而有些则没有,所以我必须测试。 The "require" way not work here .
“需要”的方式在这里不起作用 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.