MediaWiki:Common.js

From Dariah-Lab
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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

$(function () {
  const namespace = mw.config.get('wgNamespaceNumber');  
  const now = new Date();
  const day = String(now.getDate()).padStart(2, '0');
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const year = now.getFullYear();
  var messageCurrentDate = `${day}.${month}.${year}`;

/* Prepare information about current Namespace Number; namespace_info can be added to the message below and displayed on all of the pages.*/
/* 
  var namespace_info = 'ID obecnego namespace: ' + namespace;
*/

/* Add a message to all of the pages regardless of the Namespace Number.*/
/*
  var message = 'Referując do WikiHum należy używać <b>identyfikatorów Dariah.lab</b> (ang. Dariah.lab. ID).';
  
  $('<div class="global-site-message">' + message + '</div>')
    .prependTo('#content');
*/
    
  if( namespace === 120 ){
    const pageName = mw.config.get('wgPageName');
    const entityId = mw.config.get('wgTitle');
    const propertyId = 'P187'; /*ID of the property "Dariah.lab ID"*/

    var messageLabel = '';
    var messageEdit = '';
    var messageDariahLab = '';

    const userLang = mw.config.get('wgUserLanguage');

    $.getJSON(mw.util.wikiScript('api'), {
      action: 'wbgetentities',
      ids: entityId,
      props: 'labels',
      languages: userLang,
      format: 'json'
    }, function (data) {
      const entity = data.entities[entityId];
      messageLabel = entity.labels[userLang].value;

      /* $('<div class="global-site-message">' + messageLabel + userLang + '</div>').prependTo('#content'); */

    });

    $.getJSON(mw.util.wikiScript('api'), {
      action: 'query',
      prop: 'revisions',
      titles: pageName,
      rvprop: 'timestamp',
      format: 'json'
    }, function (data) {
      var pages = data.query.pages;
      var page = pages[Object.keys(pages)[0]];
      if (page && page.revisions && page.revisions.length > 0) {
        const lastEdit = new Date(page.revisions[0].timestamp);
        const editDay = String(lastEdit.getDate()).padStart(2, '0');
        const editMonth = String(lastEdit.getMonth() + 1).padStart(2, '0');
        const editYear = lastEdit.getFullYear();
        messageEdit = `${editDay}.${editMonth}.${editYear}`;
      }
    });

    $.getJSON(mw.util.wikiScript('api'), {
      action: 'wbgetentities',
      ids: entityId,
      format: 'json',
      props: 'claims'
    }, function (data) {
      const entity = data.entities[entityId];
      const claims = entity && entity.claims && entity.claims[propertyId];
      if (claims && claims.length > 0) {
        var dariahLabValue = claims[0].mainsnak.datavalue.value;
        messageDariahLab = '<a href="http://hdl.handle.net/' + dariahLabValue + '" target="_blank">http://hdl.handle.net/' + dariahLabValue + '</a>';
      }

    const option1 = 'Referując do tego elementu użyj poniższego formatu zapisu (więcej informacji na stronie <a href="https://prunus-208.man.poznan.pl/wiki/Referowanie_do_WikiHum" target="_blank">Referowanie do WikiHum)</a>.';
    const option2 = 'Referując do tego elementu w przypisie lub bibliografii użyj poniższego formatu zapisu (więcej informacji na stronie <a href="https://prunus-208.man.poznan.pl/wiki/Referowanie_do_WikiHum" target="_blank">Referowanie do WikiHum)</a>.';
    const option3 = 'Referując do tego elementu należy użyć poniższego formatu zapisu (więcej informacji na stronie <a href="https://prunus-208.man.poznan.pl/wiki/Referowanie_do_WikiHum" target="_blank">Referowanie do WikiHum)</a>.';
    const option4 = 'Referując do tego elementu w przypisie lub bibliografii należy użyć poniższego formatu zapisu (więcej informacji na stronie <a href="https://prunus-208.man.poznan.pl/wiki/Referowanie_do_WikiHum" target="_blank">Referowanie do WikiHum)</a>.';

    $('<div class="global-site-message">' + option4 + '<br><span id="copy-target">' + messageLabel + ', ' + messageDariahLab + ', [ostatnia edycja strony: ' + messageEdit + ', dostęp: ' + messageCurrentDate + '].</span><button id="copy-button" title="Kopiuj" style="margin-left: 8px; cursor: pointer;">⧉</button></div>').prependTo('#content');
    $('<div class="global-site-message">' + option3 + '<br>' + messageLabel + ', ' + messageDariahLab + ', [ostatnia edycja strony: ' + messageEdit + ', dostęp: ' + messageCurrentDate + '].</div>').prependTo('#content');
    $('<div class="global-site-message">' + option2 + '<br>' + messageLabel + ', ' + messageDariahLab + ', [ostatnia edycja strony: ' + messageEdit + ', dostęp: ' + messageCurrentDate + '].</div>').prependTo('#content');
    $('<div class="global-site-message">' + option1 + '<br>' + messageLabel + ', ' + messageDariahLab + ', [ostatnia edycja strony: ' + messageEdit + ', dostęp: ' + messageCurrentDate + '].</div>').prependTo('#content');
    
    $('#copy-button').on('click', function () {
      const plainTextToCopy = $('#copy-target').text();
      const htmlTextToCopy = $('#copy-target').html();

      const blobText = new Blob([plainTextToCopy], { type: 'text/plain' });
      const blobHTML = new Blob([htmlTextToCopy], { type: 'text/html' });

      const item = new ClipboardItem({
        'text/plain': blobText,
        'text/html': blobHTML
      });
      navigator.clipboard.write([item]).then(() => {
        $(this).text('✔');
        setTimeout(() => $(this).text('⧉'), 1500);
      });
    });


   });
  }
});