// ==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);
});
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.