-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathchat_filter.user.js
141 lines (121 loc) · 4.3 KB
/
chat_filter.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ==UserScript==
// @name Torn: Filter chats
// @namespace lugburz.filter_chat
// @version 0.3.0
// @description Add filtering by keywords to chats. Use double quotes to apply AND rule and no quotes for OR rule; use ! to apply a NOT rule.
// @author Lugburz
// @match https://www.torn.com/*
// @updateURL https://github.com/f2404/torn-userscripts/raw/master/chat_filter.user.js
// @downloadURL https://github.com/f2404/torn-userscripts/raw/master/chat_filter.user.js
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// Array of words that need to be auto filtered; please add words enclosed in quotes, separated by commas, e.g.
// const insults = ['bitch', 'slut'];
const insults = [];
function parse(keyword) {
if (keyword == null || keyword == '')
return [null, null, null];
const ands = keyword.match(/\"[^\""]*\"/g);
if (ands != null) {
for (let i=0; i<ands.length; i++) {
keyword = keyword.replace(ands[i], '');
}
}
keyword = keyword.trim();
if (keyword == null || keyword == '')
return [ands, null, null];
let nots = [];
let ors = [];
const tmp = keyword.split(" ");
if (tmp != null) {
for (let i=0; i<tmp.length; i++) {
if (tmp[i].startsWith('!') && tmp[i].length > 1)
nots.push(tmp[i].substring(1));
else
ors.push(tmp[i]);
}
}
return [ands, ors, nots];
}
function checkAnds(ands, msg) {
if (ands && ands.length) {
for (let i=0; i<ands.length; i++) {
const el = ands[i].replace(/\"/g, '');
if (!msg.toLowerCase().includes(el.toLowerCase()))
return false;
}
}
return true;
}
function checkOrs(ors, msg) {
if (!ors || !ors.length)
return true;
for (let i=0; i<ors.length; i++) {
if (msg.toLowerCase().includes(ors[i].toLowerCase()))
return true;
}
return false;
}
function checkNots(nots, msg) {
if (nots && nots.length) {
for (let i=0; i<nots.length; i++) {
if (msg.toLowerCase().includes(nots[i].toLowerCase()))
return false;
}
}
return true;
}
function filter(content, keyword) {
const p = parse(keyword);
const ands = p[0];
const ors = p[1];
const nots = p[2];
const msgs = $(content).find('div[class^=_message_]');
$(msgs).each(function() {
const msg = $(this).text();
if (checkAnds(ands, msg) && checkOrs(ors, msg) && checkNots(nots, msg) && insults.every(insult => !msg.toLowerCase().includes(insult.toLowerCase())))
$(this).show();
else
$(this).hide();
});
}
function addChatFilter(box, chat) {
const content = $(box).find('div[class^=_chat-box-content_]');
const filter_name = 'filter-' + chat;
if ($(box).find('#'+filter_name).size() > 0) {
$('#'+filter_name).val(GM_getValue(filter_name));
const keyword = $('#'+filter_name).val();
filter(content, keyword);
return;
}
const input = $(box).find('div[class^=_chat-box-input_]');
$(input).css('flex-direction', 'column').css('height', 'auto');
$(input).prepend('<div><label for="filter" style="color: green;">Filter: </label><input type="text" id="' + filter_name + '" name="' + filter_name + '"></div>');
if (insults.length) {
filter(content, '');
}
$(content).bind('DOMNodeInserted DOMNodeRemoved', function() {
$('#'+filter_name).val(GM_getValue(filter_name));
const keyword = $('#'+filter_name).val();
filter(content, keyword);
});
$('#'+filter_name).on('input', function() {
const keyword = $('#'+filter_name).val();
GM_setValue(filter_name, keyword);
filter(content, keyword);
});
}
(function() {
'use strict';
// Your code here...
$('#chatRoot').find('div[class^=_chat-box_]').each((i, box) => {
const chat = $(box).find('div[class^=_chat-box-title_]').attr('title').replace(/%/g, '_');
$(box).ready(addChatFilter(box, chat));
$(box).bind('DOMNodeInserted', function(event) {
if (event.target.className && event.target.className.startsWith('_chat-box-input_')) {
$(box).ready(addChatFilter(box, chat));
}
});
});
})();