User:Aafi/PageLinksCopier.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*
    This script produces a clipboard like icon (both on mobile and desktop) to easily help copy article links, or those of a specific section.
    @Author [[User:Nadeemulhaqmir]]
    @Contributors: [[User:Yethrosh]]
    @Maintainer [[User:Aafi]]
*/

(function(){

var wd=document.querySelector("#right-navigation .vector-menu-content-list"); 
var mb=document.querySelector("#p-views");
 if(!(wd || mb))
    return;

function i18n(key) {
	switch (mw.config.get('wgUserLanguage')) {
		case 'en':
			switch (key) {
				case 'copy':
					return 'Copy Link';
				case 'notification':
					return 'The link has been copied. Enjoy!';
			}
			break;
		case 'ur':
			switch (key) {
				case 'copy':
					return 'ربط نقل کریں';
				case 'notification':
					return 'ربط نقل کر لیا گیا!';
			}
			break;
		case 'ar':
			switch (key) {
				case 'copy':
					return 'رانسخ الرابط';
				case 'notification':
					return 'تم نسخ الرابط. استمتع!';
			}
			break;
		case 'mr':
			switch (key) {
				case 'copy':
					return 'लिंक कॉपी करा';
				case 'notification':
					return 'लिंक कॉपी केली गेली आहे.  आनंद घया!!';
			}
			break;
		case 'or':
			switch (key) {
				case 'copy':
					return 'କପି ଲିଙ୍କ୍';
				case 'notification':
					return 'ଲିଙ୍କ୍‌ଟି କପି ହୋଇଗଲା'
			}
			break;
		default:
			switch (key) {
				case 'copy':
					return 'Copy Link';
				case 'notification':
					return 'The link has been copied. Enjoy!';
			}
			break;
	}
	return key;
}

 var a=document.createElement("a");
  a.href="javascript:;";
  a.setAttribute("class",wd?'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only':'  mw-ui-button mw-ui-quiet');
  a.innerHTML = "<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Copy-outlined-circular-button.svg/28px-Copy-outlined-circular-button.svg.png' />";
  a.setAttribute("title",i18n("copy"));
  a.addEventListener('click',function(e){
	if( navigator && navigator.clipboard){
	 navigator.clipboard.writeText(decodeURI(location.href));
		mediaWiki.notify(i18n("notification"),{type:'info'});
	}
   });	
  // clipboard icon placement on all special pages
	if (mw.config.get('wgIsProbablyEditable') === false) {
		document.getElementById("firstHeading").appendChild(a); 
		}
	else {
		var deskSite = document.querySelector("#right-navigation .vector-menu-content-list"); 
		var mobSite = document.querySelector("#p-views");
		var li = document.createElement("li");
		li.setAttribute("class",deskSite?'mw-list-item':'page-actions-menu__list-item');
		li.appendChild(a);
		deskSite?deskSite.append(li):mobSite.insertBefore(li, mobSite.children[1]); // next to #p-lang in mobile version
	}
})();