gpt4 book ai didi

jquery - 为什么 jQuery 在 Chrome 用户脚本 (Greasemonkey) 中不起作用?

转载 作者:行者123 更新时间:2023-12-03 21:52:52 26 4
gpt4 key购买 nike

Possible Duplicate:
How can I use jQuery in Greasemonkey scripts in Google Chrome?

我无法让此用户脚本在 Google Chrome 中运行。

// ==UserScript==
// @name voip
// @namespace 1
// @description voip
// @include *
// @require http://jquery.com/src/jquery-latest.js
// ==/UserScript==

$(document).ready(function() {
alert("Hello world!");
});

警报未显示。如果我只是输入 alert("Hello world!");在脚本中,它有效。

如何在 Chrome 用户脚本中使用 jQuery?

最佳答案

design document for Chrome's user script's implementation提到这些已知问题:

  • Chromium does not support @require, @resource, unsafeWindow, GM_registerMenuCommand, GM_setValue, or GM_getValue.
  • GM_xmlhttpRequest is same-origin only.

这在问题 Include Jquery inside GreaseMonkey script under Google Chrome 中得到解决。 。这是my answer from that question :

<小时/>

我根据Erik Vold's answer中的脚本编写了一些函数帮助我运行文档中的函数、代码和其他脚本。您可以使用它们将 jQuery 加载到页面中,然后在全局 window 范围下运行代码。

用法示例

// ==UserScript==
// @name Example from https://stackoverflow.com/q/6825715
// @version 1.2
// @namespace https://stackoverflow.com/q/6825715
// @description An example, adding a border to a post on Stack Overflow.
// @include https://stackoverflow.com/questions/2588513/*
// ==/UserScript==

var load,execute,loadAndExecute;load=function(a,b,c){var d;d=document.createElement("script"),d.setAttribute("src",a),b!=null&&d.addEventListener("load",b),c!=null&&d.addEventListener("error",c),document.body.appendChild(d);return d},execute=function(a){var b,c;typeof a=="function"?b="("+a+")();":b=a,c=document.createElement("script"),c.textContent=b,document.body.appendChild(c);return c},loadAndExecute=function(a,b){return load(a,function(){return execute(b)})};

loadAndExecute("//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() {
$("#answer-6825715").css("border", ".5em solid black");
});

您可以click here安装它,如果你相信我不是试图欺骗你安装恶意的东西,并且没有人编辑我的帖子来指向其他东西。重新加载页面,您应该会看到我的帖子周围有一个边框。

函数

加载(url,onLoad,onError)

url 处的脚本加载到文档中。 (可选)可以为 onLoadonError 提供回调。

执行(functionOrCode)

将函数或代码字符串插入文档并执行它。这些函数在插入之前会转换为源代码,因此它们会丢失当前的作用域/闭包,并在全局 window 作用域下运行。

loadAndExecute(url, functionOrCode)

快捷方式;这将从 url 加载脚本,然后插入并执行 functionOrCode(如果成功)。

代码

源 CoffeeScript

我用 CoffeeScript ( a little language that compiles to JavaScript ) 编写了这些内容。这是 CoffeeScript 源代码,供您自己使用 CofeeScript 使用。对于 JavaScript 用户,下面包含编译和缩小的代码。

load = (url, onLoad, onError) ->
e = document.createElement "script"
e.setAttribute "src", url

if onLoad? then e.addEventListener "load", onLoad
if onError? then e.addEventListener "error", onError

document.body.appendChild e

return e

execute = (functionOrCode) ->
if typeof functionOrCode is "function"
code = "(#{functionOrCode})();"
else
code = functionOrCode

e = document.createElement "script"
e.textContent = code

document.body.appendChild e

return e

loadAndExecute = (url, functionOrCode) ->
load url, -> execute functionOrCode

编译和缩小的 JavaScript(468 个字符)

var load,execute,loadAndExecute;load=function(a,b,c){var d;d=document.createElement("script"),d.setAttribute("src",a),b!=null&&d.addEventListener("load",b),c!=null&&d.addEventListener("error",c),document.body.appendChild(d);return d},execute=function(a){var b,c;typeof a=="function"?b="("+a+")();":b=a,c=document.createElement("script"),c.textContent=b,document.body.appendChild(c);return c},loadAndExecute=function(a,b){return load(a,function(){return execute(b)})};

关于jquery - 为什么 jQuery 在 Chrome 用户脚本 (Greasemonkey) 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2588513/

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