aboutsummaryrefslogtreecommitdiff
path: root/static/fluoride.js
blob: c7f3109b66073f9a84e91466a70d53fed49503cf (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// @license magnet:?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt CC0

var reverseActions = {
	"like": "unlike",
	"unlike": "like",
	"retweet": "unretweet",
	"unretweet": "retweet"
};

var statusCache = {};

var csrfToken = "";
var antiDopamineMode = false;

function checkCSRFToken() {
	var tag = document.querySelector("meta[name='csrf_token']");
	if (tag)
		csrfToken = tag.getAttribute("content");
}

function checkAntiDopamineMode() {
	var tag = document.querySelector("meta[name='antidopamine_mode']");
	if (tag)
		antiDopamineMode = tag.getAttribute("content") === "true";
}

function http(method, url, body, type, success, error) {
	var req = new XMLHttpRequest();
	req.onload = function() {
		if (this.status === 200 && typeof success === "function") {
			success(this.responseText, this.responseType);
		} else if (typeof error === "function") {
			error(this.responseText);
		}
	};
	req.onerror = function() {
		if (typeof error === "function") {
			error(this);
		}
	};
	req.open(method, url);
	if (type)
		req.setRequestHeader("Content-Type", type);
	req.send(body);
}

function updateActionForm(id, f, action) {
	f.querySelector("[type='submit']").value = action;
	f.action = "/" + action + "/" + id;
	f.dataset.action = action;
}

function handleLikeForm(id, f) {
	f.onsubmit = function(event) {
		event.preventDefault();

		var action = f.dataset.action;
		var forms = document.
			querySelectorAll(".status-"+id+" .status-like");
		for (var i = 0; i < forms.length; i++) {
			updateActionForm(id, forms[i], reverseActions[action]);
		}

		var body = "csrf_token=" + encodeURIComponent(csrfToken);
		var contentType = "application/x-www-form-urlencoded";
		http("POST", "/fluoride/" + action + "/" + id, 
			body, contentType, function(res, type) {

			if (antiDopamineMode)
				return;
			var data = JSON.parse(res);
			var count = data.data;
			if (count === 0)
				count = "";
			var counts = document.
				querySelectorAll(".status-"+id+" .status-like-count");
			for (var i = 0; i < counts.length; i++) {
				if (count > 0) {
					counts[i].innerHTML = "(" + count + ")";
				} else {
					counts[i].innerHTML = "";
				}
			}
		}, function(err) {
			for (var i = 0; i < forms.length; i++) {
				updateActionForm(id, forms[i], action);
			}
		});
	}
}

function handleRetweetForm(id, f) {
	f.onsubmit = function(event) {
		event.preventDefault();

		var action = f.dataset.action;
		var forms = document.
			querySelectorAll(".status-"+id+" .status-retweet");
		for (var i = 0; i < forms.length; i++) {
			updateActionForm(id, forms[i], reverseActions[action]);
		}

		var body = "csrf_token=" + encodeURIComponent(csrfToken);
		var contentType = "application/x-www-form-urlencoded";
		http("POST", "/fluoride/" + action + "/" + id, 
			body, contentType, function(res, type) {

			if (antiDopamineMode)
				return;
			var data = JSON.parse(res);
			var count = data.data;
			if (count === 0)
				count = "";
			var counts = document.
				querySelectorAll(".status-"+id+" .status-retweet-count");
			for (var i = 0; i < counts.length; i++) {
				if (count > 0) {
					counts[i].innerHTML = "(" + count + ")";
				} else {
					counts[i].innerHTML = "";
				}
			}
		}, function(err) {
			for (var i = 0; i < forms.length; i++) {
				updateActionForm(id, forms[i], action);
			}
		});
	}
}

function isInView(el) {
	var ract = el.getBoundingClientRect();
	if (ract.top > 0 && ract.bottom < window.innerHeight)
		return true;
	return false;
}

function replyToLinkLocal(a) {
	if (!a)
		return;
	var id = a.getAttribute("href");
	if (!id || id[0] != "#")
		return;
	a.onmouseenter = function(event) {
		var id = event.target.getAttribute("href");
		var status = document.querySelector(id);
		if (!status)
			return;
		if (isInView(status)) {
			status.classList.add("highlight");
		} else {
			var copy = status.cloneNode(true);
			copy.id = "reply-to-popup";
			var ract = event.target.getBoundingClientRect();
			if (ract.top > window.innerHeight / 2) {
				copy.style.bottom = (window.innerHeight - 
					window.scrollY - ract.top) + "px";
			}
			event.target.parentElement.appendChild(copy);
		}
	}
	a.onmouseleave = function(event) {
		var popup = document.getElementById("reply-to-popup");
		if (popup) {
			event.target.parentElement.removeChild(popup);    
		} else {
			var id = event.target.getAttribute("href");
			document.querySelector(id)
				.classList.remove("highlight");
		}
	}
}

var inMouseEnter = false;
function replyToLinkRemote(a) {
	a.onmouseenter = function(event) {
		inMouseEnter = true;
		var id = event.target.getAttribute("href");
		var arr = id.replace("/thread", "").split("#");
		if (arr.length < 2)
			return
		id = arr[1].replace("status-", "");
		if (statusCache[id]) {
			var copy = document.createElement("div");
			copy.innerHTML = statusCache[id];
			copy = copy.firstElementChild;
			copy.id = "reply-to-popup";
			var ract = event.target.getBoundingClientRect();
			if (ract.top > window.innerHeight / 2) {
				copy.style.bottom = (window.innerHeight - 
					window.scrollY - ract.top) + "px";
			}
			event.target.parentElement.appendChild(copy);
		} else {
			http("GET", "/fluoride/status/"+id, null, null, function(res, type) {
				statusCache[id] = res;
				if (!inMouseEnter)
					return;
				var copy = document.createElement("div");
				copy.innerHTML = res;
				copy = copy.firstElementChild;
				copy.id = "reply-to-popup";
				var ract = event.target.getBoundingClientRect();
				if (ract.top > window.innerHeight / 2) {
					copy.style.bottom = (window.innerHeight - 
						window.scrollY - ract.top) + "px";
				}
				event.target.parentElement.appendChild(copy);
			}, function(err) {
				console.log("error:", err);
			})
		}
	}
	a.onmouseleave = function(event) {
		inMouseEnter = false;
		var popup = document.getElementById("reply-to-popup");
		if (popup) {
			popup.parentElement.removeChild(popup);    
		} 
	}
}

function handleReplyToLink(a) {
	if (!a)
		return;
	var id = a.getAttribute("href");
	if (!id)
		return;
	if (id[0] === "#") {
		replyToLinkLocal(a);
	} else if (id.indexOf("/thread/") === 0) {
		replyToLinkRemote(a);
	}
}

function handleReplyLink(a) {
	a.onmouseenter = function(event) {
		var id = event.target.getAttribute("href");
		var status = document.querySelector(id);
		if (!status)
			return;
		if (isInView(status)) {
			status.classList.add("highlight");
		} else {
			var copy = status.cloneNode(true);
			copy.id = "reply-popup";
			var ract = event.target.getBoundingClientRect();
			if (ract.left > window.innerWidth / 2) {
				copy.style.right = (window.innerWidth -
					ract.right - 12) + "px";
			}
			event.target.parentElement.appendChild(copy);
		}
	}
	a.onmouseleave = function(event) {
		var popup = document.getElementById("reply-popup");
		if (popup) {
			event.target.parentElement.removeChild(popup);
		} else {
			var id = event.target.getAttribute("href");
			document.querySelector(id).classList.remove("highlight");
		}
	}
}

function handleStatusLink(a) {
	if (a.classList.contains("mention"))
		a.removeAttribute("target");
	else
		a.target = "_blank";
}

function setPos(el, cx, cy, mw, mh) {
	var h = el.clientHeight;
	var w = el.clientWidth;
	var left, top;
	if (cx < mw/2) {
		if (w + cx + 20 < mw) {
			left = cx + 20; 
		} else {
			left = (mw - w);
		}
	} else {
		if (cx - w - 20 > 0) {
			left = cx - w - 20; 
		} else {
			left = 0;
		}
	}
	top = (cy - (h/2));
	if (top < 0) {
		top = 0;
	} else if (top + h > mh) {
		top = (mh - h);
	} 
	el.style.left = left + "px";
	el.style.top = top + "px";
}

var imgPrev = null;
function handleImgPreview(a) {
	a.onmouseenter = function(e) {
		var mw = document.documentElement.clientWidth;
		var mh = document.documentElement.clientHeight - 24;
		var img = document.createElement("img");
		img.id = "img-preview";
		img.src = e.target.getAttribute("href");
		img.style["max-width"] = mw + "px";
		img.style["max-height"] = mh + "px";
		img.onload = function(e2) {
			setPos(e2.target, e.clientX, e.clientY, mw, mh);
		}
		document.body.appendChild(img);
		imgPrev = img;
	}
	a.onmouseleave = function(e) {
		var img = document.getElementById("img-preview");
		if (img)
			document.body.removeChild(img);
		imgPrev = null;
	}
	a.onmousemove = function(e) {
		if (!imgPrev)
			return;
		var mw = document.documentElement.clientWidth;
		var mh = document.documentElement.clientHeight - 24;
		setPos(imgPrev, e.clientX, e.clientY, mw, mh);
	}
}

document.addEventListener("DOMContentLoaded", function() { 
	checkCSRFToken();
	checkAntiDopamineMode();

	var statuses = document.querySelectorAll(".status-container");
	for (var i = 0; i < statuses.length; i++) {
		var s = statuses[i];
		var id = s.dataset.id;

		var likeForm = s.querySelector(".status-like");
		handleLikeForm(id, likeForm);

		var retweetForm = s.querySelector(".status-retweet");
		handleRetweetForm(id, retweetForm);

		var replyToLink = s.querySelector(".status-reply-to-link");
		handleReplyToLink(replyToLink);

		var replyLinks = s.querySelectorAll(".status-reply-link");
		for (var j = 0; j < replyLinks.length; j++) {
			handleReplyLink(replyLinks[j]);
		}

		var links = s.querySelectorAll(".status-content a");
		for (var j = 0; j < links.length; j++) {
			handleStatusLink(links[j]);
		}
	}

	var links = document.querySelectorAll(".user-profile-decription a");
	for (var j = 0; j < links.length; j++) {
		links[j].target = "_blank";
	}

	var links = document.querySelectorAll(".status-media-container .img-link");
	for (var j = 0; j < links.length; j++) {
		handleImgPreview(links[j]);
	}
});

// @license-end