-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
340 lines (312 loc) · 8.91 KB
/
content-script.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var interval = 500;
var user_name = null;
var keywords = new Array();
var exclude_mute_plurk = new Array();
var friend_ids;
var show_debug = false;
function load_keywords() {
var p1 = new Promise(function(resolve, reject) {
try {
chrome.extension.sendRequest({
type: 'getOptions'
}, function(response) {
keywords = response.keywords;
exclude_mute_plurk = response.exclude_mute_plurk;
show_debug = response.show_debug;
resolve(response);
if (show_debug) console.debug('keywords:', keywords, '\nexclude_mute_plurk:', exclude_mute_plurk);
});
} catch (e) {
reject(e);
console.error('發生錯誤:', e);
var c = confirm('無法連線到插件後台,請立即重新整理頁面看看,可能有版本更新,繼續將無法正常操作');
if (c) window.location.reload();
}
});
var p2 = fetch("/Users/getCompletion", {
credentials: 'include',
method: 'POST'
}).then(r => r.json()).then(json => {
friend_ids = [];
for (var c in json) {
friend_ids.push(c * 1);
}
if (show_debug) console.debug("friend_list", friend_ids);
return friend_ids;
}).catch(err => {
setTimeout(load_keywords, 1000);
console.error("Get friend list failed");
});
return Promise.all([p1, p2]);
}
function getusername() {
if (user_name == null) {
return getGlobalVariable("GLOBAL.page_user.nick_name").then(function(nick_name) {
user_name = nick_name;
if (show_debug) console.debug('user_name:', user_name);
return nick_name;
});
}
return Promise.resolve(user_name);
}
function set_mute(pid, c) {
/*
is_unread:
0: readed
1: new
2: mute
*/
localScript(function({pid, c}) {
var plurk = PlurksManager.getPlurkById(pid);
if (((c == 0 || c == 1) && plurk.is_unread == 2) ||
(c == 2 && (plurk.is_unread == 0 || plurk.is_unread == 1))) {
PlurksManager.switchMute(pid);
}
}, {
pid: pid,
c: c
});
}
function do_match(text) {
for (i in keywords) {
var k = keywords[i];
if (k.match(/^\/.*\/[igm]*$/)) k = eval(k);
var r = text.match(k);
if (r != null) {
return true;
}
}
return false;
}
function exclude_mute() {
var $me = $("div.plurk.plurk_box"),
plurk_id = getPlurkIdByElement($me),
i;
if (!plurk_id) return;
if ((i = jQuery.inArray(plurk_id, exclude_mute_plurk)) != -1) {
exclude_mute_plurk.splice(i, 1);
set_mute(plurk_id, 2);
$("#excludemute").html("不自動消音");
} else {
exclude_mute_plurk.push(plurk_id);
set_mute(plurk_id, 0);
$("#excludemute").html("自動消音");
}
chrome.extension.sendRequest({
type: 'setOptions',
key: 'exclude_mute',
value: JSON.stringify(exclude_mute_plurk)
}, function(response) {});
console.debug('exclude_mute_plurk:', exclude_mute_plurk);
return false;
}
function mute_user() {
var $me = $("div.plurk.plurk_box"),
plurk_id = getPlurkIdByElement($me),
i;
if (!plurk_id) return;
if ($me.find("td.td_qual span.q_replurks").length > 0) var nameid = $me.find('div.text_holder span:first-child a').attr('href').match(/\/(\w+)/)[1];
else var nameid = $me.find('a.name').attr('href').match(/\/(\w+)/)[1];
if (!user_name) getusername();
if (user_name && user_name == nameid) return;
i = jQuery.inArray(nameid, keywords);
if (i == -1) i = jQuery.inArray("@" + nameid, keywords);
if (i != -1) {
keywords.splice(i, 1);
set_mute(plurk_id, 0);
$("#excludeuser").html("消音此人");
} else {
keywords.push("@" + nameid);
set_mute(plurk_id, 2);
$("#excludeuser").html("不消音此人");
}
chrome.extension.sendRequest({
type: 'setOptions',
key: 'mute_keywords',
value: JSON.stringify(keywords)
}, function(response) {});
console.debug('keywords:', keywords);
return false;
}
function getPlurkIdByElement(element) {
var $me = $(element),
plurk_id = $me.data("pid");
if (!plurk_id) {
plurk_id = $me.attr("id").match(/p(\d+)/);
if (!plurk_id) return;
plurk_id = plurk_id[1];
}
return plurk_id;
}
var last_plurkbox = null;
function getPlurkData(plurk_id) {
return localScript(async function(plurk_id) {
let plurk = PlurksManager.getPlurkById(plurk_id);
function getUserByIdAsync(uid) {
return new Promise(function(resolve, reject) {
jQuery.when(Users.fetchUsersIfNeeded([uid], "gp")).always(function () {
resolve(Users.getUserById(uid));
});
});
}
let user = await getUserByIdAsync(plurk.owner_id);
return {
plurk_id: plurk.plurk_id,
content: plurk.content,
content_raw: plurk.content_raw,
is_unread: plurk.is_unread,
is_replurk: !!plurk.replurker_id,
limited_to: plurk.limited_to,
owner: {
uid: user.uid,
display_name: user.display_name,
nick_name: user.nick_name,
is_me: SiteState.getSessionUser().id == user.uid,
is_friend: user.uid in window.FRIENDS
}
}
}, plurk_id);
}
function filter_plurk(plurk_id) {
if (!plurk_id) return Promise.resolve();
let self = $("#p" + plurk_id),
isPlurkBox = self.length > 0 && self.hasClass("plurk_box");
return getPlurkData(plurk_id).then(function(plurk) {
if (plurk.owner.is_me || plurk.limited_to && plurk.limited_to != "|0|") {
if (isPlurkBox && last_plurkbox != plurk.plurk_id) {
$("#excludemute").html("");
$("#excludeuser").html("");
last_plurkbox = plurk.plurk_id;
}
return false;
}
if (isPlurkBox && last_plurkbox != plurk.plurk_id) {
if ($("#excludemute").length < 1) {
$("#form_holder .info_box .perma_link").parent().append($("<div/>", {
"class": "perma_link",
html: $("<a/>", {
id: "excludemute",
href: "#",
click: exclude_mute
})
}), $("<div/>", {
"class": "perma_link",
html: $("<a/>", {
id: "excludeuser",
href: "#",
click: mute_user
})
}));
}
if (exclude_mute_plurk.indexOf(plurk.plurk_id) != -1) $("#excludemute").html("自動消音");
else $("#excludemute").html("不自動消音");
if (keywords.indexOf(plurk.owner.display_name) != -1 || keywords.indexOf("@" + plurk.owner.nick_name) != -1) $("#excludeuser").html("不消音此人");
else $("#excludeuser").html("消音此人");
last_plurkbox = plurk.plurk_id;
}
if (plurk.is_unread != 1) return false; /* 已讀 */
if (exclude_mute_plurk.indexOf(plurk.plurk_id) != -1) return false; /* exclude plurk */
if (plurk.is_replurk && !plurk.owner.is_friend) return true;
if (do_match(plurk.content_raw) || do_match(plurk.content) || do_match(plurk.owner.display_name) || do_match("@" + plurk.owner.nick_name)) {
return true;
}
}).then(function(mute) {
if (mute) {
set_mute(plurk_id, 2);
}
});
}
function filter_loaded_plurks() {
$('div.plurk').each(function() {
filter_plurk(getPlurkIdByElement(this));
});
}
function open_options() {
chrome.extension.sendRequest({
type: 'openOptions'
}, function(response) {});
return false;
}
function listenBroadcastStation(a, b, cb) {
const id = '__BroadcastStation__' + Math.random();
localScript(function(args) {
BroadcastStation.listen(args.a, args.b, function (res) {
document.dispatchEvent(new CustomEvent(args.id, {
"detail": res ? res.plurk_id : 0
}));
});
}, {a, b, id});
document.addEventListener(id, function(e) {
cb(e.detail);
});
}
function bindEvent() {
listenBroadcastStation("plurk", "update", filter_plurk);
listenBroadcastStation("poll", "new_response", filter_plurk);
$("#timeline_holder").on("click", ".plurk", function(e) {
filter_plurk(getPlurkIdByElement(this));
});
bindAddPlurks();
}
function bindAddPlurks() {
const id = '__PlurkTimelineHolder.addItems__' + Math.random();
localScript(function(id) {
PlurkTimelineHolder.prototype.addItems = new Proxy(PlurkTimelineHolder.prototype.addItems, {
apply: function(target, thisArg, argumentsList) {
var result = target.apply(thisArg, argumentsList);
var pids = argumentsList[0].map(p => p.plurk.plurk_id);
document.dispatchEvent(new CustomEvent(id, {
"detail": pids
}));
return result;
}
});
}, id);
document.addEventListener(id, function(e) {
e.detail.forEach(pid => filter_plurk(pid));
});
}
function plurk_mute_init() {
// check not mobile page
if ($(".plurk-feeds").length < 1) {
localScript(function() {
return SiteState.canEdit();
}).catch(function(err) {
setTimeout(plurk_mute_init, interval);
}).then(function(canEdit) {
if (canEdit == true) {
let p1 = getusername();
let p2 = load_keywords();
return Promise.all([p1, p2]).then(function() {
$(".menu ul li a[href*='/Settings/show?page=theme']").parent().after($("<li/>", {
"class": "sep"
}), $("<li/>", {
html: $("<a/>", {
css: {
color: "#aaa",
cursor: "default"
},
text: "Plurk-Mute_v2"
})
}), $("<li/>", {
html: $("<a/>", {
id: "muteoptions",
href: "#",
text: "自動消音設置",
click: open_options
})
}), $("<li/>", {
"class": "sep"
}));
setInterval(load_keywords, 60000);
bindEvent();
filter_loaded_plurks();
});
}
});
}
if ($("body").hasClass("permaplurk")) {
$(".adsense").hide();
}
}
setTimeout(plurk_mute_init, interval);