Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name OWoT GranularBlocker
- // @namespace gimmickCellar
- // @version 1.1
- // @description /block but cooler
- // @author gimmickCellar
- // @match *://*ourworldoftext.com/*
- // @match *://localhost/*
- // @match *://127.0.0.1/*
- // @grant none
- // ==/UserScript==
- (function() {
- var blocks = {
- ids: [],
- usernames: {},
- nickedanon: false,
- nonickanon: false,
- nicknames: [],
- messagecontains: [],
- blockAll: false,
- noTell: false,
- noAnon: false,
- noReg: false
- };
- var renderedChats = [];
- function loadBlocks() {
- var stored = localStorage.getItem("gimmick_blocks");
- if (stored) {
- try {
- var parsed = JSON.parse(stored);
- if (parsed && typeof parsed === "object") {
- blocks = Object.assign(blocks, parsed);
- }
- } catch (e) {}
- }
- }
- function saveBlocks() {
- try {
- localStorage.setItem("gimmick_blocks", JSON.stringify(blocks));
- applyRetroactiveDeletions();
- } catch (err) {
- console.error(err);
- }
- }
- function printFeedback(msg) {
- if (typeof addChat === "function") {
- addChat(null, 0, "user", "[ Client ]", msg, "Client", false, false, false, null, Date.now());
- } else {
- console.log("[Client] " + msg);
- }
- }
- function shouldHideChat(e) {
- try {
- if (!e) return false;
- if (blocks.blockAll) return true;
- var isTell = e.privateMessage === "to_me" || (e.dataObj && e.dataObj.privateMessage === "to_me");
- if (isTell && blocks.noTell) return true;
- if (!e.registered && blocks.noAnon) return true;
- if (e.registered && blocks.noReg) return true;
- if (e.type === "anon_nick" && blocks.nickedanon) return true;
- if (e.type === "anon" && blocks.nonickanon) return true;
- if (typeof e.id === "number" && blocks.ids.includes(e.id)) return true;
- if (e.nickname) {
- var nickUpper = e.nickname.toUpperCase();
- for (var i = 0; i < blocks.nicknames.length; i++) {
- if (nickUpper.includes(blocks.nicknames[i].toUpperCase())) {
- return true;
- }
- }
- }
- if (e.message) {
- var msgUpper = e.message.toUpperCase();
- for (var i = 0; i < blocks.messagecontains.length; i++) {
- if (msgUpper.includes(blocks.messagecontains[i].toUpperCase())) {
- return true;
- }
- }
- }
- if (e.registered && e.realUsername) {
- var userKey = e.realUsername.toUpperCase();
- if (blocks.usernames.hasOwnProperty(userKey)) {
- var userRules = blocks.usernames[userKey];
- if (Array.isArray(userRules)) {
- for (var j = 0; j < userRules.length; j++) {
- var rule = userRules[j];
- if (!rule || !rule.hasOwnProperty("condition") || rule.condition === null || rule.condition === undefined) {
- return true;
- }
- var ruleLower = String(rule.condition).toLowerCase();
- if (ruleLower === "tell" && isTell) {
- return true;
- }
- if (ruleLower.startsWith("messagecontains:")) {
- var matchText = String(rule.condition).substring("messagecontains:".length).toUpperCase();
- if (e.message && e.message.toUpperCase().includes(matchText)) {
- return true;
- }
- }
- if (ruleLower.startsWith("nickname:")) {
- var matchNick = String(rule.condition).substring("nickname:".length).toUpperCase();
- if (e.nickname && e.nickname.toUpperCase().includes(matchNick)) {
- return true;
- }
- }
- }
- }
- }
- }
- } catch (err) {
- console.error(err);
- }
- return false;
- }
- function deleteChatRecord(id, date) {
- try {
- var records = [window.chatRecordsPage, window.chatRecordsGlobal];
- for (var r = 0; r < records.length; r++) {
- var recList = records[r];
- if (!recList) continue;
- for (var i = 0; i < recList.length; i++) {
- var currentRec = recList[i];
- if (currentRec && currentRec.id === id && currentRec.date === date) {
- if (currentRec.element) {
- currentRec.element.remove();
- }
- recList.splice(i, 1);
- i--;
- }
- }
- }
- } catch (err) {
- console.error(err);
- }
- }
- function applyRetroactiveDeletions() {
- try {
- var toDelete = [];
- for (var i = 0; i < renderedChats.length; i++) {
- var chat = renderedChats[i];
- if (shouldHideChat(chat)) {
- toDelete.push(chat);
- }
- }
- for (var j = 0; j < toDelete.length; j++) {
- var target = toDelete[j];
- deleteChatRecord(target.id, target.date);
- var idx = renderedChats.indexOf(target);
- if (idx > -1) {
- renderedChats.splice(idx, 1);
- }
- }
- } catch (err) {
- console.error(err);
- }
- }
- function blockCommand(args) {
- try {
- var arg = args.join(" ").trim();
- if (!arg) {
- printFeedback("Usage: /block <nickedanon|nonickanon|anon|reg|tell|*|nickname:<text>|messagecontains:<text>|ID>");
- return;
- }
- if (arg === "nickedanon") {
- blocks.nickedanon = true;
- saveBlocks();
- printFeedback("Blocked nicked anonymous users.");
- } else if (arg === "nonickanon") {
- blocks.nonickanon = true;
- saveBlocks();
- printFeedback("Blocked no-nick anonymous users.");
- } else if (arg === "anon") {
- blocks.noAnon = true;
- saveBlocks();
- printFeedback("Blocked all anonymous users.");
- } else if (arg === "reg") {
- blocks.noReg = true;
- saveBlocks();
- printFeedback("Blocked all registered users.");
- } else if (arg === "tell") {
- blocks.noTell = true;
- saveBlocks();
- printFeedback("Blocked whispers/tells.");
- } else if (arg === "*") {
- blocks.blockAll = true;
- saveBlocks();
- printFeedback("Blocked all users.");
- } else if (arg.startsWith("nickname:")) {
- var nickText = arg.substring("nickname:".length).trim();
- if (nickText) {
- if (!blocks.nicknames.includes(nickText)) {
- blocks.nicknames.push(nickText);
- saveBlocks();
- }
- printFeedback("Blocked nickname containing: " + nickText);
- }
- } else if (arg.startsWith("messagecontains:")) {
- var msgText = arg.substring("messagecontains:".length).trim();
- if (msgText) {
- if (!blocks.messagecontains.includes(msgText)) {
- blocks.messagecontains.push(msgText);
- saveBlocks();
- }
- printFeedback("Blocked messages containing: " + msgText);
- }
- } else {
- var id = parseInt(arg, 10);
- if (!isNaN(id)) {
- if (!blocks.ids.includes(id)) {
- blocks.ids.push(id);
- saveBlocks();
- }
- printFeedback("Blocked ID: " + id);
- } else {
- printFeedback("Invalid block target. Use /block without arguments to see options.");
- }
- }
- } catch (err) {
- console.error(err);
- }
- }
- function unblockCommand(args) {
- try {
- var arg = args.join(" ").trim();
- if (!arg) {
- printFeedback("Usage: /unblock <nickedanon|nonickanon|anon|reg|tell|*|nickname:<text>|messagecontains:<text>|ID>");
- return;
- }
- if (arg === "nickedanon") {
- blocks.nickedanon = false;
- saveBlocks();
- printFeedback("Unblocked nicked anonymous users.");
- } else if (arg === "nonickanon") {
- blocks.nonickanon = false;
- saveBlocks();
- printFeedback("Unblocked no-nick anonymous users.");
- } else if (arg === "anon") {
- blocks.noAnon = false;
- saveBlocks();
- printFeedback("Unblocked all anonymous users.");
- } else if (arg === "reg") {
- blocks.noReg = false;
- saveBlocks();
- printFeedback("Unblocked all registered users.");
- } else if (arg === "tell") {
- blocks.noTell = false;
- saveBlocks();
- printFeedback("Unblocked whispers/tells.");
- } else if (arg === "*") {
- blocks.blockAll = false;
- saveBlocks();
- printFeedback("Unblocked all users.");
- } else if (arg.startsWith("nickname:")) {
- var nickText = arg.substring("nickname:".length).trim();
- var index = blocks.nicknames.indexOf(nickText);
- if (index > -1) {
- blocks.nicknames.splice(index, 1);
- saveBlocks();
- printFeedback("Unblocked nickname containing: " + nickText);
- } else {
- printFeedback("Nickname filter not found: " + nickText);
- }
- } else if (arg.startsWith("messagecontains:")) {
- var msgText = arg.substring("messagecontains:".length).trim();
- var index = blocks.messagecontains.indexOf(msgText);
- if (index > -1) {
- blocks.messagecontains.splice(index, 1);
- saveBlocks();
- printFeedback("Unblocked messages containing: " + msgText);
- } else {
- printFeedback("Message filter not found: " + msgText);
- }
- } else {
- var id = parseInt(arg, 10);
- if (!isNaN(id)) {
- var index = blocks.ids.indexOf(id);
- if (index > -1) {
- blocks.ids.splice(index, 1);
- saveBlocks();
- printFeedback("Unblocked ID: " + id);
- } else {
- printFeedback("ID block not found: " + id);
- }
- } else {
- printFeedback("Invalid unblock target.");
- }
- }
- } catch (err) {
- console.error(err);
- }
- }
- function blockuserCommand(args) {
- try {
- var input = args.join(" ").trim();
- if (!input) {
- printFeedback("Usage: /blockuser <username> [messagecontains:<text>|nickname:<text>|tell]");
- return;
- }
- var match = input.match(/^(.+?)\s+(messagecontains:.+|nickname:.+|tell)$/i);
- var username, condition;
- if (match) {
- username = match[1].trim();
- condition = match[2].trim();
- } else {
- username = input;
- condition = null;
- }
- var userKey = username.toUpperCase();
- if (!blocks.usernames[userKey]) {
- blocks.usernames[userKey] = [];
- }
- var exists = false;
- for (var i = 0; i < blocks.usernames[userKey].length; i++) {
- if (blocks.usernames[userKey][i] && blocks.usernames[userKey][i].condition === condition) {
- exists = true;
- break;
- }
- }
- if (!exists) {
- blocks.usernames[userKey].push({ condition: condition });
- saveBlocks();
- }
- if (condition) {
- printFeedback("Blocked user " + username + " under condition: " + condition);
- } else {
- printFeedback("Blocked user " + username + " unconditionally.");
- }
- } catch (err) {
- console.error(err);
- }
- }
- function unblockuserCommand(args) {
- try {
- var input = args.join(" ").trim();
- if (!input) {
- printFeedback("Usage: /unblockuser <username> [messagecontains:<text>|nickname:<text>|tell]");
- return;
- }
- var match = input.match(/^(.+?)\s+(messagecontains:.+|nickname:.+|tell)$/i);
- var username, condition;
- if (match) {
- username = match[1].trim();
- condition = match[2].trim();
- } else {
- username = input;
- condition = null;
- }
- var userKey = username.toUpperCase();
- if (blocks.usernames[userKey]) {
- if (condition === null) {
- delete blocks.usernames[userKey];
- saveBlocks();
- printFeedback("Unblocked user " + username + " completely.");
- } else {
- var index = -1;
- for (var i = 0; i < blocks.usernames[userKey].length; i++) {
- if (blocks.usernames[userKey][i] && blocks.usernames[userKey][i].condition === condition) {
- index = i;
- break;
- }
- }
- if (index > -1) {
- blocks.usernames[userKey].splice(index, 1);
- if (blocks.usernames[userKey].length === 0) {
- delete blocks.usernames[userKey];
- }
- saveBlocks();
- printFeedback("Removed rule '" + condition + "' for user " + username);
- } else {
- printFeedback("Rule '" + condition + "' not found for user " + username);
- }
- }
- } else {
- printFeedback("User block not found for " + username);
- }
- } catch (err) {
- console.error(err);
- }
- }
- function unblockallCommand() {
- try {
- blocks = {
- ids: [],
- usernames: {},
- nickedanon: false,
- nonickanon: false,
- nicknames: [],
- messagecontains: [],
- blockAll: false,
- noTell: false,
- noAnon: false,
- noReg: false
- };
- saveBlocks();
- printFeedback("Cleared all custom blocks.");
- } catch (err) {
- console.error(err);
- }
- }
- function blocklistCommand() {
- try {
- var lines = ["=== Custom Block List ==="];
- if (blocks.blockAll) lines.push("- Block All (*): Enabled");
- if (blocks.noTell) lines.push("- Block Tells (whispers): Enabled");
- if (blocks.noAnon) lines.push("- Block Anonymous: Enabled");
- if (blocks.noReg) lines.push("- Block Registered: Enabled");
- if (blocks.nickedanon) lines.push("- Block Nicked Anonymous: Enabled");
- if (blocks.nonickanon) lines.push("- Block No-Nick Anonymous: Enabled");
- if (blocks.ids.length > 0) {
- lines.push("- Blocked IDs: " + blocks.ids.join(", "));
- }
- if (blocks.nicknames.length > 0) {
- lines.push("- Nickname Filters: " + blocks.nicknames.map(function(n) { return '"' + n + '"'; }).join(", "));
- }
- if (blocks.messagecontains.length > 0) {
- lines.push("- Message Filters: " + blocks.messagecontains.map(function(m) { return '"' + m + '"'; }).join(", "));
- }
- var userKeys = Object.keys(blocks.usernames);
- if (userKeys.length > 0) {
- lines.push("- Blocked Users:");
- for (var i = 0; i < userKeys.length; i++) {
- var userKey = userKeys[i];
- var rules = blocks.usernames[userKey];
- if (Array.isArray(rules)) {
- var formattedRules = rules.map(function(r) {
- return r && r.condition ? r.condition : "unconditional";
- });
- lines.push(" • " + userKey + " [" + formattedRules.join(", ") + "]");
- }
- }
- }
- if (lines.length === 1) {
- lines.push("(No active blocks)");
- }
- printFeedback(lines.join("\n"));
- } catch (err) {
- console.error(err);
- }
- }
- function init() {
- try {
- loadBlocks();
- w.chat.registerCommand("block", blockCommand, ["target"], "Client-side block command", "messagecontains:spam");
- w.chat.registerCommand("unblock", unblockCommand, ["target"], "Client-side unblock command", "messagecontains:spam");
- w.chat.registerCommand("blockuser", blockuserCommand, ["username", "condition"], "Client-side block user command", "JohnDoe tell");
- w.chat.registerCommand("unblockuser", unblockuserCommand, ["username", "condition"], "Client-side unblock user command", "JohnDoe");
- w.chat.registerCommand("unblockall", unblockallCommand, [], "Client-side clear all blocks", "");
- w.chat.registerCommand("blocklist", blocklistCommand, [], "Client-side show all active blocks", "");
- var originalAddChat = window.addChat;
- window.addChat = function(chatfield, id, type, nickname, message, realUsername, op, admin, staff, color, date, dataObj) {
- try {
- var chatDate = date || Date.now();
- var isRegistered = (type === "user" || type === "user_nick");
- var e = {
- id: id,
- date: chatDate,
- type: type,
- nickname: nickname,
- message: message,
- realUsername: realUsername,
- registered: isRegistered,
- privateMessage: dataObj ? dataObj.privateMessage : null,
- dataObj: dataObj
- };
- if (shouldHideChat(e)) {
- return;
- }
- renderedChats.push(e);
- if (renderedChats.length > 4000) {
- renderedChats.shift();
- }
- originalAddChat(chatfield, id, type, nickname, message, realUsername, op, admin, staff, color, chatDate, dataObj);
- } catch (innerErr) {
- console.error(innerErr);
- }
- };
- } catch (err) {
- console.error(err);
- }
- }
- var checker = setInterval(function() {
- if (typeof w !== "undefined" && typeof w.chat !== "undefined" && typeof w.chat.registerCommand !== "undefined" && typeof w.on !== "undefined" && typeof window.addChat === "function") {
- clearInterval(checker);
- init();
- }
- }, 100);
- })();
Advertisement
Add Comment
Please, Sign In to add comment