User:Thetrick/tfd helper/automodtfdh.js

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.
//<pre>

/***********************automodtfdh.js***********************/
/* based on [[User:Jnothman/automod.js]] but heavily hacked */
/* Edits a page according to parameters passed in the URL.  */

if ((typeof amtfdh_loaded == 'undefined')
    || !amtfdh_loaded) {
amtfdh_loaded = true;
amtfdh_exectd = false;

function amtfdh_make_url(title, before, after, summary) {
    return 'http://en.wikipedia.org/w/index.php?title='+escape(title)+
        '&action=edit'+
        '&tfdaddbefore='+escape(before)+'&tfdaddafter='+escape(after)+
        '&tfdsummary='+escape(summary);
}

function amtfdh_get_query_vars(){ 
     var res = new Array();
     var pairs = location.search.substring(1).split("&"); 
     for(var i=0; i < pairs.length; i++){ 
         var pair = pairs[i].split("=");
         res[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
     } 
     return  res; 
}

function amtfdh_add_li(portlet, url, text, id, title) {
    var tabs = document.getElementById('p-'+portlet).getElementsByTagName('ul')[0]
    var na = document.createElement('a');
    na.href = url;
    na.id = id;
    na.appendChild(document.createTextNode(text));
    if (title)
       na.title = title;
    var li = document.createElement('li');
    if (id)
       li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    return li;
}

function amtfdh_guess_date() {
    var monthnames = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var today = new Date();
    return today.getUTCFullYear() + ' ' + monthnames[today.getUTCMonth()] + ' ' + today.getUTCDate();
}

function amtfdh_edit_string(s, insertion, loc) {
    return s.substring(0, loc) + insertion + s.substring(loc);
}

function auto_mod_tfdh() {
    if (amtfdh_exectd)
        return false;
    amtfdh_exectd = true;
    qvars = amtfdh_get_query_vars();
    if (qvars['action']=='edit') {
      if (qvars['tfdaddafter'] || qvars['tfdaddbefore'] || qvars['tfdfind']) {
        var summ_el = document.getElementById('wpSummary');
        if (summ_el.value != '' && summ_el.value.substr(summ_el.value.length - 3, 3) != '*/ ') {
            // have already added summary
            return true;
        }
        var text = document.getElementById('wpTextbox1');
        if (qvars['tfdfind'] && qvars['tfdreplace'])
            text.value = text.value.replace(new RegExp(qvars['tfdfind'], "g"), qvars['tfdreplace']);
        /* special stripping of uncategorized - damn there are a lot of these */
        if (qvars['tfdfind'] == '{{uncat}}') {
            /* uncat, Uncat, uncategorized, Uncategorized, uncategorised, Uncategorised, UncategorizedTemplate,
               Uncat-date, Uncategorized-date */
            text.value = text.value.replace(new RegExp('\{\{[Uu]ncat.*?\}\}', "g"), '');
            /* catneeded, Catneeded, CatNeeded */
            text.value = text.value.replace(new RegExp('\{\{[Cc]at[Nn]eeded.*?\}\}', "g"), '');
            /* Cat needed */
            text.value = text.value.replace(new RegExp('\{\{[Cc]at [Nn]eeded.*?\}\}', "g"), '');
            /* Cats needed */
            text.value = text.value.replace(new RegExp('\{\{[Cc]ats [Nn]eeded.*?\}\}', "g"), '');
            /* Needs cat, Needs cats */
            text.value = text.value.replace(new RegExp('\{\{[Nn]eeds [Cc]at.*?\}\}', "g"), '');
            /* Nocat, Nocats */
            text.value = text.value.replace(new RegExp('\{\{[Nn]o[Cc]at.*?\}\}', "g"), '');
            /* Categorise, Categorize */
            text.value = text.value.replace(new RegExp('\{\{[Cc]ategori[sz]e.*?\}\}', "g"), '');
            /* Categoryneeded, Category needed, Category requested */
            text.value = text.value.replace(new RegExp('\{\{[Cc]ategory[needed| needed| requested].*?\}\}', "g"), '');
            /* Categories needed, Categories requested */
            text.value = text.value.replace(new RegExp('\{\{[Cc]ategories [needed|requested].*?\}\}', "g"), '');
            /* Classify */
            text.value = text.value.replace(new RegExp('\{\{[Cc]lassify.*?\}\}', "g"), '');
            /* hardcoded category */
            text.value = text.value.replace(new RegExp('..Category.Wikipedia uncategorized templates...PAGENAME....', "gi"), '');
            text.value = text.value.replace(new RegExp('..Category.Wikipedia uncategorized templates..', "gi"), '');
            /* strip noinclude pairs */
            text.value = text.value.replace(new RegExp('<noinclude>\n*?<\/noinclude>\s?', "g"), '');
        }
        /* special stripping of TfD */
        if (qvars['tfdfind'] == '{{tfd}}') {
            /* noincluded tfd - run twice for super-munged pages */
            text.value = text.value.replace(new RegExp('<noinclude>\n*\{\{tfd.*?\}\}\s*<\/noinclude>\n?'), '');
            text.value = text.value.replace(new RegExp('<noinclude>\n*\{\{tfd.*?\}\}\s*<\/noinclude>\n?'), '');
            /* plain tfd - run twice for super-munged pages */
            text.value = text.value.replace(new RegExp('\{\{tfd.*?\}\}\n?'), '');
            text.value = text.value.replace(new RegExp('\{\{tfd.*?\}\}\n?'), '');
        }
        if (qvars['tfdaddafter']) {
            text.value = text.value.replace(/\s+$/,"");
            var loc = text.value.length;
            if (qvars['tfdatstring']) {
                var newloc = text.value.indexOf(qvars['tfdatstring']);
                if (newloc > -1) {
                    loc = newloc + qvars['tfdatstring'].length;
                }
            }
            var toadd = qvars['tfdaddafter'];
            text.value = amtfdh_edit_string(text.value, toadd, loc);
        }
        if (qvars['tfdaddbefore']) {
            var loc = 0;
            if (qvars['tfdatstring']) {
                var newloc = text.value.indexOf(qvars['tfdatstring']);
                if (newloc > -1) {
                    loc = newloc;
                }
            }
            var toadd = qvars['tfdaddbefore'];
            text.value = amtfdh_edit_string(text.value, toadd, loc);
        }
        summ_el.value += (qvars['tfdsummary'] || ' ');
        /* allow for preview */
        if (qvars['tfdpreview']) {
            document.getElementById('wpPreview').click();
        } else {
            document.getElementById('editform').submit();
        }
      }
      return true;
    }
    return false;
}

function amtfdh_add_onload(f) {
  // borrowed from [[WP:US]]
  if (window.addEventListener) window.addEventListener("load",f,false);
  else if (window.attachEvent) window.attachEvent("onload",f);
}

} // end if amtfdh_loaded

//</pre>