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 facttags = "({{[ ]*fact[ ]*}}|{{[ ]*citequote[ ]*}}|{{[ ]*citation needed[ ]*}}|{{[ ]*cn[ ]*}}|{{[ ]*verification needed[ ]*}}|{{[ ]*verify source[ ]*}}|{{[ ]*GR[ ]*[\|][ ]*[^ ]+[ ]*}}|{{[ ]*[c]?r[e]?f[ ]*[\|][^}]*}}|{{[ ]*ref[ _]label[ ]*[\|][^}]*}})";
   txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?"+facttags, "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");

   var closetag = "(([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?)</ref>";
   //var closetag = "((.*?))</ref>";

   for (var j=0; j<10; j++)  // repeat for multiple refs together
   {
      // fact tags
      txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)"+facttags+"[ ]*([\.,;:!\?])", "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)"+facttags+"[ ]*([,;:!\?])", "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)"+facttags+"[ ]*([\.,;:!\?])", "gi"), "$2$4$3");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)"+facttags+"[ ]*([\.])", "gi"), "$1$2$3");

      // ref followed by punctuation, perhaps preceeded by punctuation (with or without quote)
      // may contain other tags (eg <cite></cite>
      txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$1$2$5<ref>$3</ref>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([,;:!\?])", "gi"), "$1$2$5<ref>$3</ref>");
      txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$2$5<ref>$3</ref>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.])", "gi"), "$1$2<ref>$3</ref>");

      // named ref with content, may contain other tags
      txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$1$2$6<ref$3>$4<\/ref>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([,;:!\?])", "gi"), "$1$2$6<ref$3>$4<\/ref>");
      txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$2$6<ref$3>$4<\/ref>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.])", "gi"), "$1$2<ref$3>$4<\/ref>");

      // named ref re-used
      txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref([^>]*?)/> *?([\.,;:!\?])", "gi"), "$1$2$4<ref$3/>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([,;:!\?])", "gi"), "$1$2$4<ref$3/>");
      txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([\.,;:!\?])", "gi"), "$2$4<ref$3/>");
      txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([\.])", "gi"), "$1$2<ref$3/>");

   }
   var summary = document.editform.wpSummary;
   //summary.value = summary.value + "+[[WP:FN]] [[User:Gimmetrow/replace.js|fixes]] ";
   summary.value = summary.value + "+[[WP:FN]] fixes ";
}

function gimmeProseSize() {
    var txt2 = document.editform.wpTextbox1;
    var txt1 = txt2.value;

    // templates
    txt2.value = txt2.value.replace(new RegExp("{{[^{}]*}}", "gi"), "");

    // citations
    //var closetag = "(([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?)</ref>";
    var closetag = "((.*?))</ref>";
    txt2.value = txt2.value.replace(new RegExp("<ref>"+closetag, "gi"), "");
    txt2.value = txt2.value.replace(new RegExp("<ref([^>]*?[^/])>"+closetag, "gi"), "");
    txt2.value = txt2.value.replace(new RegExp("<ref([^>]*?)/>", "gi"), "");

    // external links, keep anything following a space
    txt2.value = txt2.value.replace(new RegExp("\[[ ]*http[s]?://[^\] ]*?]", "gi"), "uuu");
    txt2.value = txt2.value.replace(new RegExp("\[[ ]*http[s]?://[^\] ]*?([^\]]*)]", "gi"), "vvv$1");

    txt2.value = txt2.value.replace(new RegExp("\[[ ]*http[s]?://[^\] ]*[^\]]*]", "gi"), "ttt");


    // end sections
    var endoptions = "(See also|Note[s]?|Reference[s]?|Footnote[s]?|Bibliography|Further reading|External link[s]?)"
    // bunch everthing up
    txt2.value = txt2.value.replace(new RegExp("==[ ]*"+endoptions+"[ ]*==([\r\n\f]*.*)*", "gi"), "zzz");
    // remove it
    //txt2.value = txt2.value.replace(new RegExp("==[ ]*"+endoptions+"[ ]*==[.\r\n\f]*","gi"), "");

    // wikilinks, keep visible text
    //txt2.value = txt2.value.replace(new RegExp("\[\[([^|]*?)]]", "gi"), "$1");
    //txt2.value = txt2.value.replace(new RegExp("\[\[([^]]*)]]", "gi"), "");

    //txt2.value = txt2.value.replace(new RegExp("\[\[(test)\]\]", "gi"), "");

    // html comments
    //txt2.value = txt2.value.replace(new RegExp("<!--([^-]|-[^-]|--[^>])*-->", "g"), "");
    txt2.value = txt2.value.replace(new RegExp("<!--.*?-->", "g"), "");

    // other html tags
    txt2.value = txt2.value.replace(new RegExp("<([A-Za-z][A-Za-z0-9]*)[^>]*?>.*?</\1>", "g"), "yyy");
    txt2.value = txt2.value.replace(new RegExp("<([A-Za-z][A-Za-z0-9]*)[^>]*?/>", "g"), "xxx");

    var s = "Raw = "+txt1.length + String.fromCharCode(13) + "Size = " + txt2.value.length;
    //var t = document.getElementByID("BodyContent");
    //var notice = document.createElement("p");
    //notice.setAttribute('id', 'ProseSize');
    //var noticetext = document.createTextNode(s);
    //notice.appendChild(noticetext);
    //t.AppendChild(notice);

    alert(s);
}

addOnloadHook(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:regExp()', 'regexp', 'ca-regexp', 'Regexp replace for the edit window', '', 'ca-history');
        addLink('p-cactions', 'javascript:fixRefMarks()', 'fix refs', 'ca-reffix', 'Ref fix for the edit window', '', 'ca-history');
        addLink('p-cactions', 'javascript:gimmeProseSize()', 'prose2', 'ca-reffix', 'Prose count', '', 'ca-history');
    }
});

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

//</code></pre>
Navigation