User:Gimmetrow/monobook.js

From Wikipedia

< User:Gimmetrow

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror:: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences.

// <pre><code>

/*
function inc (file) {
  var lt = String.fromCharCode(60);
  var gt = String.fromCharCode(62);
  document.writeln(lt+'script type="text/javascript" src="/w/index.php?title='+file+'&amp;action=raw&amp;ctype=text/javascript&amp;dontcountme=s"'+gt+lt+'/script'+gt);
} */

function addLink(where, url, name, id, title, key, after){
    //* where is the id of the toolbar where the button should be added;
    //   i.e. one of "p-cactions", "p-personal", or "p-navigation".
    //* url is the URL which will be called when the button is clicked.
    //   javascript: urls can be used to do more complex things.
    //* name is what will appear as the name of the button.
    //* id is the id of the button; it's best to define one.  
    //   Use a prefix to make sure its unique. Optional.
    //* title is the tooltip title that gives a longer description 
    //   of the button; if you define a accesskey, mention it here. Optional.
    //* key is the char you want for the accesskey. Optional.
    //* after is the id of the button you want to follow this one. Optional.
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    var tabs = document.getElementById(where).getElementsByTagName('ul')[0];
    if(after) {
	tabs.insertBefore(li,document.getElementById(after));
    } else {
	tabs.appendChild(li);
    }
    if(id) {
	if(key && title) { ta[id] = [key, title]; }
	else if(key) { ta[id] = [key, '']; }
	else if(title) { ta[id] = ['', title];} 
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function regExp() {
    var s = prompt("Search regexp?");
    if(s) {
        var r = prompt("Replace regexp?");
        if(!r && r != '') return;
        var txt = document.editform.wpTextbox1;
        txt.value = txt.value.replace(new RegExp(s, "g"), r);
    }
}
// User:Gimmetrow/replace.js
function fixRefMarks() {
   var qt = String.fromCharCode(34)
   // var hb = String.fromCharCode(124)
   var txt = document.editform.wpTextbox1;

   // fact and other templates (geographic reference template, ref/note)
   var FactTag = "({{[ ]*fact[ ]*}}|{{[ ]*citequote[ ]*}}|{{[ ]*citation needed[ ]*}}|{{[ ]*cn[ ]*}}|{{[ ]*verification needed[ ]*}}|{{[ ]*verify source[ ]*}}|{{[ ]*verify credibility[ ]*}}|{{[ ]*who[ ]*}}|{{[ ]*failed verification[ ]*}}|{{[ ]*nonspecific[ ]*}}|{{[ ]*dubious[ ]*}}|{{[ ]*or[ ]*}}|{{[ ]*lopsided[ ]*}}|{{[ ]*GR[ ]*[\|][ ]*[^ ]+[ ]*}}|{{[ ]*[c]?r[e]?f[ ]*[\|][^}]*}}|{{[ ]*ref[ _]label[ ]*[\|][^}]*}})";
   txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?"+FactTag, "gi"), "$1");

   // one+ space/linefeed, avoid matching references tag as a named ref
   txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?<ref([ >])", "gi"), "<ref$1");

   // remove trailing spaces from named refs
   txt.value = txt.value.replace(new RegExp("<ref ([^>]*[^> ])[ ]*>", "gi"), "<ref $1>");

   // remove some superscripted punctuation between refs (is between necessary?)
   txt.value = txt.value.replace(new RegExp("(</ref>|<ref[^>]*?/>)<sup>[ ]*[,;-]?[ ]*</sup><ref", "gi"), "$1<ref");
   txt.value = txt.value.replace(new RegExp("(</ref>|<ref[^>]*?/>)[ ]*[,;-]?[ ]*<ref", "gi"), "$1<ref");

   // can this complicated close tag be simplified?
   //var closetag = "(([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?)</ref>";
   //var closetag = "((.*?))</ref>";

   var LacksPunctuation = "([^\.,;:!\?"+qt+"'’])";
   var QuestionOrExclam = "([!\?])";
   var MinorPunctuation = "([\.,;:])";

   var Quote = "([" + qt + "'’]*)";
   var Space = "[ ]*";

   var AnyPunctuation = "([\.,;:!\?])";
   var MajorPunctuation = "([,;:!\?])";
   var Period = "([\.])";

   var RefTag1 = "(<ref>([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?</ref>)";
   var RefTag2 = "(<ref[^>]*?[^/]>([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?</ref>)";
   var RefTag3 = "(<ref[^>]*?/>)";
   //var Note = "("+RefTag1 + "|(" + RefTag2 + ")|(" + FactTag + "))";

   var match0a = LacksPunctuation+Quote+FactTag+Space+AnyPunctuation;
   var match0b = QuestionOrExclam+Quote+FactTag+Space+MajorPunctuation;
   var match0c = MinorPunctuation+Quote+FactTag+Space+AnyPunctuation;
   var match0d = QuestionOrExclam+Quote+FactTag+Space+Period;

   var match1a = LacksPunctuation+Quote+RefTag1+Space+AnyPunctuation;
   var match1b = QuestionOrExclam+Quote+RefTag1+Space+MajorPunctuation;
   var match1c = MinorPunctuation+Quote+RefTag1+Space+AnyPunctuation;
   var match1d = QuestionOrExclam+Quote+RefTag1+Space+Period;

   var match2a = LacksPunctuation+Quote+RefTag2+Space+AnyPunctuation;
   var match2b = QuestionOrExclam+Quote+RefTag2+Space+MajorPunctuation;
   var match2c = MinorPunctuation+Quote+RefTag2+Space+AnyPunctuation;
   var match2d = QuestionOrExclam+Quote+RefTag2+Space+Period;

   var match3a = LacksPunctuation+Quote+RefTag3+Space+AnyPunctuation;
   var match3b = QuestionOrExclam+Quote+RefTag3+Space+MajorPunctuation;
   var match3c = MinorPunctuation+Quote+RefTag3+Space+AnyPunctuation;
   var match3d = QuestionOrExclam+Quote+RefTag3+Space+Period;

   for (var j=0; j<10; j++)  // repeat for multiple refs together
   {
      txt.value = txt.value.replace(new RegExp(match0a, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0b, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0c, "gi"), "$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0d, "gi"), "$1$2$3");

      txt.value = txt.value.replace(new RegExp(match1a, "gi"), "$1$2$5$3");
      txt.value = txt.value.replace(new RegExp(match1b, "gi"), "$1$2$5$3");
      txt.value = txt.value.replace(new RegExp(match1c, "gi"), "$2$5$3");
      txt.value = txt.value.replace(new RegExp(match1d, "gi"), "$1$2$3");

      txt.value = txt.value.replace(new RegExp(match2a, "gi"), "$1$2$5$3");
      txt.value = txt.value.replace(new RegExp(match2b, "gi"), "$1$2$5$3");
      txt.value = txt.value.replace(new RegExp(match2c, "gi"), "$2$5$3");
      txt.value = txt.value.replace(new RegExp(match2d, "gi"), "$1$2$3");

      txt.value = txt.value.replace(new RegExp(match3a, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3b, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3c, "gi"), "$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3d, "gi"), "$1$2$3");
   }
   var summary = document.editform.wpSummary;
   //summary.value = summary.value + "+[[WP:FN]] [[User:Gimmetrow/replace.js|fixes]] ";
   summary.value = summary.value + "+[[WP:FN]] fixes ";
}

addOnloadHook(function () {
    if(document.forms.editform) {
        // addLink('p-cactions', 'javascript:regExp()', 'regexp', 'ca-regexp', 'Regexp replace for the edit window', '', 'ca-history');
        addLink('p-tb', 'javascript:fixRefMarks()', 'Fix reference marks', 't-reffix', 'Ref fix for the edit window', '', '');
    }
});

//inc("User:Gimmetrow/replace.js"); // replace and fixref tabs (uses addlink)

//</code></pre>
Navigation
generic heading