Jump to content

User:קיפודנחש/common.js/personalScript.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <source lang="javascript">
// script for script developers: allows you to easilly set and clear personal script to be included
// on startup.
mw.loader.using( 'mediawiki.util' ) 
.done(function() {
        var lsKey = 'personalScript';
 
        function getTop() {return ((lsKey in localStorage) && localStorage[lsKey].split('\n')[0]) || ''}
 
        function allScripts() {
                return (localStorage[lsKey] || '').split('\n');
        }
 
        function setScript(script) {
                var ls = localStorage[lsKey];
                if (lsKey in localStorage)
                {
                        var 
                                lsa = allScripts(),
                                ind = $.inArray(script, lsa);
                        if (ind < 0)
                                ind = 10;
                        if (! lsa[0])
                                lsa.shift(); // if first is empty, remove it.
                        lsa.splice(ind, 1); // remove current if exists, or last if length >= 10
                        lsa.unshift(script);
                        localStorage[lsKey] = lsa.join('\n');
                } else
                        localStorage[lsKey] = script;
                loadScript(script);
        }
 
        function loadScript(s) {
                if (!s)
                        return;
                try {
                        var isCss = /\.css$/i.test(s);
                        if (/\/\//.test(s)) 
                                mw.loader.load(s, isCss ? 'text/css' : 'text/javascript');
                        else 
                                return isCss ? importStylesheet(s) : importScript(s);
                } catch(e) {}
        }
 
        function mySetSpecialScript() {
                mw.loader.using('jquery.ui', function() {
                        var 
                                inputBox = $('<input>').val(getTop()).css({width: '22em', direction: 'ltr'}),
                                selector = $('<select>')
                                        .css({direction: 'ltr'})
                                        .change(function() {inputBox.val(this.value)});
                        $.each(allScripts(), function(ind, item) {
                                selector.append($('<option>', {value: item, text: item.substr(0, 80) + (item.length > 80 ? '...':'')}));
                        });
                        $('<div>')
                                .css({direction: 'ltr'})
                                .dialog(
                                        {title: 'Personal script',
                                        modal: true,
                                        position: [60, 60],
                                        buttons: [
                                                {text: 'OK', click: function() {
                                                        setScript(inputBox.val());
                                                        $(this).dialog('close');
                                                }},
                                                {text: 'Cancel', click: function() {
                                                        setScript('');
                                                        $(this).dialog('close');
                                                }}]
                                })
                                .append(inputBox)
                                .append($('<p>'))
                                .append(selector)
                                .dialog('option', 'height', 'auto');
                });
        }
 
        loadScript(getTop());
        mw.util.addPortletLink('p-cactions', '#noAnchor', 'Personal script').onclick = mySetSpecialScript;
 
        if ($.inArray( mw.config.get('wgAction'), ['edit', 'submit']) + 1)
                importScript('User:קיפודנחש/TemplateParamWizard.js');
});
// </source>