为什么jQuery不能在Chrome用户脚本(Greasemonkey)中工作? [英] Why doesn't jQuery work in Chrome user scripts (Greasemonkey)?
问题描述
我无法获得此用户脚本在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?
这个问题在在GreaseMonkey中包含Jquery谷歌浏览器下的脚本。这是我对该问题的回答: 我已经根据 Erik Vold的答案中的脚本编写了几个函数来帮助运行我在文档中运行函数,代码和其他脚本。您可以使用它们将jQuery加载到页面中,然后在全局 $ b $ b
>
@require
, @resource
, unsafeWindow
, GM_registerMenuCommand
, GM_setValue
或 GM_getValue
GM_xmlhttpRequest
只是同源。
窗口
范围下运行代码。
示例用法
// == UserScript ==
// @name https://stackoverflow.com/中的示例q / 6825715
// @version 1.2
// @namespace https://stackoverflow.com/q/6825715
// @description一个例子,在Stack上为帖子添加边框溢出。
// @include https://stackoverflow.com/questions/2588513/*
// == / UserScript ==
var load,execute,loadAndExecute; load =函数(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 ==函数?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- (border,.5em solid black);
});
您可以点击这里安装它,如果你相信我并不想诱骗你安装恶意程序,并且没有人编辑我的程序张贴指向别的东西。重新加载页面,你应该看到我的文章边框。
函数
load(url,onLoad,onError)
将脚本加载到 url
文件。可选地,可为 onLoad
和 onError
提供回调。
< h3>
execute(functionOrCode)
将一个函数或一串代码插入到文档中并执行它。这些函数在被插入之前被转换为源代码,所以它们会丢失当前的范围/闭包,并在全局窗口
范围下运行。
loadAndExecute(url,functionOrCode)
这会从 url
加载脚本,如果成功则插入并执行 functionOrCode
。
代码
源代码CoffeeScript
我在CoffeeScript(编译成JavaScript的小语言)。这里是您使用CofeeScript的CoffeeScript源代码。对于JavaScript用户,编译和缩小的代码包含在下面。
load =(url,onLoad,onError) - >
e = document.createElementscript
e.setAttributesrc,url
如果onLoad?那么e.addEventListener加载,如果onError onLoad
?然后e.addEventListenererror,onError
document.body.appendChild e
return e
execute =(functionOrCode) - >
如果typeof functionOrCode是function
code =(#{functionOrCode})();
else
code = functionOrCode
e = document.createElementscript
e.textContent = code
document.body.appendChild e
return e
loadAndExecute =(url,functionOrCode) - >
加载网址, - >执行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( 负载,b),C = NULL&安培;!&安培; d.addEventListener( 错误,c)中,document.body.appendChild(d) ; return d},execute = function(a){var b,c; typeof a ==function?b =(+ a +)();:b = a,c = document.createElement( (),c.textContent = b,document.body.appendChild(c); return c},loadAndExecute = function(a,b){return load(a,function(){return execute(b)})};
Possible Duplicate:
How can I use jQuery in Greasemonkey scripts in Google Chrome?
I'm unable to get this user script to work in 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!");
});
The alert doesn't show up. If I just put alert("Hello world!");
in the script, it works.
How can I use jQuery in Chrome user scripts?
The design document for Chrome's user script's implementation mentions these known issues:
- Chromium does not support
@require
,@resource
,unsafeWindow
,GM_registerMenuCommand
,GM_setValue
, orGM_getValue
.GM_xmlhttpRequest
is same-origin only.
This is addressed in the question Include Jquery inside GreaseMonkey script under Google Chrome. Here is my answer from that question:
I have written a few functions based on the script from Erik Vold's answer to help run me run functions, code and other scripts in a document. You can use them to load jQuery into the page, and then run code under the global window
scope.
Example Usage
// ==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");
});
You can click here to install it, if you trust that I'm not trying to trick you into installing something malicious, and that nobody has edited my post to point to something else. Reload the page and you should see a border around my post.
Functions
load(url, onLoad, onError)
Loads the script at url
into the document. Optionally, callbacks may be provided for onLoad
and onError
.
execute(functionOrCode)
Inserts a function or string of code into the document and executes it. The functions are converted to source code before being inserted, so they lose their current scope/closures and are run underneath the global window
scope.
loadAndExecute(url, functionOrCode)
A shortcut; this loads a script from url
, then inserts and executes functionOrCode
if successful.
Code
Source CoffeeScript
I wrote these in CoffeeScript (a little language that compiles to JavaScript). Here is the CoffeeScript source for use of you are using CofeeScript yourself. For JavaScript users the compiled and minified code is included below.
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
Compiled and Minified JavaScript (468 characters)
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不能在Chrome用户脚本(Greasemonkey)中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!