27

我正在尝试编写一个 Greasemonkey 脚本,并且想使用 jQuery 库来执行此操作,但我不太确定如何从网址中包含 jQuery 以开始滚动。

我如何将 jQuery(来自 Google 的网络服务器)包含到greasemonkey 脚本中,这样我就可以去:

$(document).ready(function(){
  // Greasemonkey stuff here
});

我宁愿从这个来源得到它:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>

更新:感谢您的帮助,答案非常有用。但是,我确实使用了我的 GoogleFu 并遇到了这个解决方案:http : //joanpiedra.com/jquery/greasemonkey/

像魅力一样工作..只需将源更新为谷歌托管版本的 jQuery 即可完成。

4

4 回答 4

49

在最近版本的greasemonkey 中推荐的方法是使用@require 注释标签。

例如

// ==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==

但是...请注意,jQuery 1.4.1 和 1.4.2 与此方法不兼容

感谢 Paul Tarjan 指出这一点。请参阅jQuery 论坛主题

还要注意这些 @require 语义

在安装用户脚本时,Greasemonkey 将下载并保存远程文件的本地缓存副本,该副本几乎可以立即读取。缓存的副本与您安装的用户脚本保存在同一文件夹中。不监视远程文件的更改。

请注意,在编写此答案时,此 @require 标记仅在安装时读取。如果您编辑现有用户脚本以添加此标签,它将被忽略。您需要卸载并重新安装您的用户脚本以获取更改。

于 2009-04-24T10:21:06.933 回答
12

这里

// ==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"); 
                } 
            } 
        }); 
} 

它运行良好,但您可能希望限制它运行的站点或在您自己的站点上托管 jQuery js 文件,以免窃取它们的带宽。

于 2009-04-23T01:32:21.827 回答
6

您可以尝试动态创建脚本元素:

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);

您可能需要在脚本加载时延迟一段时间(setTimeout?)

于 2009-04-23T00:25:59.740 回答
2

根据克里斯的回答,我会根据自己的需要进行调整,如下所示。

// ==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;
        }
    }

})();

我的脚本需要在不同的站点上运行,有些包含 jquery,有些不包含,所以我必须测试一下。“要求”方式在这里不起作用

于 2018-01-26T02:00:07.953 回答