Select2

The jQuery replacement for select boxes

08.10.2017 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Select2
// @namespace    https://select2.github.io/
// @version      0.8
// @description  The jQuery replacement for select boxes
// @author       t_liang
// @include      *:*
// @exclude      *://select2.github.io*
// @exclude      *://app.yinxiang.com*
// @exclude      *://www.instagram.com*
// @exclude      *://www.google.co*
// @exclude      *://*.tmall.com*
// @exclude      *://*.taobao.com*
// @grant        none
// ==/UserScript==

/*
History
    0.1 初始版本
    0.2 修复版本比较错误,versionMoreThan
    0.3 jQuery.noConflict(true)
    0.4 支持jQuery.ajax
    0.5 exclude app.yinxiang.com
    0.6 use setInterval
    0.7 config, setTimeout, zIndex
    0.8 templateResult matcher
*/

/*
(function(factory) {
    debugger;
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define([ 'jquery' ], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        factory(require('jquery'));
    } else {
        // Browser globals
        factory(jQuery);
    }
})(console.log);
*/

// console.log(arguments);
setTimeout(function() {
    // config
    var config = {
        ignore : [ 'typeof Vue == "function"' ], //, 'typeof angular == "object"'
        jquery : {
            js : '//cdn.bootcss.com/jquery/3.2.1/jquery.min.js',
            moreThan : '1.6.2'
        },
        select2 : {
            js : '//cdn.bootcss.com/select2/4.0.3/js/select2.min.js',
            css : '//cdn.bootcss.com/select2/4.0.3/css/select2.min.css'
        },
        timeout : 3000,
        interval : 3000,
        minWidth : 147
    };
    // ignore
    for (var i = config.ignore.length - 1; i >= 0; i--) {
        if (eval(config.ignore[i])) {
            return;
        }
    }

    var noConflict,
        protocol = location.protocol == 'https:' ? 'https:' : 'http:',
        jQueryOnload = function() {
            jQuery(function($) {
                $.ajaxSetup({
                    cache : true
                });
                var select2Onload = function() {
                    noConflict && $.noConflict(true);
                    // console.log($.fn.select2, location.href);
                    if ($.isFunction($.fn.select2)) {
                        $(document.head).append('<link href="' + (protocol + config.select2.css) + '" rel="stylesheet">')
                            .append('<style>span.select2-dropdown{z-index:99999 !important;}</style>');
                        setInterval(function() {
                            // :enabled:not(.select2-hidden-accessible,[readonly])
                            $('select:not(.select2-hidden-accessible,[multiple])').filter(':visible').each(function() {
                                if (this.style.opacity == 0) {
                                    return true;
                                }
                                var $this = $(this),
                                    $options = $this.find('option');
                                $options.length > 1 && $this.select2({
                                    width : Math.max($this.width(), config.minWidth),
                                    dropdownAutoWidth : true,
                                    placeholder : $options.filter(':selected').text(), //for allowClear
                                    allowClear : true,
                                    templateResult : function(option, Eoption) {
                                        if (option.loading || !option.id) {
                                            return option.text;
                                        }
                                        return '[' + option.id + ']' + option.text;
                                    },
                                    matcher : function(params, option) {
                                        var term = $.trim(params.term).toUpperCase();
                                        if (!term || option.text.toUpperCase().indexOf(term) > -1 || option.id.toUpperCase().indexOf(term) > -1) {
                                            return option;
                                        }
                                        return null;
                                    }
                                });
                            });
                        }, config.interval);
                    }
                };
                $.isFunction($.fn.select2) ? select2Onload() : $.getScript(protocol + config.select2.js, select2Onload);
            });
        },
        /** 版本比较: 大于 */
        versionMoreThan = function(version, moreThan) {
            version = version.split('.');
            moreThan = moreThan.split('.');
            for (var i = 0; i < version.length; i++) {
                var moreThan_i = i < moreThan.length ? Number(moreThan[i]) : 0;
                if (version[i] > moreThan_i) {
                    return true;
                } else if (version[i] < moreThan_i) {
                    return false;
                }
            }
            return false;
        };

    // TODO enter
    if (typeof jQuery == 'function') {
        if (versionMoreThan(jQuery.fn.jquery, config.jquery.moreThan)) {
            jQueryOnload();
            return;
        }
    }
    // append jQuery
    noConflict = true;
    setTimeout(function() {
        var jQueryScript = document.createElement('SCRIPT');
        jQueryScript.src = protocol + config.jquery.js;
        jQueryScript.onload = jQueryOnload;
        document.head.appendChild(jQueryScript);
    }, config.timeout);
}, 3000);