Jump to content

User:Quarl/automod.js

From Wikipedia, the free encyclopedia
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.
// User:Quarl/automod.js

// requires: wikipage.js, util.js

// based on http://en.wikipedia.org/wiki/User:Jnothman/automod.js

// - added 'amnocreate'
// - fixed escape '+' bug

//<pre><nowiki>

if ((typeof auto_mod_loaded == 'undefined')
    || !auto_mod_loaded) {
auto_mod_loaded = true;
auto_mod_exectd = false;

function am_make_url(title, before, after, summary) {
    return 'http://en.wikipedia.org/w/index.php?title='+wpaescape(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;

    if (queryVars['action']=='edit') {
      if (queryVars['amnull']) {
        document.getElementById('editform').submit();
        return true;
      }
      if (queryVars['amaddafter'] || queryVars['amaddbefore'] || queryVars['amreplace']) {
        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 (queryVars['amnocreate'] && ! text.value) {
            alert("Error!  Page is empty; refusing to create.");
            return false;
        }
        if (queryVars['amclear'])
            text.value = '';
        if (queryVars['amfind'] && queryVars['amreplace'])
            text.value = text.value.replace(new RegExp(queryVars['amfind'], "g"), queryVars['amreplace']);
        if (queryVars['amaddafter']) {
            if (text.value.charAt(text.value.length-1) != '\n')
                text.value += '\n';
            text.value += queryVars['amaddafter'];
        }
        if (queryVars['amaddbefore']) {
            if (text.value.charAt(0) != '\n')
                text.value = '\n' + text.value;
            text.value = queryVars['amaddbefore'] + text.value;
        }
        summ_el.value += (queryVars['amsummary'] || ' ');
        document.getElementById('editform').submit();
      }
      return true;
    }
    return false;
}

addOnloadHook(auto_mod);

} // end if auto_mod_loaded

// </nowiki></pre>