gpt4 book ai didi

javascript - jquery/userscript 未定义?

转载 作者:行者123 更新时间:2023-11-30 18:05:29 26 4
gpt4 key购买 nike

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

ReferenceError: jQuery is not defined

但是@require 这行包含了jquery。至少我知道它适用于 greasemonkey(chrome 不支持吗?)

如果我删除一个函数调用的函数(这使得 $ = jQuery)我得到错误 $ is not defined。在 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);

最佳答案

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

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

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

关于javascript - jquery/userscript 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15927113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com