forked from ffaristocrat/lazytroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
231 lines (189 loc) · 7.77 KB
/
content.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
lazyTroll = {
profileKeywordList: [],
userNameKeywordList: [],
blockDefaultProfileImage: true,
blockScreenNameIsNumeric: true,
blockProfileTextIsNull: false,
minimumFollowers: 0,
alreadyChecked: [],
blockQueue: [],
blockUser: function(node, userId, screenName, reason) {
// Make sure this isn't someone who got blocked for something else
let youBlock = $(node).attr('data-you-block');
if (youBlock === 'true') return;
// one last check to make sure we're not accidentally
// triggering a block on the wrong tweet
if((screenName !== $(node).attr('data-screen-name')) ||
(userId !== $(node).attr('data-user-id'))) {
return;
}
if (lazyTroll.blockQueue.length === 0) {
setTimeout(lazyTroll.processBlockQueue, 200);
}
// Push a callback function onto the block queue
lazyTroll.blockQueue.push(
lazyTroll.clickBlockUserButton(node, userId, screenName, reason)
);
},
clickBlockUserButton: function(node, userId, screenName, reason) {
console.log('lazyTroll: blockUser ' + screenName + ' (' + userId + '): ' + reason);
// Find the block button on the tweet and click it
let blockButton = $(node)
.find('li.block-link')
.find('button.dropdown-link');
if (!blockButton) {
blockButton = $(node)
.find("li.not-blocked");
}
blockButton.click();
// Hide the modal popup and confirm the block
$("body").removeClass("modal-enabled");
$('div#block-dialog.block-dialog')
.hide()
.find('button.block-button')
.click();
// Destroy the tweet node in case it didn't get hidden
node.remove();
},
checkProfile: function(node, userId, screenName) {
let allClear = true;
// Lightweight call for the profile text used for the hover
let url = 'https://twitter.com/i/profiles/popup?user_id=' + userId;
$.getJSON(url, function (data) {
let followerCount = parseInt($(data['html'])
.find('a[data-element-term="follower_stats"]')
.find('span.ProfileCardStats-statValue')
.attr('data-count'));
if (followerCount && (followerCount < lazyTroll.minimumFollowers)) {
lazyTroll.blockUser(node, userId, screenName, 'minimum-followers');
return false;
}
let profileText = $(data['html']).find('p.bio').text().toLowerCase().trim();
if (profileText) {
if (lazyTroll.blockProfileTextIsNull && profileText === '""') {
lazyTroll.blockUser(node, userId, screenName, 'profile-text-is-null');
allClear = false;
}
else lazyTroll.profileKeywordList.forEach(function (keyword) {
if (profileText.indexOf(keyword) !== -1) {
lazyTroll.blockUser(node, userId, screenName, 'profileText "' + keyword + '"');
allClear = false;
}
});
}
else if (lazyTroll.blockProfileTextIsNull) {
lazyTroll.blockUser(node, userId, screenName, 'profile-text-is-null');
allClear = false;
}
});
return allClear;
},
checkUser: function(node) {
let userId = $(node).attr('data-user-id');
if (!userId) return;
// Don't check profiles that have already been cleared
if (lazyTroll.alreadyChecked.includes(userId)) return;
// Don't check anyone you follow or have already blocked
if ($(node).attr('data-you-follow') === 'true') return;
if ($(node).attr('data-you-block') === 'true') return;
let screenName = $(node).attr('data-screen-name');
if (lazyTroll.blockScreenNameIsNumeric && $.isNumeric(screenName.slice(-8))) {
lazyTroll.blockUser(node, userId, screenName, 'screen-name-is-numeric');
return;
}
let profileImage = $(node).find('img.avatar').attr('src');
if (lazyTroll.blockDefaultProfileImage && profileImage.indexOf('default_profile_images') !== -1) {
lazyTroll.blockUser(node, userId, screenName, 'default-profile-image');
return;
}
// User name has prohibited keywords/characters
let userName = $(node).find('strong.fullname').text().toLowerCase().trim();
let badUserName = false;
lazyTroll.userNameKeywordList.forEach(function (keyword) {
if (userName.indexOf(keyword) !== -1) {
lazyTroll.blockUser(node, userId, screenName, 'userName "' + keyword + '"');
badUserName = true;
}
});
if (badUserName) return;
// Profiles that pass the profile checks
// won't be checked again
if (lazyTroll.checkProfile(node, userId, screenName)) {
lazyTroll.alreadyChecked.push(userId);
}
},
processBlockQueue: function() {
let callback = lazyTroll.blockQueue.pop();
if (callback) callback();
if (lazyTroll.blockQueue.length > 0) {
setTimeout(lazyTroll.processBlockQueue, 200);
}
},
processMutations: function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
lazyTroll.checkForTweets(node);
});
});
},
checkForTweets: function(node) {
$(node)
.find('div.tweet')
.each(function (i, tweet) {
lazyTroll.checkUser(tweet);
});
},
loadConfig: function() {
chrome.storage.local.get({
blockDefaultProfileImage: true,
blockScreenNameIsNumeric: true,
blockProfileTextIsNull: false,
profileKeywordList: "",
userNameKeywordList: "",
minimumFollowers: 0
}, function(items) {
// Fairly straightforward true/false options
lazyTroll.blockDefaultProfileImage = items.blockDefaultProfileImage;
lazyTroll.blockScreenNameIsNumeric = items.blockScreenNameIsNumeric;
lazyTroll.blockProfileTextIsNull = items.blockProfileTextIsNull;
lazyTroll.minimumFollowers = items.minimumFollowers;
// Keywords are forced to lower case and trimmed
// Empty lines are ignored
let profileKeywordKillList = items.profileKeywordList.split('\n');
let userNameKeywordKillList = items.userNameKeywordList.split('\n');
profileKeywordKillList.forEach(function(value) {
let keyword = $.trim(value.toLowerCase());
if (keyword !== "") {
lazyTroll.profileKeywordList.push(keyword);
}
});
userNameKeywordKillList.forEach(function(value) {
let keyword = $.trim(value.toLowerCase());
if (keyword !== "") {
lazyTroll.userNameKeywordList.push(keyword);
}
});
});
},
start: function() {
console.log('lazyTroll: start');
lazyTroll.loadConfig();
// Do a first pass on the existing page
lazyTroll.checkForTweets(document);
// Setup an observer for
lazyTroll.observer = new MutationObserver(lazyTroll.processMutations)
.observe(document.body, {
subtree: true,
childList: true,
});
},
cleanup: function() {
lazyTroll.observer.disconnect()
}
};
$(document).ready(function () {
lazyTroll.start();
});
$('body').bind('beforeunload',function(){
lazyTroll.cleanup();
});