User:SuperCode111/temp-user-watch.js

// ==UserScript== // @name Temp User Watch (1 hour) // @description Adds a "Watch (1h)" button next to Thank on diffs and notifies if that us

// ==UserScript==
// @name         Temp User Watch (1 hour)
// @description  Adds a "Watch (1h)" button next to Thank on diffs and notifies if that user edits again in the next hour
// ==/UserScript==

mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
    const CHECK_INTERVAL = 30000; // 30 seconds
    const WATCH_DURATION = 60 * 60 * 1000; // 1 hour
    const NOTIFY_SOUND = 'https://upload.wikimedia.org/wikipedia/commons/4/4e/Notification.ogg';

    // Load/save temporary watchlist from localStorage
    function loadWatchlist() {
        return JSON.parse(localStorage.getItem('TempUserWatchList') || '{}');
    }
    function saveWatchlist(obj) {
        localStorage.setItem('TempUserWatchList', JSON.stringify(obj));
    }

    function addUser(username) {
        const list = loadWatchlist();
        list[username] = Date.now() + WATCH_DURATION;
        saveWatchlist(list);
        alert(`User ${username} is now watched for 1 hour.`);
    }

    function removeExpired() {
        const list = loadWatchlist();
        let changed = false;
        for (const u in list) {
            if (Date.now() > list[u]) {
                delete list[u];
                changed = true;
            }
        }
        if (changed) saveWatchlist(list);
    }

    // Check watched users for edits
    async function checkUsers() {
        const list = loadWatchlist();
        for (const username in list) {
            // API query: get latest contribution
            try {
                const result = await (new mw.Api()).get({
                    action: 'query',
                    list: 'usercontribs',
                    ucuser: username,
                    uclimit: 1,
                    format: 'json'
                });
                const uc = result.query.usercontribs[0];
                if (!uc) continue;
                const lastEditTime = new Date(uc.timestamp).getTime();
                if (!list[username + '_last']) list[username + '_last'] = 0;
                if (lastEditTime > list[username + '_last']) {
                    // Notify
                    if (Notification.permission !== 'granted') Notification.requestPermission();
                    new Notification('Watched user edited!', {
                        body: `${username} edited [[${uc.title}]]`,
                        icon: 'https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png'
                    });
                    new Audio(NOTIFY_SOUND).play();
                    list[username + '_last'] = lastEditTime;
                    saveWatchlist(list);
                }
            } catch(e) { console.error(e); }
        }
    }

    // Add "Watch (1h)" button next to Thank on diffs
    function addButtons() {
        const userLinks = document.querySelectorAll('.mw-userlink');
        userLinks.forEach(link => {
            const username = link.textContent.trim();
            if (link.nextSibling && link.nextSibling.className === 'temp-watch-btn') return;
            const btn = document.createElement('a');
            btn.textContent = '| Watch (1h)';
            btn.href = '#';
            btn.className = 'temp-watch-btn';
            btn.style.marginLeft = '4px';
            btn.addEventListener('click', e => {
                e.preventDefault();
                addUser(username);
            });
            link.parentNode.insertBefore(btn, link.nextSibling);
        });
    }

    // Initial setup
    removeExpired();
    addButtons();
    setInterval(() => { removeExpired(); checkUsers(); }, CHECK_INTERVAL);
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.