mediawiki-skins-Citizen/resources/skins.citizen.scripts/share.js
alistair3149 0c3786c754
perf(share): ️ prerender the HTML for the share button
Instead of doing it in JS, just render it altogther in the HTML
2024-11-12 22:36:59 -05:00

51 lines
1.3 KiB
JavaScript

/**
* Initializes the share button functionality for Citizen
*
* @return {void}
*/
function init() {
if ( !mw.config.get( 'wgIsArticle' ) ) {
return;
}
const shareButton = document.getElementById( 'citizen-share' );
if ( !shareButton ) {
mw.log.error( '[Citizen] Unable to find share button (#shareButton not found)' );
return;
}
const canonicalLink = document.querySelector( 'link[rel="canonical"]' );
const url = canonicalLink ? canonicalLink.href : window.location.href;
const shareData = {
title: document.title,
url: url
};
// eslint-disable-next-line es-x/no-async-functions
const handleShareButtonClick = async () => {
shareButton.disabled = true; // Disable the button
try {
if ( navigator.share ) {
await navigator.share( shareData );
} else if ( navigator.clipboard ) {
// Fallback to navigator.clipboard if Share API is not supported
await navigator.clipboard.writeText( url );
mw.notify( mw.msg( 'citizen-share-copied' ), {
tag: 'citizen-share',
type: 'success'
} );
}
} catch ( error ) {
mw.log.error( `[Citizen] ${ error }` );
} finally {
shareButton.disabled = false; // Re-enable button after error or share completes
}
};
shareButton.addEventListener( 'click', mw.util.debounce( handleShareButtonClick, 100 ) );
}
module.exports = {
init: init
};