Jump to content

User:Jnothman/automod.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jnothman (talk | contribs) at 01:11, 7 November 2005 (amclear query value). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
if ((typeof auto_mod_loaded == 'undefined')
    || !auto_mod_loaded) {
auto_mod_loaded = true;
auto_mod_exectd = false;

function am_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 am_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 am_get_title() {
    var headings = document.getElementsByTagName('h1');
    for (var i=0; i < headings.length; i++)
        if (headings[i].className == 'firstHeading')
            return headings[i].innerText || headings[i].textContent; // depending on browser, might have problems with ampersands in title
}

function am_guess_date() {
    var monthnames = new Array( "January", "Februrary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber");
    var today = new Date();
    return (today.getYear()+1900) + ' ' + monthnames[today.getMonth()] + ' ' + today.getDate();
}

function am_make_url(title, before, after, summary) {
    return 'http://en.wikipedia.org/w/index.php?title='+escape(title)+
        '&action=edit'+
        '&amaddbefore='+escape(before)+'&amaddafter='+escape(after)+
        '&amsummary='+escape(summary);
}

function auto_mod() {
    if (auto_mod_exectd)
        return false;
    auto_mod_exectd = true;
    qvars = am_get_query_vars();
    if (qvars['action']=='edit') {
      if (qvars['amaddafter'] || qvars['amaddbefore']) {
        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['amclear'])
            text.value = '';
        if (qvars['amaddafter']) {
            if (text.value.charAt(text.value.length-1) != '\n')
                text.value += '\n';
            text.value += qvars['amaddafter'];
        }
        if (qvars['amaddbefore']) {
            if (text.value.charAt(0) != '\n')
                text.value = '\n' + text.value;
            text.value = qvars['amaddbefore'] + text.value;
        }
        summ_el.value += (qvars['amsummary'] || ' ');
        document.getElementById('editform').submit();
      }
      return true;
    }
    return false;
}

var am_add_onload=function(f) {
  // borrowed from [[WP:US]]
  if (window.addEventListener) window.addEventListener("load",f,false);
  else if (window.attachEvent) window.attachEvent("onload",f);
  else {
    var oldOnload='_old_onload_'+am_add_onload.uid;
    addOnloadFunction[oldOnload] = window.onload ? window.onload : function () {};
    window.onload = function() { am_add_onload[oldOnload]();  f(); }
    ++am_add_onload.uid;
  }
}
am_add_onload.uid=0;


} // end if auto_mod_loaded