This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutemanager.js
86 lines (70 loc) · 2.3 KB
/
mutemanager.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
(function () {
"use strict";
if (! window.Twilter) {
window.Twilter = {};
}
window.Twilter.MuteManager = MuteManager;
function MuteManager(backup) {
this.muted = backup || {};
if (!this.muted.accounts) {
this.muted.accounts = [];
}
if (!this.muted.keywords) {
this.muted.keywords = [];
}
}
function account_index(account_name) {
var i,
index,
regex = new RegExp("^" + account_name + "$", "i");
for (i = this.muted.accounts.length - 1; i >= 0; i -= 1) {
if (-1 !== (index = this.muted.accounts[i].search(regex))) {
return index;
}
}
return -1;
}
MuteManager.prototype.isMutedAccount = function(account_name) {
return account_index.call(this, account_name) !== -1;
};
MuteManager.prototype.muteAccount = function(account_name) {
if (!this.isMutedAccount(account_name)) {
this.muted.accounts.push(account_name);
return true;
}
return false;
};
MuteManager.prototype.unMuteAccount = function(account_name) {
var index = account_index.call(this, account_name);
if (index !== -1) {
this.muted.accounts.splice(index, 1);
return true;
}
return false;
};
MuteManager.prototype.muteKeyword = function(keyword) {
if (this.muted.keywords.indexOf(keyword) === -1) {
this.muted.keywords.push(keyword);
return true;
}
return false;
};
MuteManager.prototype.unMuteKeyword = function(keyword) {
var index = this.muted.keywords.indexOf(keyword);
if (index !== -1) {
this.muted.keywords.splice(index, 1);
return true;
}
return false;
};
//!\ This method uses early return.
MuteManager.prototype.containsMutedKeyword = function(content) {
var i;
for (i = this.muted.keywords.length - 1; i >= 0; i -= 1) {
if (content.search(new RegExp("\\b" + this.muted.keywords[i] + "\\b", "i")) !== -1) {
return true;
}
}
return false;
};
}());