- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
, orGM_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
处的脚本加载到文档中。 (可选)可以为 onLoad
和 onError
提供回调。
执行(functionOrCode)
将函数或代码字符串插入文档并执行它。这些函数在插入之前会转换为源代码,因此它们会丢失当前的作用域/闭包,并在全局 window
作用域下运行。
loadAndExecute(url, functionOrCode)
快捷方式;这将从 url
加载脚本,然后插入并执行 functionOrCode
(如果成功)。
我用 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
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/
这个问题在这里已经有了答案: How to call Greasemonkey's GM_ functions from code that must run in the target page s
是否可以使用 Greasemonkey 脚本跨域存储数据?我想允许从使用相同 Greasemonkey 脚本的多个网站访问 Javascript 对象。 最佳答案 是的,这就是 GM_setvalue
我有一个简单的 Greasemonkey 脚本: // ==UserScript== // @name hello // @namespace http://www.webmonkey.com //
我想写一个非常简单的 greasemonkey 脚本,因为我讨厌“你确定吗?”我经常使用的网站上的 javascript 确认。我只是将它用于个人用途,不会发布它或任何东西。经过一番谷歌搜索后,我找到
这是一个有点奇怪的具体问题。 我正在编写一个可以跨十个域运行的 Greasemonkey 脚本。这些网站都具有相同的结构,但每个网站的域名不同。例如,脚本将运行: http://first-domai
我为一个域制作了一个greasemonkey脚本,现在如何让它只运行一次?就像每次访问域时它都会启动一样,我不希望这样。如何使其仅运行一次,然后删除自身或使其处于非事件状态? 谢谢。 最佳答案 如果您
我发现了很多类似的问题,但没有一个是平等的,也没有正确的解决方案。这是一个非常奇怪的问题。 我有一个简单的 Greasemonkey 脚本来测试这个问题: // ==UserScript== // @
我正在写一个 Greasemonkey 脚本,如何实现自动更新? 可以将脚本放入 GitHub 存储库并设置 @version数字? 然后......是一些自动的方式如何做到这一点?或者我必须手动检查
我现在正在研究用户脚本。我知道 Opera 与 Greasemonkey 脚本的许多方面兼容,但与其他方面不兼容。 GM functions emulation script在 Opera 上将需要,
最近在写一个用户脚本时,我发现页面上下文中的变量是可用的。 console.log(window) 在 Tampermonkey 和 Greasemonkey 中都没有导致错误。 我很困惑。全局变量不
看完后Greasemonkey recommends users to install Tampermonkey or Violentmonkey . 我已经安装了 Tampermonkey,现在我正
我在一堆 div 和一个表单中包含了一些输入值。我对 js 的了解有限,想在页面加载时使用油脂猴来设置值。 下面显示了获取值的 div 和表单的顺序。
有没有办法使用用户脚本将一些设置保存到不是 cookie 的本地计算机? 如果设置不是全局的,则很难制作用于多个域的用户脚本。 来自评论: "I am using scriptish " 。 最佳答案
我有一个带有以下元块的油脂猴脚本- // ==UserScript== // @name TDF Improved Dark Skin // @namespace TDF // @incl
我有用于Firefox的Greasemonkey脚本。该脚本包括此元块和一些代码行。 我想在服务器上更新脚本,然后自动更新浏览器的脚本。 requireSecureUpdates选项已关闭。 我究竟做
我正在按照说明将 Greasemonkey 脚本传输到 Tampermonkey:How to Transfer All Greasemonkey userscripts to Tampermonke
This question already has an answer here: My very simple Greasemonkey script is not running? (1个答案)
我需要在公司内部网站上托管用户脚本。我如何构建 href这样 Greasemonkey 会在点击链接时安装用户脚本吗? 我尝试了一个简单的 Install Userscript但是 Chrome 和
例如,在远程网页中,有如下代码片段: function foo(){ this.bar = 0; } 在我的油脂猴子脚本中,我想创建此类的对象: var _foo= unsafeWindow['fo
我想在文档中选择文本后调用函数。以下代码无效 var showSelWin = document.getElementById('innerwindow'); var txt = ' '; if (d
我是一名优秀的程序员,十分优秀!