User:Kuhlau/CopyTitle.js

/*	Appends two buttons to the edit tools to copy the title with/without namespace Author: Caburum & Thundercraft5 Based on code by: RheingoldRiver See also: w:c:dev:CopyTitle (the original script this was from before being modified) Modified by: Kuhlau Kuhlau's notes: This script adds a little button to the side of the page you can use to copy the real title of the current page (not the displayed title). Modified further by: Mikevoir (to add lightbox usage and other cleanup)

$(function {	if (window.CopyTitleLoaded) {return;}	window.CopyTitleLoaded = true;

function init(i18n) { function getSVG(id, ns) { return '' + ' ' + i18n.msg(id).escape + ' ' + '<path d="M1280 384L1280 384c-40 0-82.7 13.3-128 40V96l0 0c0-26.7-9.3-49.3-28-68s-41.3-28-68-28H640l0 0c-26.7 0-56 6.7-88 20 s-57.3 29.3-76 48L68 476l0 0c-18.7 18.7-34.7 44-48 76S0 613.3 0 640v672l0 0c0 26.7 9.3 49.3 28 68s41.3 28 68 28h544v288l0 0 c0 26.7 9.3 49.3 28 68s41.3 28 68 28h960l0 0c26.7 0 49.3-9.3 68-28s28-41.3 28-68V480l0 0c0-26.7-9.3-49.3-28-68s-41.3-28-68-28 H1280z M1152 896H853l299-299V896z M512 512H213l299-299V512z M708 860c-18.7 18.7-34.7 44-48 76s-20 61.3-20 88v256H128V640h416 l0 0c26.7 0 49.3-9.3 68-28s28-41.3 28-68V128h384v416L708 860L708 860z M768 1664v-640h416l0 0c26.7 0 49.3-9.3 68-28 s28-41.3 28-68V512h384v1152H768z" />' + (ns ? 'NS ' : '') + // i18n could be possible but would need to fit the text properly ' ';		}

mw.util.addCSS('\n'+			'#title-copy-container svg:hover { filter: brightness(1.75); }\n'+			'#title-copy-container svg {\n'+				'flex-basis: 50%;\n'+				'transition: fill 250ms;\n'+				'fill: currentColor;\n'+			'}\n'+			'body:is(.action-edit, .action-submit) #title-copy-container { flex-direction: row; }\n'+			'.lightboxContainer .LightboxHeader h1 #title-copy-container {\n'+				'flex-direction: row;\n'+				'display: inline-flex;\n'+				'margin-left: 4px;\n'+				'height: 28px;\n'+				'width: 28px;\n'+			'}\n'+			'body:is(.action-edit, .action-submit) #title-copy-container > :first-of-type { margin-right: 5px; }\n'+			'body:is(.action-edit, .action-submit) #title-copy-container svg { height: 1em; }\n'+			'.LightboxModal .LightboxHeader h1 { overflow: visible; white-space: normal; max-width: auto; }'		);

$(' ', { id: 'title-copy-container', 'class': 'page-side-tool' }) .append(getSVG('title-copy-content', false)) .appendTo($('.page-side-tools')); if (mw.config.get('wgNamespaceNumber') !== 0) { $(' ', { id: 'title-copy-container', 'class': 'page-side-tool' }) .append(getSVG('title-copy-all', true)) .appendTo($('.page-side-tools')); }		$('.page-side-tools #title-copy-content').click(function {			copy(mw.config.get('wgTitle'), this);		});

$('.page-side-tools #title-copy-all').click(function {			copy(mw.config.get('wgPageName').replace(/_/g, ' '), this);		}); var fileBox = new MutationObserver(function (mutations, me) {			mutations.forEach(function(mutation){ if ( mutation && mutation.removedNodes ) { mutation.removedNodes.forEach(function(removedNode){						if (removedNode.id && removedNode.id == 'LightboxPreload'){							var fileName = document.querySelector('.lightboxContainer .LightboxHeader h1 a').href.replace(/^.*?\/wiki\/File:/, '').replace(/_/g, ' ');							$(' ', { id: 'title-copy-container', 'class': 'page-side-tool' })								.append(getSVG('title-copy-content', false))								.appendTo($('.lightboxContainer .LightboxHeader h1'));							$(' ', { id: 'title-copy-container' , 'class': 'page-side-tool' })								.append(getSVG('title-copy-all', true))								.appendTo($('.lightboxContainer .LightboxHeader h1'));							$('.lightboxContainer .LightboxHeader h1 #title-copy-content').click(function { copy(decodeURIComponent(fileName), this); });							$('.lightboxContainer .LightboxHeader h1 #title-copy-all').click(function { copy('File:'+decodeURIComponent(fileName), this); });						}					});				}			});		});		fileBox.observe(document.body, { childList: true }); }

function copy(text, element) { var copyEl = document.createElement('textarea'); copyEl.value = text; document.body.appendChild(copyEl); copyEl.select; document.execCommand('copy'); document.body.removeChild(copyEl); document.execCommand('copy');

$(element).css('fill', 'var(--theme-success-color)'); setTimeout(function {			$(element).css('fill', '');		}, 1000); }

mw.hook('dev.i18n').add(function {		window.dev.i18n.loadMessages('CopyTitle').then(init);	});

importArticles({		type: 'script',		article: 'u:dev:MediaWiki:I18n-js/code.js'	}); });