User:Dto/monobook.js/poem
Appearance
< User:Dto | monobook.js
/* By [[User:Dto]]. Adapt/use it if it helps. */
var dto = {
lastText: null // for undo functionality
};
dto.addEditButton = function(button) {
var b = document.getElementById("wpDiff");
if(!b)
return false;
while(b.nextSibling.nodeName.toLowerCase() == "input")
b = b.nextSibling;
if(!b.nextSibling)
b.parentNode.appendChild(button);
else
b.parentNode.insertBefore(button, b.nextSibling);
return true;
};
dto.makeButton = function(bId, bValue, clickhandler) {
var b = document.createElement("input");
b.setAttribute("type", "button");
b.setAttribute("id", bId);
b.setAttribute("name", bId);
b.setAttribute("value", bValue);
b.onclick = clickhandler;
return b;
};
dto.detectSonnetNumber = function() {
var sonnet = parseInt(wgTitle.substring(wgTitle.lastIndexOf("/") + 1, wgTitle.length));
return isNaN(sonnet) ? -1 : sonnet;
}
dto.onLoad = function() {
if(wgNamespaceNumber == 0) // Main namespace
dto.addEditButton(
dto.makeButton(
"dtoFeelinglucky",
"I'm Feeling Lucky (<poem>)",
function() {
var textbox = document.getElementById("wpTextbox1");
if(!textbox)
return;
var txt = textbox.value;
// txt will be passed through the functions, in order
var functs = [dto.utf8emdashes, dto.detectPoem, dto.removeLineBreaks, dto.removeNonBreakingSpaces];
for(var i = 0; i < functs.length; i++) {
if(!(txt = functs[i](txt)))
return;
textbox.value = txt;
}
var editsummary = document.getElementById("wpSummary");
if(editsummary && editsummary.value == '') {
editsummary.value = "use <poem>";
var minor = document.getElementById("wpMinoredit");
if(minor)
minor.checked = true;
}
var diff = document.getElementById("wpDiff");
if(diff)
diff.click();
return true;
}
)
);
else if(wgNamespaceNumber == 1) // Talk namespace
dto.addEditButton(
dto.makeButton(
"dtoFeelingluckyTalk",
"I'm Feeling Lucky (textinfo)",
function() {
var textbox = document.getElementById("wpTextbox1");
if(!textbox)
return false;
var page = window.prompt("Page identifier (&pg=XXX) in the original edition?");
var page2 = window.prompt("Page identifier (&pg=XXX) in the 1883 edition?");
textbox.value = "{{textinfo\n\
| edition = [http://books.google.com/books?id=vF3QMSp6pg4C&pg=" + page + " 1609]\n\
| source = [http://books.google.com/books?id=6eRMP7KjGGkC&pg=" + page2 + " Harper & Brothers edition (1883)]\n\
| contributors = [[User:Kalki|Kalki]], [[User:Dto|Dto]]\n\
| progress = Proofread and corrected [[Image:75%.png]]\n\
| notes = \n\
| proofreaders = [[User:Dto|Dto]]\n\
}}" + (textbox.value == "" ? "" : "\n\n") + textbox.value;
var editsummary = document.getElementById("wpSummary");
if(editsummary && editsummary.value == '')
editsummary.value = "textinfo";
var preview = document.getElementById("wpPreview");
if(preview)
preview.click();
return false;
}
)
);
};
dto.utf8emdashes = function(txt) {
return txt.replace(/—/g, "—");
};
dto.detectPoem = function(txt) {
var i = txt.indexOf("\n\n");
if(i < 0)
return false;
i += 2;
txt = txt.substr(0, i) + "<poem>\n" + txt.substring(i, txt.length);
i = txt.lastIndexOf("\n\n");
if(i < 0)
return false;
txt = txt.substr(0, i) + "\n</poem>" + txt.substring(i, txt.length);
return txt;
};
dto.removeLineBreaks = function(txt) {
return txt.replace(/<br>/g, "");
};
dto.removeNonBreakingSpaces = function(txt) {
if(wgTitle.indexOf("The Rime of the Ancyent Marinere") >= 0)
return txt.replace(/ /g, " ");
return txt.replace(/ /g, " ");
};
hookEvent("load", dto.onLoad);