Jump to content

User:Veko/common.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Enable caching for resource loads
if(!/\bnocache=\b/.test(location.href)){
  let e=(e,t,n)=>(e=e.replace(/special:mypage/i,"User:"+mw.config.get("wgUserName")),
    $.get("https://"+t+"/w/api.php?titles="+e+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1")
    .then((r=>{let o=r.query.pages[0];if(o.missing)return;let a=o.revisions[0].content;
    if(n&&"text/javascript"!==n||"javascript"!==o.contentmodel){
      if("text/css"!==n||"css"!==o.contentmodel)return $.Deferred().reject('Refused to load "'+e+'"@'+t+": content type mismatch");
      mw.loader.addStyleTag(a)
    }else{let e=document.createElement("script");e.innerHTML=a,document.head.appendChild(e)}}))),
  t=e=>{let t=/^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(e),
    n=/\btitle=([^=?&]*)/.exec(e);return t&&n&&/\baction=raw\b/.test(e)&&/\bctype=/.test(e)?[n[1],t[1]||mw.config.get("wgServerName")]:null};
  window.importScript=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/javascript")},
  window.importStyleSheet=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/css")};
  let n=mw.loader.load;
  mw.loader.load=function(r,o){let a=t(r);a?e(a[0],a[1],o):n.apply(mw.loader,[...arguments])};
  let r=mw.loader.getScript;
  mw.loader.getScript=function(n){let o=t(n);return o?e(o[0],o[1],"text/javascript"):r.apply(mw.loader,[...arguments])}
}

// Useful scripts for rollback/EC/PCR rights
importScript('User:SD0001/BDCS.js');
importScript('User:RedWarn/.js');
importScript('User:Ingenuity/AntiVandal.js');
importScript('User:Awesome Aasim/rcpatrol.js');
importScript('User:Evad37/rater.js');
importScript('User:Qwertyytrewqqwerty/DisamAssist.js');
//importScript('User:Jackmcbarn/editProtectedHelper.js');
importScript('User:Evad37/duplinks-alt.js');
importScript('User:GoldenRing/wordcount.js');
importScript('User:Ohconfucius/script/Common Terms.js');
importScript('User:Evad37/Thanky.js');
importScript('User:Headbomb/unreliable.js');
importScript('User:Enterprisey/cv-revdel.js');
importScript('User:MusikAnimal/responseHelper.js');
importScript('User:SD0001/StubSorter.js');
importScript('User:Awesome Aasim/xfdvote.js');
importScript('User:Mr. Stradivarius/gadgets/Draftify.js');
importScript('User:Eejit43/scripts/rmtr-helper.js');
importScript('User:MPGuy2824/MoveToDraft.js');
importScript('User:Eejit43/scripts/redirect-helper.js');
importScript('User:Bradv/Scripts/Superlinks.js');
importScript('User:SD0001/GAN-helper.js');
importScript('User:Novem Linguae/Scripts/GANReviewTool.js');
importScript('User:The Earwig/copyvios.js');
importScript('User:DreamRimmer/User not around.js'); // Backlink: [[User:DreamRimmer/User not around.js]]
importScript('User:DreamRimmer/EasySubpage.js'); // Backlink: [[User:DreamRimmer/EasySubpage.js]]
////////// ENHANCED STATUS CHANGER SCRIPT
// Creator: Misza13
// Modified by: Various contributors
// Updated to include additional statuses from the UserStatus template

$.when(
    $.ready,
    mw.loader.using( [ "mediawiki.api" ] )
).then( function () {
    // Create configuration variable if it doesn’t exist
    if (typeof(statusChangerConfig) === 'undefined') {
        statusChangerConfig = {};
    }

    // Expanded status list (Includes additional statuses from your table)
    if (typeof(statusChangerConfig.statusList) === 'undefined') {
        statusChangerConfig.statusList = [
            'online', 'offline', 'sleeping', 'busy', 'away', 'editing', 
            'atwork', 'school', 'eating', 'vandal', 'holiday', 'twinkling',
            'huggling', 'wikibreak', 'working'
        ];
    }

    // Define the status page
    if (typeof(statusChangerConfig.statusPage) === 'undefined') {
        statusChangerConfig.statusPage = 'User:' + mw.config.get('wgUserName') + '/Status';
    }

    function makeListener(newStatus) {
        return function ( evt ) {
            evt.preventDefault();
            var api = new mw.Api({
                ajax: { headers: { 'Api-User-Agent': '[[w:User:Enterprisey/StatusChanger.js]]' } }
            });

            api.postWithEditToken({
                action: 'edit',
                title: statusChangerConfig.statusPage,
                text: newStatus,
                summary: mw.config.get('wgUserName') + " is now " + ((newStatus === "sleep") ? "sleeping" : newStatus) + "."
            }).then(function(){
                // Purge the user page after changing status
                api.post( { action: "purge", titles: 'User:' + mw.config.get('wgUserName') } ).then(function(){
                    mw.notify('Status updated and page purged.');
                });
            });
            return false;
        };
    }

    // Add status changer buttons in a collapsible table
    var statusTable = document.createElement("div");
    statusTable.innerHTML = `
        <div style="text-align: center; margin-top: 10px;">
            <strong>Change Your Status:</strong>
            <table style="margin: auto; border-collapse: collapse;">
                <tr>
                    <td><button class="status-button" data-status="online">Online</button></td>
                    <td><button class="status-button" data-status="offline">Offline</button></td>
                    <td><button class="status-button" data-status="editing">Editing</button></td>
                    <td><button class="status-button" data-status="busy">Busy</button></td>
                </tr>
                <tr>
                    <td><button class="status-button" data-status="away">Away</button></td>
                    <td><button class="status-button" data-status="sleeping">Sleeping</button></td>
                    <td><button class="status-button" data-status="atwork">At Work</button></td>
                    <td><button class="status-button" data-status="school">At School</button></td>
                </tr>
                <tr>
                    <td><button class="status-button" data-status="eating">Eating</button></td>
                    <td><button class="status-button" data-status="vandal">Fighting Vandalism</button></td>
                    <td><button class="status-button" data-status="wikibreak">On a Wikibreak</button></td>
                    <td><button class="status-button" data-status="holiday">On Holiday</button></td>
                </tr>
                <tr>
                    <td><button class="status-button" data-status="huggling">Huggling</button></td>
                    <td><button class="status-button" data-status="twinkling">Twinkling</button></td>
                    <td colspan="2"><button class="status-button" data-status="working">Working</button></td>
                </tr>
            </table>
        </div>
    `;

    // Attach event listeners to buttons
    statusTable.querySelectorAll('.status-button').forEach(button => {
        button.addEventListener('click', makeListener(button.getAttribute('data-status')));
    });

    // Insert the table below the user status box
    var userStatusDiv = document.getElementById("TemplateUserinfo");
    if (userStatusDiv) {
        userStatusDiv.appendChild(statusTable);
    }

    // Add quick-access links to the personal menu (top right)
    for (var i = 0; i < statusChangerConfig.statusList.length; i++) {
        var stat = statusChangerConfig.statusList[i];
        var message = (stat === "sleeping") ? "asleep" : stat;

        mw.util.addPortletLink(
            "p-personal", // Target tab - personal links
            "#",
            stat, // Link text
            "pt-status-" + stat, // ID of new button
            "I'm " + message + "!", // Hover text
            "", // Access key - not needed
            document.getElementById("pt-logout") // Add before logout button
        ).addEventListener('click', makeListener(stat));
    }

    // Add a purge link manually to the status section
    var purgeLink = document.createElement("a");
    purgeLink.href = "https://en.wikipedia.org/w/index.php?title=User:" + mw.config.get('wgUserName') + "&action=purge";
    purgeLink.textContent = "Click here to refresh status";
    purgeLink.style.display = "block";
    purgeLink.style.textAlign = "center";
    purgeLink.style.marginTop = "5px";

    if (userStatusDiv) {
        userStatusDiv.appendChild(purgeLink);
    }
});

importScript('User:DannyS712/Draft no cat.js');
importScript('User:BrandonXLF/ReferenceExpander.js');
importScript('User:Awesome Aasim/redirectcreator.js');
importScript('User:Qwerfjkl/scripts/CFDlister.js');
importScript('User:Shubinator/DYKcheck.js');
importScript('User:BrandonXLF/HotDefaultSort.js');
importScript('User:Theleekycauldron/DYK_promoter.js');
importScript('User:BrandonXLF/CollapseSections.js');
importScript('User:Schminnte/PageCuration.js');
importScript('User:Nardog/CatChangesViewer.js');
importScript('User:קיפודנחש/cat-a-lot.js');
importScript('User:Enterprisey/orcp-helper.js');
importScript('User:Novem Linguae/Scripts/DontForgetG12.js');
importScript('User:Writ Keeper/rollbackSummary.js');
importScript('User:BrandonXLF/CitationStyleMarker.js');
//importScript('User:Andrybak/Archiver.js');
importScript('User:PleaseStand/segregate-refs.js');
importScript('User:Ohconfucius/script/MOSNUM dates.js');
importScript('User:JPxG/Difformatter.js');
importScript('User:Qwerfjkl/scripts/editRedirect.js');
importScript('User:Enterprisey/section-redir-note.js');
importScript('User:BrandonXLF/ShowUserGender.js');
importScript('User:Novem_Linguae/Scripts/VisualEditorEverywhere.js');
importScript('User:Ingenuity/MergeDuplicateRefs.js');
importScript('User:Sohom_Datta/fastreview.js');
importScript('User:Nardog/RefRenamer.js');
importScript('User:Nardog/MoveHistory.js');
importScript('User:Trappist the monk/HarvErrors.js');
importScript('User:Polygnotus/DuplicateReferences.js');

// Add "back to top" links to each section
$(function () {
  var elems = document.getElementsByClassName('editsection');
  for (let i = 0; i < elems.length; i++) {
    var span = document.createElement('span');
    var link = document.createElement('a');
    link.href = '#top';
    link.appendChild(document.createTextNode('back to top'));
    span.appendChild(document.createTextNode('['));
    span.appendChild(link);
    span.appendChild(document.createTextNode('] '));
    elems[i].insertBefore(span, elems[i].firstChild);
  }
});

// ORES Article Quality Predictor
$.getScript(
  '//meta.wikimedia.org/w/index.php?title=User:EpochFail/ArticleQuality-system.js&action=raw&ctype=text/javascript',
  function () {
    articleQuality = new ArticleQuality({
      ores_host: "https://ores.wikimedia.org",
      weights: {
        Stub: 1,
        Start: 2,
        C: 3,
        B: 4,
        GA: 5,
        FA: 6
      },
      names: {
        Stub: "{{icon|Stub}}",
        Start: "{{icon|Start}}",
        C: "{{icon|C}}",
        B: "{{icon|B}}",
        GA: "{{icon|GA}}",
        FA: "{{icon|FA}}"
      },
      assessment_system: "Pseudoscience",
      dbname: "enwiki"
    });
    if (mw.config.get('wgAction') === "view" &&
        (mw.config.get('wgNamespaceNumber') === 0 ||
         mw.config.get('wgNamespaceNumber') === 2 ||
         mw.config.get('wgNamespaceNumber') === 118)) {
      articleQuality.getAndRenderScoreHeader();
    }
    articleQuality.addScoresToArticleLinks();
  }
);
mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:EpochFail/ArticleQuality.css&action=raw&ctype=text/javascript');

mw.loader.using(['jquery', 'mediawiki.util']).then(function () {
    function startPendingChecker() {
        if (mw.config.get("wgDBname") !== "enwiki") return;

        var seen = new Map();
        var count = 0;
        var baseInterval = 30000;
        var minDelay = 5000;

        var localKey = "pendingChangesSeen";
        var contentKey = "pendingChangesList";
        var seenStore = new Set(JSON.parse(localStorage.getItem(localKey) || "[]"));
        var storedContent = JSON.parse(localStorage.getItem(contentKey) || "[]");

        function saveContentList() {
            localStorage.setItem(contentKey, JSON.stringify(storedContent));
        }

        function formatTimestamp(date) {
            const now = Date.now();
            const diff = Math.floor((now - date) / 1000);
            if (diff < 60) return `${diff}s ago`;
            if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
            if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
            return `${Math.floor(diff / 86400)}d ago`;
        }

        var toolbar = document.querySelector('#pt-notifications-alert, .mw-echo-notifications-badge');
        if (!toolbar) return;

        var pendingBell = document.createElement("span");
        pendingBell.id = "pending-alert-bell";
        pendingBell.style.cssText = "position: relative; margin-left: 10px; cursor: pointer; display: inline-block; width: 24px; height: 24px;";
        pendingBell.innerHTML = `
            <svg viewBox="0 0 24 24" width="20" height="20" fill="#54595d" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2a6 6 0 00-6 6v4.586l-.707.707A1 1 0 006 15h12a1 1 0 00.707-1.707L18 12.586V8a6 6 0 00-6-6zm0 20a2 2 0 001.995-1.85L14 20h-4a2 2 0 001.85 1.995L12 22z" />
            </svg>
            <span class='mw-echo-notifications-badgeItem' style='background:#d33;color:#fff;padding:2px 6px;border-radius:10px;font-size:11px;position:absolute;top:-6px;right:-6px;display:none;' id='pending-alert-count'>0</span>
        `;

        toolbar.parentElement.appendChild(pendingBell);

        var notifDropdown = $("<div>").css({
            display: "none",
            position: "absolute",
            top: "30px",
            right: "0",
            width: "320px",
            maxHeight: "400px",
            overflowY: "auto",
            background: "#fff",
            border: "1px solid #ccc",
            borderRadius: "6px",
            boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
            padding: "10px",
            zIndex: 9999
        });

        $(pendingBell).on("click", function () {
            notifDropdown.toggle();
        });

        var notifCount = $("#pending-alert-count");

        var clearBtn = $('<div>').text("Clear seen history").css({
            textAlign: "center",
            margin: "10px 0",
            cursor: "pointer",
            fontSize: "12px",
            color: "#0645ad",
            textDecoration: "underline"
        }).click(function () {
            seenStore.clear();
            storedContent = [];
            localStorage.removeItem(localKey);
            localStorage.removeItem(contentKey);
            location.reload();
        });

        $("body").append(notifDropdown);

        function updateTimestamps() {
            $(".pending-timestamp").each(function () {
                var ts = $(this).data("timestamp");
                $(this).text(formatTimestamp(new Date(ts)));
            });
        }

        function removeEntry(title) {
            const info = seen.get(title);
            if (info) {
                info.element.remove();
                seen.delete(title);
                storedContent = storedContent.filter(item => item.title !== title);
                count--;
                notifCount.text(count);
                if (count === 0) notifCount.hide();
                localStorage.setItem(contentKey, JSON.stringify(storedContent));
            }
        }

        function createRemoveIcon(title) {
            return $("<span>").html(`
                <svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='#999' stroke-width='2' stroke-linecap='round'><path d='M18 6L6 18M6 6l12 12'/></svg>
            `).css({
                float: "right",
                cursor: "pointer",
                marginLeft: "8px"
            }).attr("title", "Mark as reviewed").click(() => removeEntry(title));
        }

        function renderStoredNotifications() {
            count = 0;
            notifDropdown.empty();
            for (const item of storedContent) {
                const timeDiv = $("<div>")
                    .addClass("pending-timestamp")
                    .data("timestamp", item.timestamp)
                    .text(formatTimestamp(new Date(item.timestamp)))
                    .css({ fontSize: "10px", color: "#666" });

                const element = $("<div>").css({
                    marginBottom: "8px",
                    paddingBottom: "8px",
                    borderBottom: "1px solid #eee"
                }).append(
                    $("<a>")
                        .attr("href", item.url)
                        .attr("target", "_blank")
                        .text(item.title)
                        .css({ fontWeight: "bold", color: "#0645ad" }),
                    createRemoveIcon(item.title),
                    timeDiv
                );

                notifDropdown.prepend(element);
                seen.set(item.title, { url: item.url, revid: item.revid, element });
                count++;
            }
            notifDropdown.append(clearBtn);
            notifCount.text(count);
            if (count > 0) notifCount.show();
            setInterval(updateTimestamps, 60000);
        }

        function addNotification(title, url, revid, timestamp) {
            if (seen.has(title)) return;

            const entry = { title, url, revid, timestamp };

            storedContent.push(entry);
            if (storedContent.length > 100) {
                storedContent = storedContent.slice(-100);
            }
            saveContentList();

            const timeAgo = formatTimestamp(new Date(timestamp));

            const timeDiv = $("<div>")
                .addClass("pending-timestamp")
                .data("timestamp", timestamp)
                .text(timeAgo)
                .css({ fontSize: "10px", color: "#666" });

            const item = $("<div>").css({
                marginBottom: "8px",
                paddingBottom: "8px",
                borderBottom: "1px solid #eee"
            }).append(
                $("<a>")
                    .attr("href", url)
                    .attr("target", "_blank")
                    .text(title)
                    .css({ fontWeight: "bold", color: "#0645ad" }),
                createRemoveIcon(title),
                timeDiv
            );

            notifDropdown.prepend(item);
            notifDropdown.append(clearBtn);
            notifCount.text(++count);
            if (count > 0) notifCount.show();

            seen.set(title, { url: url, revid: revid, element: item });

            mw.notify(
                $("<a>")
                    .attr("href", url)
                    .attr("target", "_blank")
                    .text("Pending: " + title)[0],
                {
                    title: "Pending Change Alert",
                    autoHide: false
                }
            );
        }

        function cleanupReviewed() {
            if (seen.size === 0) return;

            const titles = Array.from(seen.keys());
            const url = "https://en.wikipedia.org/w/api.php?action=query&prop=flagged&titles=" + encodeURIComponent(titles.join("|")) + "&format=json&origin=*";

            fetch(url)
                .then(res => res.json())
                .then(data => {
                    const pages = data.query.pages;
                    for (const id in pages) {
                        const page = pages[id];
                        if (!page.flagged || page.flagged.revid >= seen.get(page.title).revid) {
                            removeEntry(page.title);
                        }
                    }
                });
        }

        function fetchExistingPending() {
            const url = "https://en.wikipedia.org/w/api.php?action=query&list=oldreviewedpages&ornamespace=0&orlimit=50&format=json&origin=*";
            fetch(url)
                .then(res => res.json())
                .then(data => {
                    const pages = (data && data.query && data.query.oldreviewedpages) || [];
                    const titles = pages.map(page => page.title);

                    if (titles.length === 0) return;

                    const revUrl = "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=ids|timestamp&titles=" + encodeURIComponent(titles.join("|")) + "&format=json&origin=*";

                    fetch(revUrl)
                        .then(res => res.json())
                        .then(data2 => {
                            const pagesData = data2.query.pages;
                            Object.keys(pagesData).forEach(pageId => {
                                const page = pagesData[pageId];
                                if (!page.revisions || !page.revisions[0]) return;
                                const rev = page.revisions[0];
                                const revid = rev.revid;
                                const timestamp = rev.timestamp;
                                const title = page.title;
                                const url = "https://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(title) + "&diff=" + revid + "&oldid=prev";
                                if (!seen.has(title)) {
                                    addNotification(title, url, revid, timestamp);
                                }
                            });
                        });
                })
                .catch(err => {
                    console.error("Error loading oldreviewedpages:", err);
                });
        }

        function checkRecentChanges() {
            const start = performance.now();
            const url = "https://en.wikipedia.org/w/api.php?action=query&list=recentchanges&rcprop=title|ids|tags|timestamp&rclimit=50&rcshow=!bot&format=json&origin=*";

            fetch(url)
                .then(res => res.json())
                .then(data => {
                    const changes = (data && data.query && data.query.recentchanges) || [];
                    for (let i = 0; i < changes.length; i++) {
                        const change = changes[i];
                        if (change.tags && change.tags.indexOf("flaggedrevs-pending") !== -1 && !seen.has(change.title)) {
                            const title = change.title;
                            const revid = change.revid;
                            const timestamp = change.timestamp;
                            const url = "https://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(title) + "&diff=" + revid + "&oldid=prev";
                            addNotification(title, url, revid, timestamp);
                        }
                    }
                })
                .catch(e => {
                    console.error("Pending changes check failed:", e);
                })
                .finally(() => {
                    const duration = performance.now() - start;
                    const delay = Math.max(minDelay, baseInterval - duration);
                    setTimeout(checkRecentChanges, delay);
                    setTimeout(cleanupReviewed, 10000);
                });
        }

        renderStoredNotifications();
        fetchExistingPending();
        checkRecentChanges();
    }

    startPendingChecker();
});
importScript('User:FlightTime/OneClickArchiver.js'); // Backlink: [[User:FlightTime/OneClickArchiver.js]]
importScript('User:Writ Keeper/Scripts/deletionFinder.js'); // Backlink: [[User:Writ Keeper/Scripts/deletionFinder.js]]
importScript('User:Novem Linguae/Scripts/ReviewStatus.js'); // Backlink: [[User:Novem Linguae/Scripts/ReviewStatus.js]]
importScript('User:Novem Linguae/Scripts/DraftCleaner.js'); // Backlink: [[User:Novem Linguae/Scripts/DraftCleaner.js]]
importScript('User:Terasail/Edit Request Tool.js'); // Backlink: [[User:Terasail/Edit Request Tool.js]]
importScript('User:The Editor\'s Apprentice/randomlink.js'); // Backlink: [[User:The Editor's Apprentice/randomlink.js]]