mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-23 22:13:38 +00:00
0c3786c754
Instead of doing it in JS, just render it altogther in the HTML
51 lines
1.3 KiB
JavaScript
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
|
|
};
|