לדלג לתוכן

משתמש:רובין בוט/ניסוי.js

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
const style = document.createElement('style');
style.textContent = `
  a.redirect-link { color: red; font-weight: bold; text-decoration: underline; cursor: pointer; }
  #fixAllRedirectsBtn { margin: 10px 0; padding: 4px 10px; font-size: 0.9em; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
`;
document.head.appendChild(style);

const btn = document.createElement('button');
btn.id = 'fixAllRedirectsBtn';
btn.textContent = 'תקן את כל קישורי ההפניה';
document.body.prepend(btn);

function markRedirects() {
  const links = Array.from(document.querySelectorAll('a[href^="/wiki/"], a[href^="https://chabadpedia.co.il/wiki/"]'));
  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) {
          link.classList.add('redirect-link');
          link.title = 'זה קישור הפניה. לחץ על הכפתור למטה כדי לתקן.';
        }
      })
      .catch(err => console.log(err));
  });
}

btn.addEventListener('click', function() {
  const links = Array.from(document.querySelectorAll('a.redirect-link'));
  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();