ব্যবহারকারী:খাত্তাব হাসান/খেলাঘর.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
(function() {
  'use strict';

  // Function to calculate age in days, hours, and minutes
  function calculateAge(dateString) {
    const now = new Date();
    const pageDate = new Date(dateString);
    const difference = now - pageDate;

    const days = Math.floor(difference / (1000 * 60 * 60 * 24));
    const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));

    return `${days} days, ${hours} hours, ${minutes} minutes`;
  }

  // Check if the page is in edit mode
  if (document.body.classList.contains('mw-editing')) {
    // Get the page creation date from the edit history tab
    const editHistoryTab = document.querySelector('.mw-edit-history a');
    if (editHistoryTab) {
      const url = editHistoryTab.href;
      const match = url.match(/action=history&rev=\d+/);
      if (match) {
        const revId = match[1];
        const apiUrl = `https://bn.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=timestamp&revids=${revId}&format=json`;

        // Fetch the creation date using the MediaWiki API
        fetch(apiUrl)
          .then(response => response.json())
          .then(data => {
            const pageData = data.query.pages[Object.keys(data.query.pages)[0]];
            const creationDate = pageData.revisions[0].timestamp;

            // Calculate and display the page age
            const age = calculateAge(creationDate);
            const message = document.createElement('p');
            message.textContent = `This page is ${age} old.`;
            message.style.fontWeight = 'bold';
            document.querySelector('.mw-edit-form').prepend(message);
          })
          .catch(error => console.error('Error fetching page data:', error));
      }
    }
  }
})();