User:JoeSmack/reftagger.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.
if(wgAction == 'edit' || wgAction == 'view') addOnloadHook(refbuttons)
function refbuttons() {
  var yesref='/wiki/Special:Random';
  if(wgAction == 'edit') {
    var noref='javascript:reftag();';
  } else {
    var noref='javascript:edreftag();';
  }
  mw.util.addPortletLink('p-cactions',yesref,'y ref','ca-yesref','Go to random article');
  mw.util.addPortletLink('p-cactions',noref,'n ref','ca-noref','tag with {{references}}');
}

function reftag() {
  if(document.getElementById('wpTextbox1').value.indexOf('{'+'{references}}') != -1) {
    alert('Warning: There is already a references tag present. \nNo action taken.');
    return;
  }
  document.getElementById('wpTextbox1').value = '{'+'{references}}\n' + document.getElementById('wpTextbox1').value;
  var saveme = confirm('A {'+'{references}} tag has been added to the top of this section.\nDo you want to save this page automatically?\n\nClick cancel/escape to abort, to move the tag, or to save manually.');
document.getElementById('wpSummary').value = '{{references}} - this article or section does not cite any references or sources. Please help improve this article by adding citations to [[WP:RS|reliable sources]].';
document.getElementById('wpMinoredit').checked = true;
//  if(saveme) document.getElementById('wpPreview').click()
  if(saveme) document.getElementById('wpSave').click()
}

function edreftag() {
  var url = document.getElementById('ca-edit').firstChild.href;
  document.location.href = url + '&addreftag=true';
}
if(queryString("addreftag") == "true") addOnloadHook(reftag);

function queryString(p) {
        var re = RegExp('[&?]' + p + '=([^&]*)');
  
        var matches;
  
        if (matches = re.exec(document.location)) {
                try { 
                        return decodeURI(matches[1]);
                } catch (e) {
                }
        }
  
        return null;
}