User:PerfektesChaos/vector.js

// ==UserScript== // @name       vector // @version    2014-12-10 // ==/UserScript== /*jshint curly:true, forin:false, laxbreak:true, strict:true, trailing:true, white:false                                  */ /*global window:false, parseInt:false                                 */

if ( ! window.PerfektesChaos ) { window.PerfektesChaos =  { }; } if ( ! window.PerfektesChaos.bb ) { window.PerfektesChaos.bb =  { }; }

window.PerfektesChaos.bb.javascript =  function (mw, $) { // 2013-10-20  "use strict"; var $textarea;

function fire { // 2013-10-20     var subject, s =  mw.config.get("wgAction"); if (s === "edit" ||  s === "submit") { $textarea =  $("#wpTextbox1"); if ($textarea.length) { subject =  mw.config.get("wgTitle"); if (/\/d.*\.js$/.test(subject)) { $textarea.change(sourceVersion, sourceDebugPath); } else if (/\/r.*\.js$/.test(subject)) { sourceMinify; }           sourceVersion; }     }      // sourceDebugPath; }  // fire

function fromCharCode(adjust) { // 2013-10-23     var c, got, j, k, re, s, v,          r  =  adjust, i =  r.indexOf("String.fromCharCode"); if (i > 3) { re  =    "(\"\\+)?"                 // 1                 + "String.fromCharCode\\("                 + "([0-9,]+)"                // 2                 + "\\)"                 + "(\\+\")?";                // 3 re  =  new RegExp(re); while (i > 3) { i -=  2; got =  re.exec(r.substr(i)); if (got) { s =  (got[1] ? "" : "\"");               v  =  got[2].split(",");               for (k = 0;  k < v.length;  k++) {                  j  =  parseInt(v[k], 10);                  switch (j) {                     case 10 :                        c  =  "\\n";                        break;                     case 34 :   // " c =  "\\\"";                        break;                     case 92 :   // \                        c  =  "\\\\";                        break;                     default:                        c  =  String.fromCharCode(j);                  }   // switch j                  s  =  s + c;               }   // for k               s   =  s  +  (got[3] ? "" : "\""); i +=  got.index; r  =  r.substr(0, i)                      +  s                      +  r.substr(i + got[0].length); i +=  2; } else { i +=  20; }           i  =  r.indexOf("String.fromCharCode", i); }  // while i > 0 }     return r;   }   // fromCharCode

function sourceDebugPath { // 2013-10-20     var re, shift, source =  $textarea.val; if (source.indexOf("/// ") === 0) { re    =  /(\/\/\/ .*[a-z]\/)\?([-a-zA-Z_0-9]*\.js\n?)/g; shift =  source.replace(re, "$1d$2"); if (shift !== source) { $textarea.val(shift); }     }   }   // sourceDebugPath

function sourceMinify { // 2013-10     var shift, source =  $textarea.val; if (source.indexOf("String.fromCharCode") > 0) { shift =  fromCharCode(source); if (shift !== source) { $textarea.val(shift); }     }   }   // sourceMinify

function sourceVersion { // 2014-12-10     var got, re, $summary, source =  $textarea.val; if (source.length) { re  =  "(?:      |,|var |[a-z]\\.|\\[ *[a-zA-Z0-9]+ *\\]+\\.)" + "(?:version|vsn|Version|VERSION|VSN)" + " *= *"                 +  "(-?[0-9]+\\.?[0-9]*)"                 +  "[,;]";         re   =  new RegExp(re); got =  re.exec(source); if (got) { $summary =  $("#wpSummary"); if ($summary.length) { source =  $summary.val; if (source.length) { re =  /^(-?[0-9]+\.?[0-9]*)$/; if (re.test(source)) { source =  ""; }              }               if (! source.length) { $summary.val(got[1]); }           }         }      }   }   // sourceVersion

/*  function sourceYW { var got, source =  $textarea.val; if (source.length) { re  =  /(\.main\.wait\(\")Y\"/;         got  =  re.exec(source);         if (got) {            source  =  source.replace(re, "$1W\"");            $textarea.val(source);         }      }   }   // sourceYW

mw.hook("wikipage.content").add(fire); };  // .bb.javascript

//---

( function (PC, mw, $) {  // 2014-12-01   "use strict";   if (mw.config.get("wgPageContentModel") === "javascript"  && mw.config.get("wgNamespaceNumber") === 2) {     if (! mw.config.get("wgIsArticle")) {        PC.bb.javascript(mw, window.jQuery);      }       mw.hook("postEdit").add( function  { PC.bb.vc(PC, mw, $); } );  } }(window.PerfektesChaos, window.mediaWiki, window.jQuery) );