User:Nardog/CopySectLink.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Nardog/CopySectLink. |
mw.config.get('wgNamespaceNumber') >= 0 &&
$.when($.ready, mw.loader.using('mediawiki.util')).then(function copySectLink() {
let handler = function (e) {
e.preventDefault();
let text = (mw.config.get('wgPageName') + (this.hash ? decodeURI(this.hash).replace(
/[\[\]\{\|\}]/g,
s => '&#' + s.codePointAt(0) + ';'
) : '')).replace(/_/g, ' ');
navigator.clipboard.writeText(text).then(() => {
mw.notify(`Copied "${text}"`);
}, () => {
let $input = $('<input>').attr({
type: 'text',
value: text,
readonly: '',
style: 'position:fixed;top:-100%'
}).appendTo(document.body);
$input[0].select();
document.execCommand('copy');
$input.remove();
mw.notify(`Probably copied "${text}"`);
});
};
let css;
switch (mw.config.get('skin')) {
case 'minerva':
css = '.collapsible-heading:not(.open-block) .copysectlink{visibility:hidden} .mw-editsection{white-space:nowrap}';
break;
case 'timeless':
css = '.copysectlink{background-color:#fff;margin-left:-1em} .copysectlink:first-of-type{margin-left:-20px;padding-left:0} .copysectlink::before{content:"";display:inline-block;width:16px;height:16px;background-size:16px 16px;vertical-align:bottom;opacity:0.5}';
}
if (css) {
mw.loader.addStyleTag(`.copysectlink::before{background-image:url("data:image/svg+xml,%3Csvg width='20' height='20' version='1.1' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%2354595d'%3E%3Cpath d='M16 0H5v2h11v14h2V2a2 2 0 0 0-2-2z'/%3E%3Cpath d='m4 20h9c1.1 0 2-0.9 2-2v-13c0-1.1-0.9-2-2-2h-9c-1.1 0-2 0.9-2 2v13c0 1.1 0.9 2 2 2zm0-15h9v13h-9z'/%3E%3C/g%3E%3C/svg%3E")} ${css}`);
}
let addButton = function () {
let isFirst = this.id === 'firstHeading';
let hn = isFirst ? this : this.parentElement;
if (hn.querySelector('.copysectlink')) return;
let $button = $('<a>').attr({
class: 'copysectlink',
href: mw.util.getUrl() + (isFirst ? '' : '#' + this.id),
role: 'button'
}).text('copy').click(handler);
if (mw.config.get('skin') === 'minerva') {
$button.addClass('mw-ui-icon mw-ui-icon-element').attr('title', 'Copy');
let wrapper = hn.querySelector('.mw-editsection');
if (wrapper) {
$button.prependTo(wrapper);
} else {
$button.appendTo(hn);
}
} else {
let bracket = hn.querySelector('.mw-editsection-bracket:last-child');
if (bracket) {
bracket.insertAdjacentText('beforebegin', ' | ');
} else {
bracket = $('<span>').addClass('mw-editsection-bracket').text(']')[0];
$('<span>').addClass('mw-editsection').append(
$('<span>').addClass('mw-editsection-bracket').text('['),
bracket
).appendTo(hn);
}
$button.insertBefore(bracket);
}
};
let waitFor = ['ext.gadget.edittop', 'ext.gadget.edit0'].filter(m => {
let state = mw.loader.getState(m);
return state && state!== 'registered';
});
mw.loader.using(waitFor, () => {
addButton.call(document.getElementById('firstHeading'));
});
if (mw.config.get('wgAction') !== 'view') return;
mw.hook('wikipage.content').add($content => {
$content.find('.mw-headline').each(addButton);
});
});