User talk:Minesweeper.007/force edit summary.js

// Adapted from: //http://en.wikipedia.org/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Force_edit_summary // User:Razorclaw/force edit summary.js

// The original value of the edit summary field is stored here var editsummOriginalSummary = new String;

// A global ref to the dropdown with canned edit summaries var editsummDropdown = null;

function editsummInitialize {   // TEMPORARY - check window title while testing this code // and only do anything if it's the sandbox or a sample CP page // var wt = document.title; // if ((wt.indexOf('Sandbox')==-1) && (wt.indexOf('Mitch McCon')==-1)) // {   //     return; // }

if(!/&action=edit/.test(window.location.href) && !/&action=submit/.test(window.location.href)) return; if(/&section=new/.test(window.location.href)) return; if(!document.forms.editform) return; document.forms.editform.wpSave.onclick = editsummForceSummary;

// Save the original value of the edit summary field editsummOriginalSummary = document.forms.editform.wpSummary.value;

// For convenience, add a dropdown box with some canned edit // summaries to the form. // Put in alphabetical order

var insertBeforeThis = document.forms.editform.wpSummary.nextSibling; var theParent = insertBeforeThis.parentNode; theParent.insertBefore(dropdown,insertBeforeThis);

// Store a global ref to it   editsummDropdown = dropdown; }

function editsummAddOptionToDropdown(dropdown,optionText) {   var option = document.createElement("option"); var optionTextNode = document.createTextNode(optionText); option.appendChild(optionTextNode); dropdown.appendChild(option); }

// There's a cross-browser issue when accessing the selected text: // *In Firefox you can use: selectObj.value // *In IE, you have to use: selectObj.options[selectObj.selectedIndex].text // *The latter method also works in Firefox function editsummOnCannedSummarySelected {   var idx = editsummDropdown.selectedIndex; var canned = editsummDropdown.options[idx].text;

var newSummary = editsummOriginalSummary; if (newSummary.length!=0) newSummary += " "; newSummary += canned; document.forms.editform.wpSummary.value = newSummary; }

function editsummForceSummary {   if(!document.forms.editform.wpSummary.value.replace(/^(?:\/\\*.*\\*\/)? *(.*) *$/,'$1'))   {        alert("Please supply a meaningful summary of this edit"); document.forms.editform.wpSummary.focus; return false; }

// An edit summary has been supplied. So now do the SW/CP auto-prefixing.

editsummAddSubProjectPrefix;

return true; }

// Prefix the edit summary with "SW" or "CP" // depending on whether it's a SourceWatch or Congresspedia page. // To determine this, look for a link to "Template:Congresspedia" // on the edit page.

// To readd SW: put it in the first "" after the var prefix = // To readd CP: put it in the second "" after the prefix =

function editsummAddSubProjectPrefix {   // Using the document.links array and the href prop seems to give // the best cross-browser results. var allAnchors = document.links; if (allAnchors) {       var prefix = ""; for (i = 0; i < allAnchors.length; i++) {            var anchorHref = allAnchors[i].href; if (anchorHref) {                if (anchorHref.indexOf('Template:Congresspedia') != -1) {                   prefix = ""; }            }        }

document.forms.editform.wpSummary.value = prefix + document.forms.editform.wpSummary.value; } }

addOnloadHook(editsummInitialize);