User:Masssly/copy-DagChar.js

From Wikidata
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.
// This script enables you to copy and paste Dagbani language special characters and digraphs: ɛ, ɣ, ŋ, ɔ, ʒ and ch, gb, kp, ŋm, sh ny.
// To enable this script, put the following line on your common.js User subpage: importScript('User:Masssly/copy-DagChar.js');
// To fork this tool for your language, simply change the characters in the chars array.
// Code inspired by [[User:Abbe98/copy-qid.js]]

const chars = [
  { text: 'ɛ', capitalizedText: 'Ɛ' },
  { text: 'ɣ', capitalizedText: 'Ɣ' },
  { text: 'ŋ', capitalizedText: 'Ŋ' },
  { text: 'ɔ', capitalizedText: 'Ɔ' },
  { text: 'ʒ', capitalizedText: 'Ʒ' },
  { text: 'ch', capitalizedText: 'Ch' },
  { text: 'gb', capitalizedText: 'Gb' },
  { text: 'kp', capitalizedText: 'Kp' },
  { text: 'ŋm', capitalizedText: 'Ŋm' },
  { text: 'sh', capitalizedText: 'Sh' },
  { text: 'ny', capitalizedText: 'Ny' },
];

let isCapitalized = false;

mw.loader.using('oojs-ui-core').done(() => {
  const container = $('<div>').attr('id', 'dag-char-buttons').css({ position: 'fixed', right: '10px', bottom: '10px', zIndex: '1000' }).appendTo('.mw-indicators');
  
  const capsButton = $('<button>').addClass('oo-ui-buttonElement oo-ui-widget oo-ui-widget-enabled oo-ui-buttonWidget oo-ui-buttonWidget-framed').appendTo(container);
  
  updateCapsButtonText();
  
  capsButton.click(() => {
    isCapitalized = !isCapitalized;
    updateCapsButtonText();
    chars.forEach((char, index) => $(`#dag-char-button-${index}`).text(isCapitalized ? char.capitalizedText : char.text));
  });
  
  chars.forEach((char, index) => {
    $('<button>').attr('id', `dag-char-button-${index}`).addClass('oo-ui-buttonElement oo-ui-widget oo-ui-widget-enabled oo-ui-buttonWidget oo-ui-buttonWidget-framed').text(char.text).click(() => {
      navigator.clipboard.writeText(isCapitalized ? char.capitalizedText : char.text).then(
        () => $('<div>').addClass('copy-popup').text('Copied!').css({ width: 80, position: 'fixed', bottom: '10px', right: '10px', textAlign: 'center', backgroundColor: 'rgba(0, 0, 0, 0.7)', color: '#fff', padding: '10px', borderRadius: '5px', zIndex: '1001' }).appendTo('body').fadeIn(1000).fadeOut(1000, () => $(this).remove()),
        () => $('<div>').addClass('copy-popup').text('Copy failed').css({ width: 80 }).appendTo('.mw-indicators').fadeIn(1000).fadeOut(1000, () => $(this).remove())
      );
    }).appendTo(container);
  });
  
  function updateCapsButtonText() {
    capsButton.text(`Caps: ${isCapitalized ? 'On' : 'Off'}`);
  }
});