User:Aafi/InterwikiLinksCopier.js
(Redirected from User:TheAafi/InterwikiLinksCopier.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.
![]() |
Documentation for this user script can be added at User:Aafi/InterwikiLinksCopier. |
/*
This script produces a text "COPY" next to each title (desktop view), and a clipboard icon (mobile view), to easily help copy article titles for interwiki linking purposes, or those of a specific section.
@Author [[User:Nardog]]
@Fork by: [[User:Aafi]]
*/
(function copySectLink() {
let dependencies = ['mediawiki.util'];
let classes = 'copysectlink';
let css;
switch (mw.config.get('skin')) {
case 'minerva':
classes += ' cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet';
css = '.copysectlink{opacity:0.65;font-size:1rem;margin-right:0 !important} .collapsible-heading:not(.open-block) .copysectlink{visibility:hidden} .copysectlink > span + span{display:block;position:absolute;clip:rect(1px,1px,1px,1px);width:1px;height:1px;margin:-1px;border:0;padding:0;overflow:hidden}';
break;
case 'timeless':
classes += ' mw-ui-icon-copy';
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:20px;height:20px;background-size:20px 20px;vertical-align:bottom;opacity:0.33}';
}
if (css) {
mw.loader.addStyleTag(css);
dependencies.push('oojs-ui.styles.icons-editing-advanced');
}
let handler = function (e) {
e.preventDefault();
e.stopPropagation();
let text = (mw.config.get('wgPageName') + (this.hash ? decodeURI(this.hash).replace(
/[\[\]{|}]/g,
s => '&#' + s.codePointAt(0) + ';'
) : '')).replace(/_/g, ' ');
let $input = $('<input>').attr({
type: 'text',
readonly: '',
style: 'position:fixed;top:-100%'
}).val(text).appendTo(document.body);
$input[0].select();
let copied;
try {
copied = document.execCommand('copy');
} catch (e) {}
$input.remove();
if (copied) {
mw.notify(`Copied "${text}"`);
} else {
mw.notify('Something went wrong. Please try again!', { type: 'error' });
}
};
let addButton = (block, id) => {
if (block.querySelector('.copysectlink')) return;
let $button = $('<a>').attr({
class: classes + (id ? ' mw-selflink-fragment' : ''),
href: mw.util.getUrl() + (id ? '#' + encodeURI(id): ''),
role: 'button'
}).text('copy').on('click', handler);
if (mw.config.get('skin') === 'minerva') {
$button.attr('title', 'Copy').wrapInner('<span>')
.prepend($('<span>').addClass('minerva-icon oo-ui-icon-copy'));
let wrapper = block.querySelector('.mw-editsection');
if (wrapper) {
$button.prependTo(wrapper);
} else {
$button.appendTo(block);
}
} else {
let bracket = block.querySelector('.mw-editsection-bracket:last-child');
if (bracket) {
bracket.before(' | ', $button[0]);
} else {
$('<span>').addClass('mw-editsection').append(
$('<span>').addClass('mw-editsection-bracket').text('['),
$button,
$('<span>').addClass('mw-editsection-bracket').text(']')
).appendTo(block);
}
}
};
if (['view', 'purge'].includes(mw.config.get('wgAction'))) {
['ext.gadget.edittop', 'ext.gadget.edit0'].forEach(m => {
let state = mw.loader.getState(m);
if (state && state !== 'registered') {
dependencies.push(m);
}
});
}
$.when($.ready, mw.loader.using(dependencies)).then(() => {
if (mw.config.get('wgNamespaceNumber') >= 0) {
addButton(document.getElementById('firstHeading'));
}
mw.hook('wikipage.content').add($content => {
$content.find(
':is(h1, h2, h3, h4, h5, h6) > .mw-headline[id], .mw-heading > :is(h1, h2, h3, h4, h5, h6)[id]'
).each(function () {
addButton(this.parentElement, this.id);
});
if (mw.config.get('wgNamespaceNumber') &&
mw.config.get('wgAction') === 'view'
) {
$content.find('h2:not(#mw-toc-heading, .mw-heading > h2)[id]').each(function () {
addButton(this, this.id);
});
}
});
});
}());