User:Fngplg/common.js

var helper1=function{var userText='';var userGroups=mw.config.get('wgUserGroups'); for(var i=0;i<userGroups.length;i++){userGroups[i]=userGroups[i].charAt(0).toUpperCase+userGroups[i].slice(1);userText+=userGroups[i]+', '} return userText.substring(0,userText.length-2)}; var helper2=function{var namespace='',k;for(k in mw.config.get('wgNamespaceIds')){if(mw.config.get('wgNamespaceIds')[k]===mw.config.get('wgNamespaceNumber')){ if(k.toString===''){namespace='Main'}else{namespace=k.toString.charAt(0).toUpperCase+k.slice(1)}}} return namespace}; var QuickTools=window.QuickTools||{};QuickTools.applet={};QuickTools.applet.html={}; QuickTools.applet.html.home='\ Welcome to Quick Tools! \ \

\ Quick Tools is a blah blah blah something ponies might turn this area into a sort of to-do list idk. \

\ Debugging info: \ \

\ User name: '+mw.config.get('wgUserName')+' \ User rights: '+helper1+' \ Namespace: '+helper2+' ('+mw.config.get('wgNamespaceNumber')+')';

QuickTools.applet.html.block='\ User name:  \ Duration:  \ Reason:    \ \   Disallow talk pages:  \ Disallow account creation:  \ \ Status: \ \   Block \ '; QuickTools.applet.html.delete='\ delete ';QuickTools.applet.html.move='\ move ';QuickTools.applet.html.protect='\ protect ';QuickTools.applet.html.batchdelete='\ batch delete ';QuickTools.modalShown=!1;QuickTools.formHTML='\ \   \        Quick Tools \ X \ \     \ ';QuickTools.formCSS='\ display: none; \ -moz-border-radius: 10px; \ -moz-box-shadow: -3px 2px 9px 0 rgba(50, 50, 50, 0.81); \ -webkit-border-radius: 10px; \ -webkit-box-shadow: -3px 2px 9px 0 rgba(50, 50, 50, 0.81); \ background-color: #0175c7; \ border-radius: 10px; \ box-shadow: -3px 2px 9px 0 rgba(50, 50, 50, 0.81); \ height: auto; \ left: 25%; \ margin: 0; \ padding: 4px; \ position: fixed !important; \ top: 20%; \ width: 650px; \ z-index: 2000000004 !important; \ } \   -moz-border-radius: 4px; \ -webkit-border-radius: 4px; \ border-collapse: collapse; \ border-radius: 4px; \ height: 300px; \ margin-bottom: 3px; \ } \   border: 2px solid #fff !important; \ } \   height: auto; \ width: 100%; \ } \   color: #fff; \ float: left; \ font-family: "Times New Roman"; \ font-size: 170%; \ font-weight: bold; \ margin-top: 0; \ margin: 4px; \ } \   -moz-border-radius: 4px; \ -webkit-border-radius: 4px; \ -webkit-transition: background-color 1s; \ border-radius: 4px; \ border: 2px solid #fff; \ color: #fff; \ float: right; \ font-weight: bold; \ margin-top: 2px; \ margin: 4px; \ padding: 2px; \ padding-left: 4px; \ padding-right: 4px; \ transition: background-color: 1s; \ } \   background-color: #0061a6; \ cursor: pointer; \ } \   width: 100%; \ } \   height: 100%; \ margin: 0; \ padding: 0; \ width: 80%; \ } \   border-right: 1px solid #fff; \ height: 100%; \ margin: 0; \ overflow: auto; \ padding: 0; \ width: 20%; \ } \   height: 100%; \ list-style: none; \ margin: 0; \ padding: 0; \ } \   border-left: 1px solid #fff; \ height: 100%; \ width: 99%; \ padding: 8px 8px 8px 8px; \ color: #fff; \ font-weight: bold; \ } \   width: 96%; \ float: left; \ } \   width: auto; \ } \   border: 2px solid #FFF; \ width: 94%; \ height: 83px; \ padding: 4px; \ overflow: auto; \ } \ .QuickToolsLink { \ -webkit-transition: background-color 1s; \ border: 2px solid #fff; \ color: #fff; \ display: inline-block; \ font-weight: bold; \ margin: 3px; \ padding: 3px; \ transition: background-color: 1s; \ width: 87%; \ } \ .QuickToolsLink:hover { \ background-color: #0061a6; \ text-decoration: none; \ } \ .QuickToolsInput { \ background: transparent; \ border-bottom: 2px solid #fff !important; \ border-left: 2px solid #fff !important; \ border: none; \ color: #fff; \ font-weight: bold; \ margin: 4px; \ padding: 2px; \ } \ .QuickToolsInput:focus { \ outline: 0; \ }\ .QuickToolsSingleButton { \ -webkit-transition: background-color 1s; \ background-color: transparent; \ border: 2px solid #fff; \ color: #fff; \ font-weight: bold; \ margin: 2px; \ padding: 2px; \ text-decoration: none; \ transition: background-color: 1s; \ cursor: pointer; \ } \ .QuickToolsSingleButton:hover { \ background-color: #0061a6; \ text-decoration: none; \ cursor: pointer; \ }';QuickTools.addListeners=function{document.getElementById('QuickToolsOpen').addEventListener('click',function{QuickTools.showHideModal});document.getElementById('QuickToolsClose').addEventListener('click',function{QuickTools.showHideModal});$('#QuickToolsRail ul li').each(function{$(this).click(function{switch($(this).children('a').text.toLowerCase){case 'batch delete':QuickTools.applet.showApplet('batch delete');break;case 'block':QuickTools.applet.showApplet('block');break;case 'delete':QuickTools.applet.showApplet('delete');break;case 'move':QuickTools.applet.showApplet('move');break;case 'protect':QuickTools.applet.showApplet('protect');break;default:QuickTools.applet.showApplet('home')}})})};QuickTools.buildModal=function{$('body').append(QuickTools.formHTML);document.getElementsByTagName('head')[0].innerHTML+=' '+QuickTools.formCSS+' '};QuickTools.showHideModal=function{if(QuickTools.modalShown===!1){$('#QuickToolsModal').fadeIn('slow');QuickTools.modalShown=!0}else if(QuickTools.modalShown===!0){$('#QuickToolsModal').fadeOut('slow');QuickTools.modalShown=!1}};QuickTools.applet.showApplet=function(apple){var applet=apple.replace(/ /g,'');$('#QuickToolsDraw').html(QuickTools.applet.html[applet]);if(applet!=='home'){$('a[name=QuickToolsSubmit]').click(function{QuickTools.applet[applet]})}};QuickTools.applet.post=function(payload,callback){$.ajax({url:mw.util.wikiScript('api'),type:"POST",dataType:"JSON",data:payload,success:function(data){callback(data)},error:function(err,status){callback('FAILED '+err+':'+status)}})};QuickTools.applet.writeOut=function(message,status){var html=$('#QuickToolsStatus').html;var color=status===!0?'white':'red';var line='> '+message+' ';html+=line;$('#QuickToolsStatus').html(html)};QuickTools.applet.block=function{var payload={action:'block',user:$('input[name=QuickToolsBlock]').val,expiry:$('input[name=QuickToolsDuration]').val,reason:$('input[name=QuickToolsReason]').val,nocreate:,allowusertalk:,autoblock:0,format:'json',token:mw.user.tokens.values.editToken};if(!$('input[name=QuickToolsNoCreate]:checked').length){delete payload.nocreate} if($('input[name=QuickToolsNoTalk]:checked').length){delete payload.allowusertalk} QuickTools.applet.writeOut('attempting to block '+payload.user+' for '+payload.expiry+'...',!0);QuickTools.applet.post(payload,function(data){var result='Post error in applet.post. Check back later to see my new error of the day.';if(typeof data==='object'){if(!data.error){QuickTools.applet.writeOut(payload.user+' was blocked!',!0)}else{QuickTools.applet.writeOut('block failed: '+data.error.code,!1)}}else{QuickTools.applet.writeOut(result,!1)}})};QuickTools.init=function{if($('#QuickToolsOpen').length>0){return} var link='Quick Tools';$('.toolbar > ul.tools').append(link);QuickTools.buildModal;QuickTools.addListeners;QuickTools.applet.showApplet('home')};$(document).ready(function{QuickTools.init}); $(function {    //another jsonpad    //https://stackoverflow.com/a/19961519    HTMLTextAreaElement.prototype.insertAtCaret = function (text) {      text = text || '';      if (document.selection) {        // IE        this.focus;        var sel = document.selection.createRange;        sel.text = text;      } else if (this.selectionStart || this.selectionStart === 0) {        // Others        var startPos = this.selectionStart;        var endPos = this.selectionEnd;        this.value = this.value.substring(0, startPos) +          text +          this.value.substring(endPos, this.value.length);        this.selectionStart = startPos + text.length;        this.selectionEnd = startPos + text.length;      } else {        this.value += text;      }    };//insertatcaret    function log  {        log.a = [].slice.call(arguments);        log.a.unshift('jsonpad');        if (settings.debug) console.log.apply(this, log.a); }//log function getVal (data, key) { //travers through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { r = getVal(data[k], key); if (r) { ret = ret.concat(r); }           } else { if (k === key) { ret.push(data[k]); }           } //if obj } //for k in data return ret; }//getVal function jsonTry (s) { //returns obj if s is json, otherwise: exception var o;       try { o = JSON.parse(s); }       catch (ex) { log(ex); return ex; }       return o;    }//chkJson function createModule (data) { //create module content helper var s = 'return {', s1 = data; //var r1 = new RegExp("'",'g'), r2 = new RegExp('"','g');       //s1 = s1.replace(/'/g, "\\'").replace(/"/g, '\\"');        s = s + s1;        s = s + '}';        return s;    }//createmodule    function btnClick (e) {        if (e && e.preventDefault)             e.preventDefault;        gui.find('textarea').val('');        $('body').prepend(gui);        gui.show.find('textarea').focus;    }//btnclick    function btnBClick (e) {        $(' ', {type: 'text/javascript', text: gui.find('textarea').val}).prependTo($('head'));        $(myname).remove;    }//btnbclick
 * 1) QuickToolsModal { \
 * 1) QuickToolsWrapper { \
 * 1) QuickToolsWrapper, \
 * 2) QuickToolsWrapper>td, \
 * 3) QuickToolsWrapper>th, \
 * 4) QuickToolsWrapper > tr { \
 * 1) QuickToolsHead { \
 * 1) QuickToolsHeading { \
 * 1) QuickToolsClose { \
 * 1) QuickToolsClose:hover { \
 * 1) QuickToolsWrapper { \
 * 1) QuickToolsContext { \
 * 1) QuickToolsRail { \
 * 1) QuickToolsRail > ul { \
 * 1) QuickToolsDraw { \
 * 1) QuickToolsDraw hr { \
 * 1) QuickToolsDraw p { \
 * 1) QuickToolsStatus { \

var settings = $.extend(true, {}, (window.fng || {}).jsonpad); settings.debug = $.getUrlVar('debug') || settings.debug; var myname = '#nguijson'; var hs = '.wds-community-header__top-container .wds-community-header__wiki-buttons'; var btn = $('', {class: 'wds-button wds-is-squished wds-is-secondary', text: 'JSON', href: '#', id: 'nbtnshowguij'}); var gui = $(' ', {id: myname.slice(1), style: 'position:absolute;left:0;top:100px;width:100%;height:90%;z-index:10000;visibility:visible;'}); //var guib = $(' ', {type: 'button', class: 'wikia-button', value: 'start', id: 'nbtnstart'}); var guibLoad = $(' ', {type: 'button', class: 'wikia-button', value: 'load', id: 'nbtnload'}); var guibSave = $(' ', {type: 'button', class: 'wikia-button', value: 'save', id: 'nbtnsave'}); var guibExport = $(' ', {type: 'button', class: 'wikia-button', value: 'export', id: 'nbtnexport'}); var guit = $(' ', {id: 'ntxtscript', style: 'display:block;margin: 0 5%;width:80%;height:auto;min-height:100px;max-height:80%;'}); var guitLoad = $(' ', {type: 'text', id: 'ntxtLoad', style: 'margin-left: 5%;', value: 'MediaWiki:Custom-DemoScripts'}); var guitExport = $(' ', {type: 'text', id: 'ntxtExport', value: 'Module:Sandbox/fngplg/module'}); gui.append(guitLoad).append(guibLoad).append(guitExport).append(guibExport); gui.append(guibSave).append(guit); if ($(hs).length) { //oasis $(hs).append(btn); } else { //monobook $('#p-personal .pBody ul').append(btn); }//append js button $('body').on('keydown', function (e) {       if (e.ctrlKey && e.keyCode === 8) { //ctrl+bckspc            btnClick;            return false;        }    });//body keydown $('body').on('click', '#nbtnshowguij', btnClick); //$('body').on('click', myname + ' #nbtnstart', btnBClick); $('body').on('click', myname + ' #nbtnload', function (e) {               var nfile = guitLoad.val;        log('btnl.file', nfile);        if (!nfile || !nfile.length) return;        $.get('/index.php', {action: 'raw', title: nfile}).done(function (data) { if (data.error) return; guit.val(data); });   });//btnload $('body').on('click', myname + ' #nbtnsave', function (e) {       var file = guitLoad.val,            fileContent;        if (!file || !file.length) {            log('save file required is', file);            return;        }        var jss = guit.val,            js = jsonTry(jss);        if (js instanceof Error) {            log('save json', js.name, js.message);            alert('JSON parse error\n' + js.name + ': ' + js.message);            return;        }//if js        fileContent = jss;//JSON.stringify(jss);        log('export jss', js, jss, fileContent);        var qurl = (new mw.Uri('/api.php')).extend({action: 'query', titles: file, prop: 'info', intoken: 'edit', format: 'json'});       //get edit token        $.getJSON(qurl).done(function (data) { if (data.error) { log('save gettoken error', data); return; }           log('get token', qurl, data); var etoken = getVal(data, 'edittoken')[0]; log('etoken', etoken); var edata = {action: 'edit', title: file, text: fileContent, summary: 'jsonpad file update', minor: , bot: , format: 'json', token: etoken}; log('save', etoken, edata); //save data to module //*           $.post('/api.php', edata) .done(function (e, stat) {               if (e.error) {                    log('save error', e.error.code, e.error.info);                } else {                    log('save', stat, e.edit, e);                }            })//done .fail(function(e, e1, e2){log('save mega fail', e, e1, e2)}); //*/       });//get qurl            });//btnsave $('body').on('click', myname + ' #nbtnexport', function (e) {       var module = guitExport.val,            moduleContent;                if (!module || !module.length) {            log('export module required is', module);            return;        }        var jss = guit.val,            js = jsonTry(jss);        if (js instanceof Error) {            log('export json', js.name, js.message);            alert('JSON parse error\n' + js.name + ': ' + js.message);            return;        }//if js        moduleContent = createModule(JSON.stringify(jss));        log('export jss', js, jss, moduleContent);        var qurl = (new mw.Uri('/api.php')).extend({action: 'query', titles: module, prop: 'info', intoken: 'edit', format: 'json'});       //get edit token        $.getJSON(qurl).done(function (data) { if (data.error) { log('export gettoken error', data); return; }           log('get token', qurl, data); var etoken = getVal(data, 'edittoken')[0]; log('etoken', etoken); var edata = {action: 'edit', title: module, text: moduleContent, summary: 'jsonpad module update', minor: , bot: , format: 'json', token: etoken}; log('exp', etoken, edata); //save data to module //*           $.post('/api.php', edata) .done(function (e, stat) {               if (e.error) {                    log('export error', e.error.code, e.error.info);                } else {                    log('export', stat, e.edit, e);                }            })//done .fail(function(e, e1, e2){log('export mega fail', e, e1, e2)}); //*/       });//get qurl            });//btnexport $('body').on('keydown', myname + ' #ntxtscript', function (e) {               if (e.keyCode >= 37 && e.keyCode <= 40) return true; //arrows        var $this = $(myname + ' #ntxtscript');        switch (e.keyCode) {            case 9:                $this.focus;                $this.get(0).insertAtCaret('    ');                return false;            case 13:                /*                if (e.shiftKey || e.ctrlKey) {                    btnBClick;                    return false;                }                */                break;            case 27:                $(myname).remove;                return false;        }        if ($this.val && $this.val.length > 100) $this.height($this.get(0).scrollHeight);    });//txt keydown });//json pad