User:Zackmann08/scripts/copyPageName.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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Zackmann08/scripts/copyPageName.
//<nowiki>
function waitForElement(selector) {
return new Promise(resolve => {
// Check if it already exists
const element = document.querySelector(selector);
if (element) {
return resolve(element);
}
// Observe changes if it doesn't exist yet
const observer = new MutationObserver(mutations => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
observer.disconnect(); // Stop observing once found
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function copyPageName()
{
var dummy = $('<input>').val(mw.config.get('wgPageName').replace(/_/g, ' ')).appendTo('body').select();
document.execCommand('copy');
mw.notify('Page name copied to clipboard');
}
function copyPageTitle()
{
var dummy = $('<input>').val(mw.config.get('wgTitle').replace(/_/g, ' ')).appendTo('body').select();
document.execCommand('copy');
mw.notify('Page title copied to clipboard');
}
mw.loader.using( [ 'oojs-ui-core' ] ).then( function () {
// jQuery(document).ready(function($) {
(async () => {
await waitForElement("#firstHeading");
var first_heading = document.getElementById('firstHeading');
var copyButton = new OO.ui.ButtonWidget({framed: false});
copyButton.setElementId( 'copyPageName' ).setIcon( 'copy' );
copyButton.on( 'click', function () {
copyPageName(0);
});
copyButton.$element.css( 'font-size', '50%' );
copyButton.$element.css( 'vertical-align', 'text-bottom' );
copyButton.$element.css( 'margin-right', '0' );
copyButton.$element.insertBefore( $(first_heading.firstChild) );
if (mw.config.get('wgNamespaceNumber') != 0) {
var copyTitleButton = new OO.ui.ButtonWidget({framed: false});
copyTitleButton.setElementId( 'copyPageTitle' ).setIcon( 'copy' );
copyTitleButton.on( 'click', function () {
copyPageTitle(0);
});
copyTitleButton.$element.css( 'font-size', '50%' );
copyTitleButton.$element.css( 'vertical-align', 'text-bottom' );
copyTitleButton.$element.css( 'margin-right', '0' );
copyTitleButton.$element.insertAfter( $(document.getElementsByClassName('mw-page-title-main')[0]) );
}
})();
});
//</nowiki>