User:Kuhlau/CopyTitle.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
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)
Modified even further by: Kuhlau (to add it to vector list)
*/
$(function() {
if (window.CopyTitleLoaded) {return;}
window.CopyTitleLoaded = true;
mw.util.addCSS(`
#title-copy-content ,
#title-copy-all {
color: #0645ad;
cursor: pointer;
}
#title-copy-content:hover ,
#title-copy-all:hover {
text-decoration: underline;
color: #0645ad;
}
`);
function init() {
$('<li>', {
id: 'title-copy-content',
class: 'mw-list-item title-copy-button',
text: 'Copy title'
}).appendTo($('#mw-panel #p-navigation .vector-menu-content-list'));
if (mw.config.get('wgNamespaceNumber') !== 0) {
$('<li>', {
id: 'title-copy-all',
class: 'mw-list-item title-copy-button',
text: 'Copy title (namespace)'
}).appendTo($('#mw-panel #p-navigation .vector-menu-content-list'));
}
$('#mw-panel .vector-menu-content-list #title-copy-content').click(function() {
copy(mw.config.get('wgTitle'), this);
});
$('#mw-panel .vector-menu-content-list #title-copy-all').click(function() {
copy(mw.config.get('wgPageName').replace(/_/g, ' '), this);
});
}
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');
var $el = $(element);
var originalText = $el.text();
$el
.addClass('copied')
.text('Copied!');
setTimeout(function () {
$el
.removeClass('copied')
.text(originalText);
}, 1000);
}
init();
});