Greasy Fork

Greasy Fork is available in English.

Any jQuery,页面注入jQuery,方便控制台调试代码

write once run anywhere,注入jQuery,方便控制台调试代码

目前为 2020-09-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         Any jQuery,页面注入jQuery,方便控制台调试代码
// @namespace    http://bbs.91wc.net/any-jquery.htm
// @version      0.2
// @description  write once run anywhere,注入jQuery,方便控制台调试代码
// @author       Wilson
// @match        http*://*/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @resource     jquery https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant        unsafeWindow
// @grant        GM_getResourceText
// ==/UserScript==

//main
function main() {
    //Your codes here


    //你也可以像这样向页面中写全局变量或函数
    //_g.a="test a";
    //_g.b = function(){
    //   console.log("test b");
    //}
}

//注入jQuery和全局变量_g
this.$ = this.jQuery = jQuery.noConflict(true);
$("body").append('<script id="_g_script">var _g=window, _w=_g, _jq=(typeof jQuery === "undefined") ? null : jQuery;</script>');
var init = function(){
    //初始化_jq变量
    _g._jq = _g.jQuery;
    _g._jq.version=_g._jq.fn.jquery;

    //call main
    main();
};
if(_g._jq === null){
    setTimeout(function(){
        //动态设置jQuery
        var jq_code = GM_getResourceText('jquery');
        $("body").append('<script id="_g_jquery">'+jq_code+'</script>');

        //初始化_jq变量
        init();
    }, 0);
} else {
    //初始化_jq变量
    init();
}