میدیاویکی:RandomWordOfTheDay.js

لە ویکیفەرھەنگ

تێبینی: لە دوای پاشەکەوت کردن، لەوانەیە بۆ بینینی گۆڕانکارییەکان پێویست بێ کاشی وێبگەڕەکەت پاکبکەیتەوە.

  • Firefox / Safari: دوگمەی Shift بگرە کاتێک لەسەر Reloadدا کرتە دەکەی، یان ھەرکام لە Ctrl-F5 یان Ctrl-R لێبدە (⌘-R لەسەر Mac دا)
  • Google Chrome: دوگمەکانی Ctrl-Shift-R لێبدە (⌘-Shift-R لەسەر Mac دا)
  • Internet Explorer: دوگمەی Ctrl بگرە کاتێک لەسەر Refreshدا کرتە دەکەی، یان Ctrl-F5 لێبدە
  • Opera: لە ڕێگەی Tools → Preferences ەوە cacheەکە بسڕەوە.
/**
 * Script: Random Word of the Day - For [[Wiktionary:Main page]]
 * Description: Fetches a random word from the specified category on Central Kurdish Wiktionary and displays it daily. The word is updated daily at 00:00 according to the Asia/Baghdad time zone.
 * Original author: [[User:Aram]]
 * Version: 1.0.3
 * Last updated: 2023-07-02
 */

// Function to fetch a random word from the specified category on Wiktionary
function getRandomWord() {
  var category = "کوردی";
  var apiUrl = "https://ckb.wiktionary.org/w/api.php";
  var params = {
    action: "query",
    list: "categorymembers",
    cmtitle: "Category:" + category,
    cmprop: "title",
    cmnamespace: "0", // Only fetch pages in the main namespace
    cmlimit: "max",
    format: "json"
  };

  // Fetch a list of category members
  $.getJSON(apiUrl, params, function(data) {
    var pages = data.query.categorymembers;
    var randomPage = pages[Math.floor(Math.random() * pages.length)];
    var randomPageTitle = randomPage.title;

    // Store the random word title in localStorage
    localStorage.setItem("randomWordTitle", randomPageTitle);

    // Display the page title
    $(".randomWordOfTheDayTitle").html(randomPageTitle);

    // Fetch the parsed content of a random page
    $.ajax({
      url: apiUrl,
      data: {
        action: "parse",
        page: randomPageTitle,
        format: "json"
      },
      dataType: "json",
      success: function(data) {
        var randomPageContent = data.parse.text["*"];
        $(".randomWordOfTheDayContent").html(randomPageContent);

        // Store the random word content in localStorage
        localStorage.setItem("randomWordContent", randomPageContent);
      }
    });
  });
}

// Function to update the word daily
function updateWord() {
  var now = new Date();
  var baghdadTimezone = "Asia/Baghdad";

  // Get the current time in the Asia/Baghdad timezone; it is default on this project
  var desiredTime = new Date(now.toLocaleString("en-US", { timeZone: baghdadTimezone }));

  // Set the desired update time to 12:00 AM in the Asia/Baghdad timezone
  desiredTime.setHours(
    0,   // Set the hours to 0 (midnight)
    0,   // Set the minutes to 0
    0,   // Set the seconds to 0
    0    // Set the milliseconds to 0
  );

  // Calculate the time until the desired update time
  var timeUntilUpdate = desiredTime - now;
  if (timeUntilUpdate < 0) {
    // The desired time has already passed today, add 24 hours to set the update for the next day
    timeUntilUpdate += 24 * 60 * 60 * 1000;
  }

  // Delay the initial update until the desired time
  setTimeout(function() {
    getRandomWord();

    // Set the interval to update the word every 24 hours
    setInterval(getRandomWord, 24 * 60 * 60 * 1000);
  }, timeUntilUpdate);
}

// Check if the stored random word is fresh on page load
$(document).ready(function () {
    var storedTitle = localStorage.getItem("randomWordTitle");
    var storedContent = localStorage.getItem("randomWordContent");

    if (storedTitle && storedContent) {
        // Display the stored random word title and content
        $(".randomWordOfTheDayTitle").html(storedTitle);
        $(".randomWordOfTheDayContent").html(storedContent);
    } else {
        // No stored random word, fetch a new one
        getRandomWord();
    }

    // Create the link element for editing the random word
    var editUrl = "";
    var randomWordTitle = localStorage.getItem("randomWordTitle");

    if (randomWordTitle) {
        editUrl = "https://ckb.wiktionary.org/w/index.php?title=" + encodeURIComponent(randomWordTitle) + "&action=edit";
    }

    // Create the anchor tag for editing the random word
    var editLink = $("<a>")
    .attr("href", editUrl)
    .text("دەستکاری")
    .attr("target", "_blank")
    .css({
    	"float": "left",
    	"color": "#999",
    	"font-size": "14px"
    });

    // Append the edit link and the word title to the container element
    $(".randomWordOfTheDayTitle").append(editLink);
    
    // Call the updateWord function to start the update process
    updateWord();
});