jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么jQuery在Chrome用户脚本(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!");
});

警报不显示。如果我只是提醒(“Hello world!”);在脚本中,它的作品。

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

解决方法

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

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

我已经写了几个基于Erik Vold’s answer脚本的功能,以帮助我运行文档中的运行功能代码和其他脚本。您可以使用它们将jQuery加载到页面中,然后在全局窗口范围下运行代码

使用示例

// ==UserScript==
// @name           Example from http://stackoverflow.com/q/6825715
// @version        1.2
// @namespace      http://stackoverflow.com/q/6825715
// @description    An example,adding a border to a post on Stack Overflow.
// @include        http://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安装它,如果你相信我不是在欺骗你安装恶意的东西,没有人编辑我的帖子来指向别的东西。重新加载页面,你应该看到我的帖子周围的边框。

功能

load(url,onLoad,onError)

将脚本加载到文档中。可选地,可以为onLoad和onError提供回调。

执行(functionOrCode)

在文档中插入一个或多个代码字符串并执行它。这些函数在插入之前被转换为源代码,所以它们会丢失当前的scope / closure,并在全局窗口范围内运行。

loadAndExecute(url,functionOrCode)

一个捷径这会从url载入脚本,如果成功则插入并执行functionOrCode。

源咖啡脚本

我在CoffeeScript(a little language that compiles to JavaScript)中写了这些。以下是您自己使用CofeeScript的CoffeeScript源代码。对于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,function(){return execute(b)})};

大佬总结

以上是大佬教程为你收集整理的为什么jQuery在Chrome用户脚本(Greasemonkey)中工作?全部内容,希望文章能够帮你解决为什么jQuery在Chrome用户脚本(Greasemonkey)中工作?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: