如何使用用户脚本将 jQuery 包含在 Google 中?

How To Include jQuery in Google with userscripts?

我尝试在 Google 页面上使用 jQuery。通常,网站包含 jQuery 或至少允许它。 Google,出于某种原因,不允许使用其他来源的脚本。这被证明是一项令人沮丧的任务,并让我最终来到这里。如果有人能帮我把jQuery导入google的网页供javascript使用,我当然很高兴。


我使用 Tampermonkey 作为用户脚本管理器来加载用户脚本。这是我尝试输入 link jQuery:

的一些代码
// ==UserScript==
// @name         jQuery For Chrome (A Cross Browser Example)
// @namespace    jQueryForChromeExample
// @include      *
// @author       Erik Vergobbi Vold & Tyler G. Hicks-Wright
// @description  This userscript is meant to be an example on how to use jQuery in a userscript on Google Chrome.
// ==/UserScript==

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {
  // Note, jQ replaces $ to avoid conflicts.
  alert("There are " + jQ('a').length + " links on this page.");
}

// load jQuery and execute the main function
addJQuery(main);

Google 给我一个错误,说所有 jQuery 函数都不是函数,例如 'btn.click is not a function'.


谢谢大家的尝试。

没关系,现在已经完成了 dsf asfd asdfasdf asfd asdfasdf asdf asdf asfd

你需要一个//@require然后证明link到jquery。比如google hosted one or the jquery one。 它应该看起来像这样。

// @require   https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js

注意:确保缩进正确,以便与代码的其余部分匹配。

// ==UserScript==
// @name         Example
// @namespace    http://tampermonkey.net/
// @version      Beta 1.4.6
// @author       LightLord
// @match        https://whosebug.com
// @icon         https://cdn.sstatic.net/Sites/Whosebug/Img/apple-touch-icon.png?v=c78bd457575a
// @description  example
// @require  https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        GM_addStyle
// ==/UserScript==


//above is WRONG

下面是它应该看起来的样子:)

// ==UserScript==
// @name         Example
// @namespace    http://tampermonkey.net/
// @version      Beta 1.4.6
// @author       LightLord
// @match        https://whosebug.com
// @icon         https://cdn.sstatic.net/Sites/Whosebug/Img/apple-touch-icon.png?v=c78bd457575a
// @description  example
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        GM_addStyle
// ==/UserScript==


//above is RIGHT