לדלג לתוכן

משתמש:רובין בוט/ניסוי.js: הבדלים בין גרסאות בדף

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית
אין תקציר עריכה
אין תקציר עריכה
 
(2 גרסאות ביניים של אותו משתמש אינן מוצגות)
שורה 1: שורה 1:
const style = document.createElement('style');
var sidebar = document.createElement('div');
style.textContent = `
sidebar.id = 'hebrewDateSidebar';
   a.redirect-link { color: red; font-weight: bold; text-decoration: underline; cursor: pointer; }
sidebar.style.cssText = `
   #fixAllRedirectsBtn { margin: 10px 0; padding: 4px 10px; font-size: 0.9em; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
   position: fixed;
  top: 100px;
  right: 10px;
  width: 150px;
   background-color: #FFF3E0;
  border: 1px solid #FFA726;
  padding: 10px;
  font-size: 90%;
  z-index: 1000;
`;
`;
document.head.appendChild(style);
document.body.appendChild(sidebar);


const btn = document.createElement('button');
function convertHebrewDate(hebrewDate, callback) {
btn.id = 'fixAllRedirectsBtn';
    const months = {
btn.textContent = 'תקן את כל קישורי ההפניה';
        'תשרי':1,'חשוון':2,'כסלו':3,'טבת':4,
document.body.prepend(btn);
        'שבט':5,'אדר':6,'אדר ב׳':7,'ניסן':8,
 
        'אייר':9,'סיון':10,'תמוז':11,'אב':12,'אלול':13
function markRedirects() {
    };
  const links = Array.from(document.querySelectorAll('a[href^="/wiki/"], a[href^="https://chabadpedia.co.il/wiki/"]'));
    const match = hebrewDate.match(/\[\[([^\]]+)\]\]\s*\[\[([^\]]+)\]\]/);
  links.forEach(link => {
    if (!match) return;
     const title = link.getAttribute('href').split('/wiki/')[1];
    let day = parseInt(match[1].replace(/[^\d]/g,''));
     fetch(`https://chabadpedia.co.il/api.php?action=query&titles=${encodeURIComponent(title)}&format=json&redirects&prop=info`)
    let month = months[match[2].trim()];
      .then(resp => resp.json())
     let year = match[2].includes('תש') ? parseInt('5' + match[2].replace(/[^\d]/g,'')) : parseInt(match[2].replace(/[^\d]/g,''));
      .then(data => {
     fetch(`https://www.hebcal.com/converter?cfg=json&hy=${year}&hm=${month}&hd=${day}&h2g=1`)
        const page = Object.values(data.query.pages)[0];
        .then(resp => resp.json())
        if (page && page.redirect !== undefined) {
        .then(data => {
          link.classList.add('redirect-link');
            callback(data.gy + '-' + data.gm + '-' + data.gd);
          link.title = 'זה קישור הפניה. לחץ על הכפתור למטה כדי לתקן.';
        });
        }
      })
      .catch(err => console.log(err));
  });
}
}


btn.addEventListener('click', function() {
convertHebrewDate('[[י"ח חשוון]] [[תשל"א]]', function(gregDate) {
  const links = Array.from(document.querySelectorAll('a.redirect-link'));
    sidebar.textContent = 'תאריך לועזי: ' + gregDate;
  links.forEach(link => {
    const title = link.getAttribute('href').split('/wiki/')[1];
    fetch(`https://chabadpedia.co.il/api.php?action=query&titles=${encodeURIComponent(title)}&format=json&redirects&prop=info`)
      .then(resp => resp.json())
      .then(data => {
        const page = Object.values(data.query.pages)[0];
        if (page && page.redirect !== undefined && page.title) {
          link.setAttribute('href', '/wiki/' + encodeURIComponent(page.title));
          link.style.color = 'green';
          link.style.fontWeight = 'normal';
          link.classList.remove('redirect-link');
          link.title = 'קישור תוקן בהצלחה!';
        }
      })
      .catch(err => console.log(err));
  });
  alert("כל הקישורים תוקנו!");
});
});
markRedirects();

גרסה אחרונה מ־14:01, 5 בספטמבר 2025

var sidebar = document.createElement('div');
sidebar.id = 'hebrewDateSidebar';
sidebar.style.cssText = `
  position: fixed;
  top: 100px;
  right: 10px;
  width: 150px;
  background-color: #FFF3E0;
  border: 1px solid #FFA726;
  padding: 10px;
  font-size: 90%;
  z-index: 1000;
`;
document.body.appendChild(sidebar);

function convertHebrewDate(hebrewDate, callback) {
    const months = {
        'תשרי':1,'חשוון':2,'כסלו':3,'טבת':4,
        'שבט':5,'אדר':6,'אדר ב׳':7,'ניסן':8,
        'אייר':9,'סיון':10,'תמוז':11,'אב':12,'אלול':13
    };
    const match = hebrewDate.match(/\[\[([^\]]+)\]\]\s*\[\[([^\]]+)\]\]/);
    if (!match) return;
    let day = parseInt(match[1].replace(/[^\d]/g,''));
    let month = months[match[2].trim()];
    let year = match[2].includes('תש') ? parseInt('5' + match[2].replace(/[^\d]/g,'')) : parseInt(match[2].replace(/[^\d]/g,''));
    fetch(`https://www.hebcal.com/converter?cfg=json&hy=${year}&hm=${month}&hd=${day}&h2g=1`)
        .then(resp => resp.json())
        .then(data => {
            callback(data.gy + '-' + data.gm + '-' + data.gd);
        });
}

convertHebrewDate('[[י"ח חשוון]] [[תשל"א]]', function(gregDate) {
    sidebar.textContent = 'תאריך לועזי: ' + gregDate;
});