Jump to content

🚨 Get your Google Knowledge Panel
◉ Displays your name, photo, and profession in Google Search
Click here to get started now

MediaWiki:Common.js: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 158: Line 158:
     if (mw.config.get('wgNamespaceNumber') === 0 && mw.config.get('wgAction') === 'view') {
     if (mw.config.get('wgNamespaceNumber') === 0 && mw.config.get('wgAction') === 'view') {
         const pageTitle = mw.config.get('wgPageName');
         const pageTitle = mw.config.get('wgPageName');
         const protection = mw.config.get('wgRestrictionEdit');
         const api = new mw.Api();


         // ✅ Proceed only if the page is not protected
         // Step 1: First check if page is protected
         if (!protection || protection.length === 0) {
         api.get({
            const api = new mw.Api();
            action: 'query',
            api.get({
            titles: pageTitle,
                action: 'query',
            prop: 'info|revisions',
                prop: 'revisions',
            inprop: 'protection',
                titles: pageTitle,
            rvprop: 'content',
                rvprop: 'content',
            formatversion: 2
                formatversion: 2
        }).done(function (data) {
            }).done(function (data) {
            const page = data.query.pages[0];
                const content = data.query.pages[0].revisions[0].content;


                const hasReferences = /<ref>|{{cite/i.test(content);
            const isProtected = page.protection && page.protection.some(p => p.type === 'edit');
                 const hasNotability = /{{[Nn]otability/.test(content);
            if (isProtected) {
                 const hasStub = /{{[Ss]tub/.test(content);
                 console.log("🔒 Page is protected. Skipping auto-tag.");
                 return; // Do nothing for protected pages
            }


                if (!hasReferences && !hasNotability) {
            const content = page.revisions[0].content || '';
                    alert("⚠️ This article has no references. Adding {{Notability}} tag.");
            const hasReferences = /<ref>|{{cite/i.test(content);
                    window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=notability`;
            const hasNotability = /{{[Nn]otability/.test(content);
                } else if (content.length < 2000 && !hasStub) {
            const hasStub = /{{[Ss]tub/.test(content);
                    alert("ℹ️ This is a short article. Adding {{Stub}} tag.");
 
                    window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=stub`;
            if (!hasReferences && !hasNotability) {
                }
                alert("⚠️ This article has no references. Adding {{Notability}} tag.");
            });
                window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=notability`;
        }
            } else if (content.length < 2000 && !hasStub) {
                alert("ℹ️ This is a short article. Adding {{Stub}} tag.");
                window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=stub`;
            }
        });
     }
     }


     // ✅ On edit page, pre-fill tags
     // Step 2: Pre-fill edit form with tag
     if (mw.config.get('wgAction') === 'edit') {
     if (mw.config.get('wgAction') === 'edit') {
         const urlParams = new URLSearchParams(window.location.search);
         const urlParams = new URLSearchParams(window.location.search);

Revision as of 19:23, 13 July 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using('mediawiki.util').then(function () {
  function initAutoScroll() {
    const container = document.getElementById('scrollable-articles');
    if (!container) return;

    if (container.dataset.autoScrollInitialized) return;
    container.dataset.autoScrollInitialized = 'true';

    if (!container.dataset.duplicated) {
      const originalChildren = Array.from(container.children);
      originalChildren.forEach(child => {
        const clone = child.cloneNode(true);
        container.appendChild(clone);
      });
      container.dataset.originalWidth = container.scrollWidth / 2;
      container.dataset.duplicated = 'true';
    }

    let scrollSpeed = window.innerWidth < 500 ? 0.3 : 0.5;
    let requestId;

    function autoScroll() {
      container.scrollLeft += scrollSpeed;
      const originalWidth = parseFloat(container.dataset.originalWidth);

      if (container.scrollLeft >= originalWidth) {
        container.scrollLeft -= originalWidth;
      }

      requestId = requestAnimationFrame(autoScroll);
      container._autoScrollRequestId = requestId;
    }

    if (container._autoScrollRequestId) {
      cancelAnimationFrame(container._autoScrollRequestId);
    }

    function pauseScroll() {
      if (container._autoScrollRequestId) {
        cancelAnimationFrame(container._autoScrollRequestId);
        container._autoScrollRequestId = null;
      }
    }

    function resumeScroll() {
      if (!container._autoScrollRequestId) {
        autoScroll();
      }
    }

    container.removeEventListener('mouseenter', pauseScroll);
    container.removeEventListener('mouseleave', resumeScroll);

    container.addEventListener('mouseenter', pauseScroll);
    container.addEventListener('mouseleave', resumeScroll);

    autoScroll();
  }

  if (document.readyState === 'complete' || document.readyState === 'interactive') {
    initAutoScroll();
  } else {
    document.addEventListener('DOMContentLoaded', initAutoScroll);
  }

  mw.hook('wikipage.content').add(initAutoScroll);
});

// Hide 'View source' tab using JS fallback
$(document).ready(function () {
  $('#ca-viewsource').hide();
});

// Auto-tag NeedsReview for user-level edits only
$(document).ready(function() {
  if (mw.config.get("wgAction") === "edit" || mw.config.get("wgAction") === "submit") {
    var userGroups = mw.config.get('wgUserGroups');

    if (userGroups.includes('user') && !userGroups.includes('approver') && !userGroups.includes('sysop')) {
      var $wpTextbox1 = $('#wpTextbox1');
      if ($wpTextbox1.length && !$wpTextbox1.val().includes("{{NeedsReview}}")) {
        $wpTextbox1.val("{{NeedsReview}}\n" + $wpTextbox1.val());
      }
    }
  }
});

$(document).ready(function() {
  var desc = $('meta[name="knowlepedia-desc"]').attr('content');
  if (desc) {
    $('head').append('<meta name="description" content="' + desc + '">');
  }
});



mw.loader.using('mediawiki.util').then(function () {
  var schema = {
    "@context": "https://schema.org",
    "@type": "Person",
    "name": "Gaurav Singh Chouhan",
    "image": "https://commons.wikimedia.org/wiki/Special:FilePath/Gaurav_Singh_Chouhan.jpg",
    "url": "https://knowlepedia.org/wiki/Gaurav_Singh_Chouhan",
    "sameAs": [
      "https://www.imdb.com/name/nm14658047/",
      "https://www.instagram.com/mrgauravchouhan",
      "https://www.linkedin.com/in/mrgauravchouhan",
      "https://x.com/mrgauravchouhan",
      "https://www.timesofmalwa.in/2025/06/gaurav-singh-chouhan.html"
    ],
    "birthDate": "1999-07-21",
    "jobTitle": "Writer, Law Student, Motivational Speaker",
    "alumniOf": "Chaudhary Charan Singh University",
    "nationality": "Indian",
    "description": "Gaurav Singh Chouhan is an Indian writer, motivational speaker, and BA LLB student, known for his public talks and biography 'From Pen to Purpose.'"
  };

  var script = document.createElement('script');
  script.type = 'application/ld+json';
  script.text = JSON.stringify(schema);
  document.head.appendChild(script);
});

// Auto-notability tagger: Adds {{Notability}} to pages missing references
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
    if (mw.config.get('wgNamespaceNumber') !== 0 || mw.config.get('wgAction') !== 'view') return;

    var pageName = mw.config.get('wgPageName');
    var api = new mw.Api();

    api.get({
        action: 'query',
        prop: 'revisions',
        rvprop: 'content',
        titles: pageName,
        formatversion: 2
    }).then(function (data) {
        var content = data.query.pages[0].revisions[0].content;
        if (!content.includes('<ref') && !content.includes('==References==') && !content.includes('{{Notability')) {
            var newContent = '{{Notability|date=July 2025}}\n' + content;

            api.postWithEditToken({
                action: 'edit',
                title: pageName,
                text: newContent,
                summary: 'Automatically added Notability template due to missing references'
            }).then(function (result) {
                if (result.edit && result.edit.result === 'Success') {
                    location.reload();
                }
            });
        }
    });
});

mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
    if (mw.config.get('wgNamespaceNumber') === 0 && mw.config.get('wgAction') === 'view') {
        const pageTitle = mw.config.get('wgPageName');
        const api = new mw.Api();

        // Step 1: First check if page is protected
        api.get({
            action: 'query',
            titles: pageTitle,
            prop: 'info|revisions',
            inprop: 'protection',
            rvprop: 'content',
            formatversion: 2
        }).done(function (data) {
            const page = data.query.pages[0];

            const isProtected = page.protection && page.protection.some(p => p.type === 'edit');
            if (isProtected) {
                console.log("🔒 Page is protected. Skipping auto-tag.");
                return; // Do nothing for protected pages
            }

            const content = page.revisions[0].content || '';
            const hasReferences = /<ref>|{{cite/i.test(content);
            const hasNotability = /{{[Nn]otability/.test(content);
            const hasStub = /{{[Ss]tub/.test(content);

            if (!hasReferences && !hasNotability) {
                alert("⚠️ This article has no references. Adding {{Notability}} tag.");
                window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=notability`;
            } else if (content.length < 2000 && !hasStub) {
                alert("ℹ️ This is a short article. Adding {{Stub}} tag.");
                window.location.href = `/w/index.php?title=${pageTitle}&action=edit&autoTag=stub`;
            }
        });
    }

    // Step 2: Pre-fill edit form with tag
    if (mw.config.get('wgAction') === 'edit') {
        const urlParams = new URLSearchParams(window.location.search);
        const autoTag = urlParams.get('autoTag');
        const textarea = document.getElementById('wpTextbox1');

        if (autoTag && textarea) {
            if (autoTag === 'notability' && !textarea.value.includes("{{Notability")) {
                textarea.value = "{{Notability|date={{subst:DATE}}}}\n\n" + textarea.value;
            }
            if (autoTag === 'stub' && !textarea.value.includes("{{Stub")) {
                textarea.value += "\n\n{{Stub}}";
            }
        }
    }
});