User:Cloned/User-Javascript

From Guild Wars 2 Wiki
Jump to navigationJump to search

This is my Greasemonkey-Script for a copy-paste interwiki box on every page. It even adds the summary "Interwiki" to the page.

The interwiki-links on each page are modified to open in a new tab. I find it better that way, especially if i add many interwiki links.

It uses the GM_setClipboard function which automatically copies the interwiki-link to the clipboard, this is not possible with plain javascript. So if you want to use this code as userscript, remove the line with this function.

Script also adds the text "Original Uploader in the german-wiki: " to image upload field (and to the german wiki the same text in german)

This is not my full Javascript, if something is not working because of variables I forgot to copy here or something else, let me know.

var lang = {},
    txtSummary = "Interwiki";
if (location.hostname.indexOf('wiki.guildwars2') != -1) {lang.current = "en";lang.other="de";}
else {lang.current = "de";lang.other="en";}

/*functions*/

//Add Text field
function createTxtfield(){
    var langLink = document.createTextNode("[["+lang.current+":"+ document.getElementById("firstHeading").innerHTML + "]]"),
	txt = document.createElement("textarea");
	txt.value += langLink.data;
	txt.onclick = function(e){
		this.select();
        GM_setClipboard(this.value.substring(this.selectionStart,this.selectionEnd) );
	};
	txt.rows = "5"; //why 5? because then it's not too large or too small for me. Modify as you like.

    if (document.getElementById("p-lang") ){
		x = document.getElementById("p-lang").getElementsByTagName("LI");
		//i = x.length;
		for (let i = 0; i < x.length; i++){
			if (x[i].className != "interlanguage-link interwiki-"+lang.other){
				langText = decodeURIComponent (x[i].firstChild.href.split("/wiki/")[1].replace(/_/g," ") ) ;
				langLink = document.createTextNode("[[" + x[i].className.substr(29)+":" + langText + "]]" );
				txt.value += "\n" + langLink.data;
			}
		}
	}

    document.getElementById("content").insertBefore(txt,document.getElementById('firstHeading'));

    //Set interwiki-links to target _blank.
    if (document.getElementById("p-lang")){
		var As = document.getElementById("p-lang").getElementsByTagName("A"), i = As.length;
		while (i--){
			As[i].setAttribute("target","_blank");
		}
	}
}

if (document.getElementById("wpSummary") && txtSummary !== '' ) fillFields(txtSummary);

/*
***ADD "Original Uploader in the german-wiki" TO IMAGE UPLOAD FIELD DESCRIPTION***
*/

if (document.getElementById('wpUploadDescription')){
    if (lang.other == "de"){
        document.getElementById('wpUploadDescription').value = 'Original Uploader in the german-wiki: ' ; }
    else{
        document.getElementById('wpUploadDescription').value = 'Original Uploader im '+lang.other+'-wiki: ' ; }
}