User:DPMaid/common.js
Jump to navigation
Jump to search
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. You may create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
This JavaScript is executed for DPMaid on every page load.
/** Returns the content of a parameter passed via URL. Does not
* treat booleans in a dedicated manner, returning their string
* content instead. If the parameter is not found, returns either
* a default value (iDefaultValue) or an empty string.
* Based on Wiktionary's MediaWiki:Edit.js. */
function getURLParameter(parameterName, /*optional*/ iDefaultValue) {
var defaultValue = typeof iDefaultValue != 'undefined' ? iDefaultValue : "";
if (! (window.location.search && window.location.search.split("?")[1]))
return defaultValue;
try {
urlParts=window.location.search.split("?")[1].split("&"); }
catch (e) {
return defaultValue; } //not an edit page
for (var k = 0; k < urlParts.length; ++k) {
pair=urlParts[k].split("=");
if (pair[0]==parameterName)
return decodeURIComponent(pair[1].replace(/\+/gi," "));
}
return defaultValue;
}
/** Alerts the user by adding a new div to the center of the page rather than
* via a pop up. */
function divAlert(message) {
var div = document.createElement("div");
div.style.position = "fixed";
div.style.zIndex = 100;
div.style.top = "40%";
div.style.left = "30%";
div.style.marginRight = "30%";
div.style.padding = "15pt";
div.style.backgroundColor = "brown";
div.style.color = "white";
div.style.fontSize = "15pt";
div.innerHTML = message;
document.body.appendChild(div);
}
/** Adds further reading (former "external links") unless present.
Supports Czech, Slovak, and some other languages.
Does not check whether the target page of the link actually exists.
buttonToPress: wpSave, wpPreview, wpDiff
*/
function addExternalLinks(language, buttonToPress) {
if (language=="") {
divAlert("The language or language code is missing. No action done.");
return; }
dictionaryLink="";
dictionaryLink2="";
if (language=="Czech") {
dictionaryLink = "{{R:PSJC}}";
dictionaryLink2 = "{{R:SSJC}}"; }
if (language=="Malagasy")
dictionaryLink = "{{R:MGW}}";
if (language=="Slovak")
dictionaryLink = "{{R:SDK}}";
if (language=="Polish")
dictionaryLink = "{{R:PWN}}";
if (language=="Romanian")
dictionaryLink = "{{R:DEX}}";
if (language=="Turkish")
dictionaryLink = "{{R:TDK}}";
if (dictionaryLink=="") {
divAlert("Language "+language+" not supported. No action done.");
return; }
var content=document.getElementById("wpTextbox1").value;
var i = content.indexOf("==" + language,0);
if (i<0) {
divAlert("Language section was not found on the page: \""+gloss+"\". No further reading added.");
//console.log("The gloss was not found on the page: \""+gloss+"\". No translation added."); Does not seem to do anything.
return; }
if (content.indexOf(dictionaryLink,0)>=0) {
divAlert("Further reading for "+language+" already there. No action taken.");
return; }
langSectionCategoryBeginning=0;
insertionPointFound=false;
while (i<content.length && !insertionPointFound) {
i=i+1;
fourNext = content.substring(i,i+4);
manyNext = content.substring(i,i+20);
if (fourNext=="----") // looking at the end of a language section
insertionPointFound=true;
if (fourNext.match(/^==[A-Z]/) && content.substring(i-1,i)!="=") // looking at language section without preceding ----
insertionPointFound=true;
if (manyNext.match(/^\[\[[^:\]]*:/) &&
!manyNext.match(/^\[\[File:/) &&
!manyNext.match(/^\[\[Image:/))
// looking at interwiki, like [[ast:word]] or [[es:word]], but not at [[File:image.png]]
insertionPointFound=true;
if (langSectionCategoryBeginning==0 && content.substring(i,i+10)=="[[Category")
langSectionCategoryBeginning=i;
}
//var j = content.indexOf("{{trans-bottom}}",i);
if (langSectionCategoryBeginning!=0)
insertionPoint=langSectionCategoryBeginning;
else
insertionPoint=i;
var part1 = content.substring(0,insertionPoint);
var part2 = content.substring(insertionPoint,content.length);
twoCharsBeforeInsertion = content.substring(insertionPoint-2,insertionPoint);
insertedPart = "===Further reading===\n* " + dictionaryLink;
if (dictionaryLink2!="")
insertedPart += "\n* " + dictionaryLink2;
if (insertionPoint<content.length)
insertedPart+="\n\n";
if (twoCharsBeforeInsertion!="\n\n") {
// Not empty line before insertion point
insertedPart = "\n" + insertedPart; }
document.getElementById("wpTextbox1").value=part1+insertedPart+part2;
var summary = "/* "+ language +" */ Add further reading";
document.getElementById("wpSummary").value=summary;
if (buttonToPress)
document.getElementById(buttonToPress).click();
// buttonToPress: "wpSave", "wpPreview", "wpDiff"
}
function enableAddingExternalLinks() {
if (getURLParameter("task")=="addel")
addExternalLinks(getURLParameter("language"), getURLParameter("buttonToPress"));
// Example URL to add external links, showing edit preview rather than directly saving:
//https://en.wiktionary.org/wiki/beh?action=edit&task=addel&language=Slovak&buttonToPress=wpPreview
}
$(enableAddingExternalLinks);