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

מתוך חב"דציטוט, מאגר ציטוטים חב"דים
קפיצה לניווט קפיצה לחיפוש
(תיקונים)
אין תקציר עריכה
שורה 1: שורה 1:
mw.loader.using(["jquery.spinner", "mediawiki.diff.styles", "mediawiki.api"]).then(() => {
(function() {
    // כתובת API של חב"דפדיה
    const API = "https://chabadpedia.co.il/api.php";


  // פונקציה לשליפת הפניות מחב"דפדיה
    // פונקציה לבדיקת האם קיים ערך
  function getAll() {
    function checkPage(title) {
    var PageNameLocal = mw.config.get("wgPageName");
        return fetch(`${API}?action=query&format=json&titles=${encodeURIComponent(title)}&origin=*`)
            .then(res => {
                if (!res.ok) throw new Error("Network response was not ok");
                return res.json();
            })
            .then(data => {
                const pages = data.query.pages;
                const pageId = Object.keys(pages)[0];
                return pageId !== "-1"; // מחזיר true אם הערך קיים
            })
            .catch(err => {
                console.error("שגיאה בקריאת API:", err);
                return false;
            });
    }


     var checkParams = {
     // פונקציה שמוסיפה כפתור ב־UI
      action: "query",
    function addButton() {
      format: "json",
        const btn = document.createElement("button");
      prop: "linkshere",
        btn.innerText = "בדוק בחב״דפדיה";
      indexpageids: 1,
        btn.style.margin = "5px";
      lhprop: "title",
        btn.onclick = async () => {
      lhshow: "redirect",
            const title = prompt("איזה ערך לחפש?");
      lhnamespace: "0",
            if (!title) return;
      lhlimit: "max",
            const exists = await checkPage(title);
      titles: decodeURIComponent(PageNameLocal),
            if (exists) {
      origin: "*",
                alert(`הערך "${title}" קיים בחב״דפדיה ✅`);
     };
            } else {
                alert(`הערך "${title}" לא נמצא ❌`);
            }
        };
        document.body.appendChild(btn);
     }


     var apiEndpoint = "https://chabadpedia.co.il/api.php";
     // הוספת הכפתור לדף
 
     addButton();
    $.ajax({
})();
      url: apiEndpoint,
      data: checkParams,
      dataType: "json",
    })
    .done(function (result) {
      if (!result || !result.query) return;
 
      var pages = result.query.pages[result.query.pageids[0]];
      if (!pages.linkshere) return;
 
      for (var l of pages.linkshere) {
        var oneRedirect = l.title;
        checkChabadTzitut(oneRedirect);
      }
    });
  }
 
  // בדיקה אם ההפניה קיימת בציטוט
  function checkChabadTzitut(oneRedirect) {
    fetch(
      "https://zitut.chabadpedia.com/api.php?action=query&prop=info|revisions&titles=" +
        encodeURIComponent(oneRedirect) +
        "&indexpageids=true&rvlimit=1&rvprop=size|ids&format=json&origin=*"
    )
    .then((response) => response.json())
    .then(function (result) {
      if (result && result.query) {
        if (result.query.pageids[0] == "-1") {
          // לא קיים בציטוט → מייבא
          saveChabadTzitut(oneRedirect);
        }
      }
    });
  }
 
  // שמירה בציטוט
  function saveChabadTzitut(page) {
    var apiEndpoint =
      "https://chabadpedia.com/w/api.php?action=parse&page=" +
      encodeURIComponent(page) +
      "&format=json&prop=wikitext&origin=*";
 
     fetch(apiEndpoint)
      .then((response) => response.json())
      .then(function (result) {
        if (result && result.parse) {
          var textpage = result.parse.wikitext["*"];
          var red = decodeURIComponent(result.parse.title);
 
          if (confirm("ההפניה הבאה קיימת בחב\"דפדיה:\n " + red + "\nלייבא לחב\"דציטוט?")) {
            $.post(
              "https://zitut.chabadpedia.com/w/api.php",
              {
                action: "edit",
                format: "json",
                tags: "ייבוא-הפניות",
                bot: 1,
                watchlist: "nochange",
                recreate: false,
                title: result.parse.title,
                text: textpage,
                token: mw.user.tokens.get("csrfToken"),
              },
              function (data) {
                if (data && data.edit) {
                  mw.notify("ייבוא ההפניה \"" + decodeURIComponent(data.edit["title"]) + "\" הושלם בהצלחה");
                } else {
                  console.log(data);
                }
              }
            );
          }
        }
      })
      .catch((error) =>
        mw.notify("אירעה שגיאה בייבוא ההפניה. תיאור השגיאה הוא: " + error)
      );
  }
 
  // הפעלה
  if (
    mw.config.get("wgArticleId") > 0 &&
    mw.config.get("wgNamespaceNumber") === 0 &&
    mw.config.get("wgPageName") != "עמוד_ראשי" &&
    mw.config.get("wgAction") === "view" &&
    location.href.indexOf("&diff") == -1
  ) {
    getAll();
  }
 
});

גרסה מ־09:47, 18 באוגוסט 2025

(function() {
    // כתובת API של חב"דפדיה
    const API = "https://chabadpedia.co.il/api.php";

    // פונקציה לבדיקת האם קיים ערך
    function checkPage(title) {
        return fetch(`${API}?action=query&format=json&titles=${encodeURIComponent(title)}&origin=*`)
            .then(res => {
                if (!res.ok) throw new Error("Network response was not ok");
                return res.json();
            })
            .then(data => {
                const pages = data.query.pages;
                const pageId = Object.keys(pages)[0];
                return pageId !== "-1"; // מחזיר true אם הערך קיים
            })
            .catch(err => {
                console.error("שגיאה בקריאת API:", err);
                return false;
            });
    }

    // פונקציה שמוסיפה כפתור ב־UI
    function addButton() {
        const btn = document.createElement("button");
        btn.innerText = "בדוק בחב״דפדיה";
        btn.style.margin = "5px";
        btn.onclick = async () => {
            const title = prompt("איזה ערך לחפש?");
            if (!title) return;
            const exists = await checkPage(title);
            if (exists) {
                alert(`הערך "${title}" קיים בחב״דפדיה ✅`);
            } else {
                alert(`הערך "${title}" לא נמצא ❌`);
            }
        };
        document.body.appendChild(btn);
    }

    // הוספת הכפתור לדף
    addButton();
})();