// ==UserScript== // @name Wavez New Users // @namespace https://wavez.fm/ // @author fluteds // @icon https://wavez.fm/favicon.ico // @version 2.4 // @updateURL https://raw.githubusercontent.com/fluteds/wavez/main/userscripts/wavez-new-users.user.js // @downloadURL https://raw.githubusercontent.com/fluteds/wavez/main/userscripts/wavez-new-users.user.js // @description Pills accounts younger than NEW_DAYS in chat and the user list, using each account's real join date from the public profile API. // @match https://wavez.fm/* // @run-at document-idle // @grant unsafeWindow // @grant GM_notification // ==/UserScript== (function () { var KEY = 'wavez-tools:'; var SPEC = { id: 'new-users', label: 'New Users', cat: 'Moderation', settings: [["NEW_DAYS", "Days flagged", 2, ""], ["BADGE", "Pill text", "NEW", ""], ["COLOR_NAME", "Colour the name", false, ""], ["COLOR", "Colour", "#00E5A0", ""], ["NOTIFY", "Desktop alert", true, ""]] }; function setting(id, name, dflt) { try { var v = localStorage.getItem(KEY + id + ":" + name); return v === null ? dflt : JSON.parse(v); } catch (e) { return dflt; } } function announce() { document.dispatchEvent(new CustomEvent('wavez-tools:addon', { detail: JSON.stringify(SPEC) })); } document.addEventListener('wavez-tools:ping', announce); announce(); if (localStorage.getItem(KEY + SPEC.id) === 'off') return; var NEW_DAYS = setting("new-users", "NEW_DAYS", 2); var BADGE = setting("new-users", "BADGE", "NEW"); var COLOR_NAME = setting("new-users", "COLOR_NAME", false); var COLOR = setting("new-users", "COLOR", "#00E5A0"); var NOTIFY = setting("new-users", "NOTIFY", true); (function () { "use strict"; var TAG = ["%c[wz-new]", "color:#00E5A0;font-weight:bold"]; var KEY = "wavez-user-joined-v2"; var API = "https://api.wavez.fm/users/by-username/"; var NAME_SELECTOR = '[data-wavezfm-chat-name="true"], [data-wavezfm-people-name="true"], [data-wavezfm-user-preview-name="true"]'; var joined = (function () { try { return JSON.parse(localStorage.getItem(KEY)) || {}; } catch (e) { return {}; } })(); var pending = {}; function save() { localStorage.setItem(KEY, JSON.stringify(joined)); } function isNew(name, now) { return joined[name] > 0 && now - joined[name] < NEW_DAYS * 86400000; } var SAFE_COLOR = /^#[0-9a-f]{3,8}$/i.test(COLOR) ? COLOR : "#00E5A0"; var css = document.createElement("style"); css.textContent = (COLOR_NAME ? ".wz-new-user { color: " + SAFE_COLOR + " !important; }" : "") + (BADGE ? ".wz-new-user::after { content: " + JSON.stringify(String(BADGE)).replace(/ 0 ? at : 0; save(); if (isNew(name, Date.now())) console.log.apply(console, TAG.concat([name + " joined " + new Date(at).toISOString().slice(0, 10)])); remark(name); }) .catch(function () {}) .then(function () { delete pending[name]; }); } function notify(body) { if (typeof GM_notification === "function") return GM_notification({ title: "Wavez New Users", text: body, onclick: function () { window.focus(); } }); if (window.Notification && Notification.permission === "granted") new Notification("Wavez New Users", { body: body }); } var started = Date.now(); var pinged = {}; function ping(name) { if (pinged[name]) return; pinged[name] = true; if (!NOTIFY || Date.now() - started < 10000 || !(document.hidden || !document.hasFocus())) return; notify(name + " is a new account (under " + NEW_DAYS + " day" + (NEW_DAYS === 1 ? "" : "s") + " old)"); } function apply(el, name) { var fresh = isNew(name, Date.now()); el.classList.toggle("wz-new-user", fresh); if (fresh) ping(name); } function mark(el) { var name = (el.textContent || "").trim().toLowerCase(); if (!name || el.dataset.wzName === name) return; el.dataset.wzName = name; if (name in joined) apply(el, name); else lookup(name); } function remark(name) { var nodes = document.querySelectorAll(NAME_SELECTOR); for (var i = 0; i < nodes.length; i++) if (nodes[i].dataset.wzName === name) apply(nodes[i], name); } function scan(root) { if (root.nodeType !== 1) return; if (root.matches && root.matches(NAME_SELECTOR)) mark(root); var nodes = root.querySelectorAll(NAME_SELECTOR); for (var i = 0; i < nodes.length; i++) mark(nodes[i]); } scan(document.body); new MutationObserver(function (records) { for (var i = 0; i < records.length; i++) { var added = records[i].addedNodes; for (var j = 0; j < added.length; j++) scan(added[j]); } }).observe(document.body, { childList: true, subtree: true }); var pageWindow = typeof unsafeWindow !== "undefined" ? unsafeWindow : window; pageWindow.WZNew = { list: function () { var now = Date.now(); return Object.keys(joined).filter(function (n) { return isNew(n, now); }); }, joined: function () { return joined; }, forget: function (name) { delete joined[String(name).toLowerCase()]; save(); }, reset: function () { localStorage.removeItem(KEY); joined = {}; }, }; console.log.apply(console, TAG.concat(["watching for accounts under " + NEW_DAYS + " days old. WZNew.list() / WZNew.forget(name) / WZNew.reset()"])); if (location.hash === "#wz-new-test") { var real = joined; var now = Date.now(); joined = { fresh: now - 1000, stale: now - (NEW_DAYS + 1) * 86400000, missing: 0 }; console.assert(isNew("fresh", now) === true, "joined a second ago is new"); console.assert(isNew("stale", now) === false, "joined before the window is not new"); console.assert(isNew("missing", now) === false, "no known date is never new"); console.assert(isNew("nobody", now) === false, "unlooked-up name is not new"); joined = real; console.log.apply(console, TAG.concat(["tests passed"])); } })(); })();