1

在 chrome 中,我的用户脚本导致错误

ReferenceError: jQuery is not defined

但是@require 行包含jquery。至少我知道它与greasemonkey(铬不支持吗?)

如果我删除一个函数调用的函数(这使得 $ = jQuery)我得到错误 $ 未定义。在 firefox/greasemonkey 中运行良好

// ==UserScript==
// @name        Detect Duplicate IDs
// @namespace   jkbfvsdjkzsvfshefsdvh
// @description Alerts you when more than one ID is on a page
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include     *
// @version     1
// @grant
// ==/UserScript==

//using localhost in @include doesn't seem to work
if(location.hostname!='localhost')
    return;

//Ignore above for console.
(function($){
$(function() {
var checkDupeIDs = function () {
    var dupes = [];
    var ids = [];
    $('[id]').each(function(i,e){ids.push(e.id)});
    var len = ids.length;
    for(i=0; i<len; ++i) {
        if(dupes.indexOf(ids[i])!=-1)
            continue;
        for(n=i+1; n<len; ++n) {
            if (ids[n]==ids[i]) {               
                dupes.push(ids[n]);
                break;
            }
        }
    }
    if (dupes.length!=0) { 
        for(i=0; i<dupes.length; ++i)
            console.warn('Multiple IDs #' + dupes[i]);
        alert(dupes.join('\n')); 
    }
}


var jHtml = $.html;
$.html = function () {
    checkDupeIDs();
    return jHtml.call(this, arguments);
}

checkDupeIDs();

})
})(jQuery);
4

2 回答 2

2

开箱即用,Chrome 不能很好地支持 Greasemonkey 脚本(Out-of-date tableOut-of-date page)。例如,该@require指令不起作用。

要享受与 Firefox Greasemonkey 脚本几乎完全的兼容性,请安装Tampermonkey Extension for Chrome。您的脚本应该可以在 Tampermonkey 中运行。

此外,为避免 Firefox 出现问题,请勿@grant留空。使用@grant  GM_addStyle,如果没有别的。

于 2013-04-10T13:59:18.577 回答
0

检查两个最重要的事情:

  1. 你已经在你的项目中包含了 jQuery
  2. 您在使用 jQuery 的任何其他 js 文件之前包含了 jQuery 文件
于 2013-04-10T13:28:54.640 回答