Gebruiker:Freestyle/vector.js

Uit Wikikids
< Gebruiker:Freestyle
Versie door Freestyle (overleg | bijdragen) op 17 feb 2025 om 10:22 (update userscript)
Naar navigatie springen Naar zoeken springen

Let op! Nadat je de veranderingen hebt opgeslagen, moet je de cache van je browser nog legen om ze daadwerkelijk te zien.

Mozilla (incl. Firefox) ctrl-shift-r
IE ctrl-f5
Opera f5
Safari cmd-r
Konqueror f5
// Script op basis van [[:en:User:Enterprisey/watchlist-notice.js]] 
// Specifieke pagina om in de gaten te houden (moet wel op de volglijst staan):
const pageToWatch = "";
const meldingMinuten = 120; // minuten waarbinnen een edit moet zijn gedaan om nog een melding te geven.
const currentPageName = mw.config.get("wgPageName").replace(/_/g, ' ');
// Volglijst update-notice tonen aan of uit?
const volglijstNotice = true;
var showPTWNotice = false;

mw.loader.using("mediawiki.util", function () {
  var showNotice = function (bewerking, isAnonymous = false) {
    // Stick a notice after the notification count
    $("#pt-notifications-notice").after(
      $("<li>")
        .append(
          $("<a>")
            .text(bewerking.title + " geüpdatet")
            .css({
              "background-color": isAnonymous ? "#bb3333" : "#3385ff",
              color: "white",
              "border-radius": "2px",
              padding: "0.25em 0.45em 0.2em",
              cursor: "pointer",
              "font-weight": "bold",
              transition: "background-color 0.5s",
            })
            .attr("href", "/wiki/Special:" + (isAnonymous ? "RecentChanges" : "Watchlist"))
            .mouseover(isAnonymous ? null : updateNotice)
        )
        .attr("id", isAnonymous ? "anon-update-notice" : "watchlist-update-notice")
    );

    var commentString = bewerking.comment.replace("/* ", "");
    var bewerkingArray = commentString.split(' */');
    var tooltip = isAnonymous ? '' : ('➝ ' + bewerkingArray[0]);
    if (bewerkingArray[1] && !isAnonymous) {
      tooltip += ': ' + bewerkingArray[1];
    }
    
    var kop = bewerkingArray[0];
    if (kop.substr(0, 22) == 'Nieuw kopje aangemaakt') {
      kop = bewerkingArray[0].substr(24, bewerkingArray[0].length);
    }

    // Add diff link (*)
    $("#pt-mytalk").before(
      $("<li>")
        .append(
          $("<a>")
            .text("*")
            .css({
              color: isAnonymous ? "#bb3333" : "#555555",
              "border-radius": "2px",
              padding: "0.25em 0.45em 0.2em",
              cursor: "pointer",
              "font-weight": "bold",
            })
            .attr(
              "href",
              "https://wikikids.nl/index.php?title=" +
                encodeURI(bewerking.title.replace(/ /g,"_")) +
                "&diff=" +
                bewerking.revid +
                (isAnonymous ? "" : "&volglijstmelder=true")
            )
        )
        .attr("id", isAnonymous ? "anon-update-diff" : "watchlist-update-diff")
        .attr(
          "title",
          bewerking.type + " door " + bewerking.user + " (" + tooltip + ")"
        )
    );

    // Add section link (→)
    $("#pt-mytalk").before(
      $("<li>")
        .append(
          $("<a>")
            .text("→")
            .css({
              color: isAnonymous ? "#bb3333" : "#555555",
              "border-radius": "2px",
              padding: "0.25em 0.45em 0.2em",
              cursor: "pointer",
              "font-weight": "bold",
            })
            .attr(
              "href",
              "https://wikikids.nl/index.php?title=" + 
                encodeURI(bewerking.title.replace(/ /g,"_")) + 
                (kop ? "#" + kop.replace(/ /g,"_") : "")
            )
        )
        .attr("id", isAnonymous ? "anon-update-link" : "watchlist-update-link")
        .attr("title", isAnonymous ? "" : tooltip)
    );
  };

  var checkRecentAnonymousEdits = function() {
    $.getJSON(mw.util.wikiScript("api"), {
      format: "json",
      action: "query",
      list: "recentchanges",
      rcprop: "title|ids|timestamp|comment|user",
      rcanon: true,
      rclimit: 1
    }).done(function(data) {
      if (!data.query || !data.query.recentchanges.length) return;
      
      const anonEdit = data.query.recentchanges[0];
      showNotice(anonEdit, true);
    });
  };

  var updateNotice = function () {
    // Lighten the background color so the user knows we're updating
    $("#watchlist-update-notice a").css("background-color", "#7bff7b");

    // Remove any existing anonymous edit notifications
    $("#anon-update-notice, #anon-update-diff, #anon-update-link").remove();

    // Update the notice
    var aantalEdits = 1;
    if (pageToWatch) {
      aantalEdits = 5;
    }
    
    $.getJSON(mw.util.wikiScript("api"), {
      format: "json",
      action: "query",
      list: "watchlist",
      wlprop: "comment|title|user|timestamp",
      wlshow: "unread",
      wllimit: aantalEdits,
    }).done(function (data) {
      if (!data.query) return;
      if (data.query.watchlist.length) {
        // There are new watchlist diffs to read, so show notice
        var minuten = Math.round((Date.now() - Date.parse(data.query.watchlist[0].timestamp)) / 60000);
        if (
          data.query.watchlist[0].title == pageToWatch &&
          data.query.watchlist[0].user !== "Nlwikibots" && minuten <= meldingMinuten
        ) {
          showPTWNotice = true;
          document.getElementById("siteNotice").innerHTML =
            '<div style="width:100%; margin: 0 auto; padding:.5em; background-color:#ffb84d;">De pagina <strong><a href="' +
            "https://wikikids.nl/wiki/" +
            encodeURI(pageToWatch.replace(/ /g,"_")) +
            '">' +
            pageToWatch +
            '</a></strong> is onlangs gewijzigd door <a href = "https://wikikids.nl/Overleg_gebruiker:' +
            encodeURI(data.query.watchlist[0].user) +
            '">' +
            data.query.watchlist[0].user +
            '</a>. (<a href="https://wikikids.nl/index.php?title=' +
            encodeURI(pageToWatch.replace(/ /g,"_")) +
            "&curid=" +
            data.query.watchlist[0].pageid +
            "&diff=" +
            data.query.watchlist[0].revid +
            '">diff</a>)</div>';
        }
        if (!$("#watchlist-update-notice").length) {
          // That is, if it isn't shown already
          if (volglijstNotice) {
            showNotice(data.query.watchlist[0]);
          }
        } else {
          // There's already a notice, so change background
          $("#watchlist-update-notice a").css("background-color", "#3385ff");
        }
      } else {
        // No new watchlist diffs to read, so hide notice, diff and link
        $("#watchlist-update-notice, #watchlist-update-diff, #watchlist-update-link").remove();
        if (pageToWatch === currentPageName) {
          $("#siteNotice").remove();
          showPTWNotice = false;
        }
        // Check for anonymous edits when there are no watchlist updates
        checkRecentAnonymousEdits();
      }
    });
  };

  $(document).ready(function () {
    updateNotice();
    window.setInterval(updateNotice, 120000);
  });
});
Afkomstig van Wikikids , de interactieve Nederlandstalige Internet-encyclopedie voor en door kinderen. "https://wikikids.nl/index.php?title=Gebruiker:Freestyle/vector.js&oldid=920375"