MediaWiki:Common.js: Difference between revisions

From LHDB
Jump to navigation Jump to search
(Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: $(function () { var link = $('<a>') .attr('href', '/wiki/Deklaracja_dostepnosci') .text('Deklaracja dostępności'); $('<li>').append(link).appendTo('#footer-places').parent(); });")
 
No edit summary
Line 6: Line 6:


     $('<li>').append(link).appendTo('#footer-places').parent();  
     $('<li>').append(link).appendTo('#footer-places').parent();  
});
$(function () {
  const namespace = mw.config.get('wgNamespaceNumber');
  if (namespace !== 120) return; // tylko Item (Q)
  const entityId = mw.config.get('wgTitle');
  const pageName = mw.config.get('wgPageName');
  const userLang = mw.config.get('wgUserLanguage');
  const url = window.location.href;
  // 📅 data dostępu
  const now = new Date();
  const messageCurrentDate = `${String(now.getDate()).padStart(2, '0')}.${String(now.getMonth() + 1).padStart(2, '0')}.${now.getFullYear()}`;
  // 🔄 wszystkie requesty naraz (bez błędów timingowych)
  Promise.all([
    // label
    $.getJSON(mw.util.wikiScript('api'), {
      action: 'wbgetentities',
      ids: entityId,
      props: 'labels',
      languages: userLang,
      format: 'json'
    }),
    // ostatnia edycja
    $.getJSON(mw.util.wikiScript('api'), {
      action: 'query',
      prop: 'revisions',
      titles: pageName,
      rvprop: 'timestamp',
      format: 'json'
    })
  ]).then(function ([labelData, revisionData]) {
    // 🏷️ LABEL
    let messageLabel = entityId;
    const entity = labelData.entities[entityId];
    if (entity.labels && entity.labels[userLang]) {
      messageLabel = entity.labels[userLang].value;
    }
    // 🕓 DATA EDYCJI
    let messageEdit = '';
    const pages = revisionData.query.pages;
    const page = pages[Object.keys(pages)[0]];
    if (page?.revisions?.length > 0) {
      const lastEdit = new Date(page.revisions[0].timestamp);
      messageEdit = `${String(lastEdit.getDate()).padStart(2, '0')}.${String(lastEdit.getMonth() + 1).padStart(2, '0')}.${lastEdit.getFullYear()}`;
    }
    // 🧾 FINALNY TEKST
    const final_message = 'Referując do tego elementu użyj:';
    const citationHTML = `
      ${messageLabel},
      <a href="${url}" target="_blank">${url}</a>,
      [ostatnia edycja strony: ${messageEdit}, dostęp: ${messageCurrentDate}].
    `;
    const box = $(`
      <div class="item-message">
        ${final_message}<br>
        <span id="copy-target">${citationHTML}</span>
        <button id="copy-button" title="Kopiuj" style="margin-left:8px;cursor:pointer;">⧉</button>
      </div>
    `);
    $('#content').prepend(box);
    // 📋 kopiowanie (HTML + tekst)
    $('#copy-button').on('click', function () {
      const plainText = $('#copy-target').text();
      const htmlText = $('#copy-target').html();
      const item = new ClipboardItem({
        'text/plain': new Blob([plainText], { type: 'text/plain' }),
        'text/html': new Blob([htmlText], { type: 'text/html' })
      });
      navigator.clipboard.write([item]).then(() => {
        $(this).text('✔');
        setTimeout(() => $(this).text('⧉'), 1500);
      });
    });
  });
});
});

Revision as of 14:12, 23 March 2026

/* Any JavaScript here will be loaded for all users on every page load. */
$(function () {
    var link = $('<a>')
        .attr('href', '/wiki/Deklaracja_dostepnosci')
        .text('Deklaracja dostępności');

    $('<li>').append(link).appendTo('#footer-places').parent(); 
});

$(function () {
  const namespace = mw.config.get('wgNamespaceNumber');

  if (namespace !== 120) return; // tylko Item (Q)

  const entityId = mw.config.get('wgTitle');
  const pageName = mw.config.get('wgPageName');
  const userLang = mw.config.get('wgUserLanguage');

  const url = window.location.href;

  // 📅 data dostępu
  const now = new Date();
  const messageCurrentDate = `${String(now.getDate()).padStart(2, '0')}.${String(now.getMonth() + 1).padStart(2, '0')}.${now.getFullYear()}`;

  // 🔄 wszystkie requesty naraz (bez błędów timingowych)
  Promise.all([

    // label
    $.getJSON(mw.util.wikiScript('api'), {
      action: 'wbgetentities',
      ids: entityId,
      props: 'labels',
      languages: userLang,
      format: 'json'
    }),

    // ostatnia edycja
    $.getJSON(mw.util.wikiScript('api'), {
      action: 'query',
      prop: 'revisions',
      titles: pageName,
      rvprop: 'timestamp',
      format: 'json'
    })

  ]).then(function ([labelData, revisionData]) {

    // 🏷️ LABEL
    let messageLabel = entityId;
    const entity = labelData.entities[entityId];
    if (entity.labels && entity.labels[userLang]) {
      messageLabel = entity.labels[userLang].value;
    }

    // 🕓 DATA EDYCJI
    let messageEdit = '';
    const pages = revisionData.query.pages;
    const page = pages[Object.keys(pages)[0]];

    if (page?.revisions?.length > 0) {
      const lastEdit = new Date(page.revisions[0].timestamp);
      messageEdit = `${String(lastEdit.getDate()).padStart(2, '0')}.${String(lastEdit.getMonth() + 1).padStart(2, '0')}.${lastEdit.getFullYear()}`;
    }

    // 🧾 FINALNY TEKST
    const final_message = 'Referując do tego elementu użyj:';

    const citationHTML = `
      ${messageLabel}, 
      <a href="${url}" target="_blank">${url}</a>, 
      [ostatnia edycja strony: ${messageEdit}, dostęp: ${messageCurrentDate}].
    `;

    const box = $(`
      <div class="item-message">
        ${final_message}<br>
        <span id="copy-target">${citationHTML}</span>
        <button id="copy-button" title="Kopiuj" style="margin-left:8px;cursor:pointer;">⧉</button>
      </div>
    `);

    $('#content').prepend(box);

    // 📋 kopiowanie (HTML + tekst)
    $('#copy-button').on('click', function () {
      const plainText = $('#copy-target').text();
      const htmlText = $('#copy-target').html();

      const item = new ClipboardItem({
        'text/plain': new Blob([plainText], { type: 'text/plain' }),
        'text/html': new Blob([htmlText], { type: 'text/html' })
      });

      navigator.clipboard.write([item]).then(() => {
        $(this).text('✔');
        setTimeout(() => $(this).text('⧉'), 1500);
      });
    });

  });
});