【问题标题】:Greasemonkey issue with $(document).ready in jQueryjQuery 中 $(document).ready 的 Greasemonkey 问题
【发布时间】:2012-05-24 15:03:09
【问题描述】:

我不明白为什么当我访问 www.reuters.com 时这至少没有提醒我一次。我错过了什么吗?

// ==UserScript==
// @name        test3
// @namespace   test3
// @version     1
// ==/UserScript==

$(document).ready(function () {

    var actualHost = window.location.toString();
    var intendedHost = "www.reuters.com";

    alert("Debug 1 - " + actualHost);

    if (actualHost == intendedHost) {
        alert("Debug 2 - " + actualHost);
    }

});

谢谢。

【问题讨论】:

    标签: jquery greasemonkey loading document-ready


    【解决方案1】:
    1. 要使用 jQuery,您必须加载 jQuery。
    2. $(document).ready() 在大多数 Greasemonkey 脚本中不需要默认为 Greasemonkey fires at the appropriate time。

    所以脚本最简单的版本变成了:

    // ==UserScript==
    // @name     test3
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @grant    GM_addStyle
    // ==/UserScript==
    //- The @grant directive is needed to restore the proper sandbox.
    
    var actualHost = window.location.toString();
    var intendedHost = "www.reuters.com";
    
    alert("Debug 1 - " + actualHost);
    
    if (actualHost == intendedHost) {
        alert("Debug 2 - " + actualHost);
    }
    

    注意:

    1. 尽可能使用@require(几乎总是这样)。

      1. @require 将脚本副本放在本地计算机上,因此您的脚本运行速度更快,并且每次运行都无需依赖外部服务器。
      2. @require 维护沙盒安全/分离,因此您的脚本不会受到来自目标页面的副作用或攻击
      3. @require 让您的脚本在您禁用所有目标页面的 javascript 的情况下仍能正常运行——这是一项非常有价值的技术。
      4. @require 甚至可以移植到 Chrome,如果您使用出色的 Tampermonkey extension。



      其他复杂的添加 jQuery 的方法有多个问题:

      1. 他们是不必要的Security risk。
      2. 它们使您的脚本在每次运行时都不必要地依赖外部服务器!
      3. 它们会减慢您的脚本速度。
      4. 他们使使用 GM 的出色功能like GM_xmlhttpRequest() and GM_setValue(),不可能或更加困难。
      5. 它们将您的脚本与目标页面的 JS 执行的变幻莫测联系在一起。


    2. 始终为您的脚本提供适当的@include, @exclude, and/or @match directives,以便它只在所需的页面上运行。

    3. 考虑使用console.log() 而不是alert()。它对调试的干扰要小得多。

    【讨论】:

      【解决方案2】:

      不起作用是对问题的非常糟糕的描述..

      无论如何,我在这里看到一个问题。试试这个:

      // ==UserScript==
      // @name        test3
      // @namespace   test3
      // @version     1
      // @include     *reuters.com*
      // ==/UserScript==
      
      loadDependancies(function () {
      
          var actualHost = unsafeWindow.location.toString();
          var intendedHost = "www.reuters.com";
      
          alert("Debug 1 - " + actualHost);
      
      
          if (actualHost == intendedHost) {
              alert("Debug 2 - " + actualHost);
          }
      
      
      });
      

      您需要使用@include 指令告诉GM 脚本应该在哪里运行。 您应该使用unsafeWindow 访问该页面的窗口对象

      如果页面上不存在 jquery,您还需要加载它:

      DEBUG = true
      
      function addScript(url){
          var s = document.createElement('script');
          s.src = url;
          s.type = 'text/javascript';
          document.getElementsByTagName('head')[0].appendChild(s);
      }
      
      function log(msg){
        if(DEBUG){
          unsafeWindow.console && unsafeWindow.console.log(msg);
        }
      }
      
      function loadDependancies(boostrapFn) {
      
        addScript('jquery CDN url goes here..');
      
        var check = function(){
          log("waiting for dependancies to load: "+ typeof unsafeWindow.jQuery);
          if(typeof unsafeWindow.jQuery == 'undefined'){
            window.setTimeout(check, 500);
          }    else {
            jQuery = $ = unsafeWindow.jQuery;
            boostrapFn();
          }
        }
        check();
      }
      

      这就是你的新脚本。它将加载 jquery 供您使用

      【讨论】:

      • 感谢您的意见。我的意思是没有任何警报触发,我不知道为什么。
      • 您忘记了包含指令。看看任何其他有效的 GM 脚本
      • 嗯,还是没有提醒我。
      • 啊,你从来没有加载过 jquery。让我给你提供一个装载机
      • 根据我的经验,我发现@require 指令不能很好地工作。
      猜你喜欢
      • 1970-01-01
      • 2013-07-23
      • 2013-07-19
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2014-04-20
      相关资源
      最近更新 更多