User:RoyZuo/PagenameCopier.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
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 makes a button (both on mobile and desktop) to help copy FULLPAGENAME easily, or those of a specific section.
    @Author [[User:Nadeemulhaqmir]]
    @Author [[User:TheAafi]]
    @Author RoyZuo (fork)
*/

(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 pagename';
				case 'notification':
					return 'Pagename is copied. Enjoy!';
			}
			break;
		default:
			switch (key) {
				case 'copy':
					return 'Copy pagename';
				case 'notification':
					return 'Pagename is 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 = 'copy FULLPAGENAME to clipboard';
  a.setAttribute("title",i18n("copy"));
  a.addEventListener('click',function(e){
	if( navigator && navigator.clipboard){
	 navigator.clipboard.writeText(mw.config.get('wgPageName'));
		mediaWiki.notify(i18n("notification"),{type:'info'});
	}
   });
   document.getElementById("firstHeading").appendChild(a);
})();