尝试将someFunction.jQuery映射到"$" [英] trying to map someFunction.jQuery to "$"

查看:103
本文介绍了尝试将someFunction.jQuery映射到"$"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过此人的github找到了一个函数( ),该函数可以在我的脚本中使用,以模仿该功能API对象.

以下是链接中的相关代码:

unsafeWindow = (function() {
    var e1 = document.createElement('p')
    e1.setAttribute('onclick', 'return window;');
    return e1.onclick();
})();

张贴者说您可以使用unsafeWindow.jQuery

格式的功能

现在,我希望能够使用$代替代码中其他位置的jQuery关键字.我尝试从此堆栈溢出问题中学习,以简化并重新编写像这样的代码:

(function($){
    var e1 = document.createElement('p')
    e1.setAttribute('onclick', 'return window;');
    return e1.onclick();
})(jQuery);

但是没有用.我想我可以尝试像$ = unsafeWindow.jQuery这样的东西来映射到$,但是我想尝试以上面看到的格式来做.

解决方案

您将像这样将$映射到unsafeWindow.jQuery:

unsafeWindow    = ( function () {
    var dummyElem   = document.createElement('p');
    dummyElem.setAttribute ('onclick', 'return window;');
    return dummyElem.onclick ();
} ) ();

var $ = unsafeWindow.jQuery;

// Now you can use the page's jQuery. EG:
$("body").append ('<p>Content added by unsafeWindow.jQuery</p>');


但请记住:

  1. 这是一个黑客,它可能会在Chrome 28版附近停止运行.

  2. 由于用户脚本触发的比赛条件,它仍然可能会失败.要解决此问题,请在用户脚本的元数据块中添加// @run-at document-end.

  3. 请勿以这种方式做事!只会造成悲伤,副作用和维护头痛.

    对于 用户脚本: :使用此技术(最佳跨浏览器)此技术(依赖于页面的jQuery,但该示例也显示了如何使用GM_函数).. >

    对于 完整扩展 内容脚本: ,请使用via this person's github) that I might use in my script that mimics the functionality of an API object.

    Here's the relevant code from the link:

    unsafeWindow = (function() {
        var e1 = document.createElement('p')
        e1.setAttribute('onclick', 'return window;');
        return e1.onclick();
    })();
    

    Where the poster says you can use the function in the format unsafeWindow.jQuery

    Now, I want to be able to use $ instead of the jQuery keyword elsewhere in my code. I tried learning from this stack overflow question to simplify it and re-wrote the code like so:

    (function($){
        var e1 = document.createElement('p')
        e1.setAttribute('onclick', 'return window;');
        return e1.onclick();
    })(jQuery);
    

    But it didn't work. I guess I could just try something like $ = unsafeWindow.jQuery in order to map to the $, but I wanted to try to do it in the format seen above.

    解决方案

    You would map $ to unsafeWindow.jQuery like so:

    unsafeWindow    = ( function () {
        var dummyElem   = document.createElement('p');
        dummyElem.setAttribute ('onclick', 'return window;');
        return dummyElem.onclick ();
    } ) ();
    
    var $ = unsafeWindow.jQuery;
    
    // Now you can use the page's jQuery. EG:
    $("body").append ('<p>Content added by unsafeWindow.jQuery</p>');
    


    But keep in mind:

    1. This is a Hack, and it will probably stop working around Chrome version 28.

    2. It may still fail due to a race condition about when userscripts fire. To fix that, add // @run-at document-end to the userscript's metadata block.

    3. Don't do things this way! It will only cause grief, side effects and maintenance headaches.

      For userscripts: use this technique (best cross-browser)  or  this technique (relies on page's jQuery, but the example shows how to use GM_ functions too).

      For full extensions or content scripts:, use this technique (use the manifest.json and keep everything properly sandboxed).

    这篇关于尝试将someFunction.jQuery映射到"$"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆