2020-01-26 09:21:26 +00:00
|
|
|
// @license magnet:?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt CC0
|
|
|
|
|
2020-01-08 18:16:06 +00:00
|
|
|
var reverseActions = {
|
|
|
|
"like": "unlike",
|
|
|
|
"unlike": "like",
|
|
|
|
"retweet": "unretweet",
|
|
|
|
"unretweet": "retweet"
|
|
|
|
};
|
|
|
|
|
2020-09-03 06:26:32 +00:00
|
|
|
var csrfToken = "";
|
|
|
|
var antiDopamineMode = false;
|
|
|
|
|
|
|
|
function checkCSRFToken() {
|
2020-06-21 17:18:12 +00:00
|
|
|
var tag = document.querySelector("meta[name='csrf_token']");
|
2020-01-25 10:07:06 +00:00
|
|
|
if (tag)
|
2020-09-03 06:26:32 +00:00
|
|
|
csrfToken = tag.getAttribute("content");
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkAntiDopamineMode() {
|
|
|
|
var tag = document.querySelector("meta[name='antidopamine_mode']");
|
|
|
|
if (tag)
|
|
|
|
antiDopamineMode = tag.getAttribute("content") === "true";
|
2020-01-25 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function http(method, url, body, type, success, error) {
|
2020-01-08 18:16:06 +00:00
|
|
|
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.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
req.open(method, url);
|
2020-01-25 10:07:06 +00:00
|
|
|
req.setRequestHeader("Content-Type", type);
|
|
|
|
req.send(body);
|
2020-01-08 18:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateActionForm(id, f, action) {
|
2020-06-21 17:18:12 +00:00
|
|
|
f.querySelector("[type='submit']").value = action;
|
2020-01-08 18:16:06 +00:00
|
|
|
f.action = "/" + action + "/" + id;
|
|
|
|
f.dataset.action = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleLikeForm(id, f) {
|
|
|
|
f.onsubmit = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var action = f.dataset.action;
|
2020-06-21 17:18:12 +00:00
|
|
|
var forms = document.
|
|
|
|
querySelectorAll(".status-"+id+" .status-like");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < forms.length; i++) {
|
|
|
|
updateActionForm(id, forms[i], reverseActions[action]);
|
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
|
2020-09-03 06:26:32 +00:00
|
|
|
var body = "csrf_token=" + encodeURIComponent(csrfToken);
|
2020-01-25 10:07:06 +00:00
|
|
|
var contentType = "application/x-www-form-urlencoded";
|
2020-06-21 17:18:12 +00:00
|
|
|
http("POST", "/fluoride/" + action + "/" + id,
|
|
|
|
body, contentType, function(res, type) {
|
|
|
|
|
2020-09-03 06:26:32 +00:00
|
|
|
if (antiDopamineMode)
|
|
|
|
return;
|
2020-01-08 18:16:06 +00:00
|
|
|
var data = JSON.parse(res);
|
|
|
|
var count = data.data;
|
2020-06-21 17:18:12 +00:00
|
|
|
if (count === 0)
|
2020-01-08 18:16:06 +00:00
|
|
|
count = "";
|
2020-06-21 17:18:12 +00:00
|
|
|
var counts = document.
|
|
|
|
querySelectorAll(".status-"+id+" .status-like-count");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < counts.length; i++) {
|
2020-01-30 13:56:29 +00:00
|
|
|
if (count > 0) {
|
2020-02-01 15:06:06 +00:00
|
|
|
counts[i].innerHTML = "(" + count + ")";
|
2020-01-30 13:56:29 +00:00
|
|
|
} else {
|
2020-02-01 15:06:06 +00:00
|
|
|
counts[i].innerHTML = "";
|
2020-01-30 13:56:29 +00:00
|
|
|
}
|
2020-02-01 15:06:06 +00:00
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
}, function(err) {
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < forms.length; i++) {
|
|
|
|
updateActionForm(id, forms[i], action);
|
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleRetweetForm(id, f) {
|
|
|
|
f.onsubmit = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var action = f.dataset.action;
|
2020-06-21 17:18:12 +00:00
|
|
|
var forms = document.
|
|
|
|
querySelectorAll(".status-"+id+" .status-retweet");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < forms.length; i++) {
|
|
|
|
updateActionForm(id, forms[i], reverseActions[action]);
|
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
|
2020-09-03 06:26:32 +00:00
|
|
|
var body = "csrf_token=" + encodeURIComponent(csrfToken);
|
2020-01-25 10:07:06 +00:00
|
|
|
var contentType = "application/x-www-form-urlencoded";
|
2020-06-21 17:18:12 +00:00
|
|
|
http("POST", "/fluoride/" + action + "/" + id,
|
|
|
|
body, contentType, function(res, type) {
|
|
|
|
|
2020-09-03 06:26:32 +00:00
|
|
|
if (antiDopamineMode)
|
|
|
|
return;
|
2020-01-08 18:16:06 +00:00
|
|
|
var data = JSON.parse(res);
|
|
|
|
var count = data.data;
|
2020-06-21 17:18:12 +00:00
|
|
|
if (count === 0)
|
2020-01-08 18:16:06 +00:00
|
|
|
count = "";
|
2020-06-21 17:18:12 +00:00
|
|
|
var counts = document.
|
|
|
|
querySelectorAll(".status-"+id+" .status-retweet-count");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < counts.length; i++) {
|
2020-01-30 13:56:29 +00:00
|
|
|
if (count > 0) {
|
2020-02-01 15:06:06 +00:00
|
|
|
counts[i].innerHTML = "(" + count + ")";
|
2020-01-30 13:56:29 +00:00
|
|
|
} else {
|
2020-02-01 15:06:06 +00:00
|
|
|
counts[i].innerHTML = "";
|
2020-01-30 13:56:29 +00:00
|
|
|
}
|
2020-02-01 15:06:06 +00:00
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
}, function(err) {
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < forms.length; i++) {
|
|
|
|
updateActionForm(id, forms[i], action);
|
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 08:58:15 +00:00
|
|
|
function isInView(el) {
|
|
|
|
var ract = el.getBoundingClientRect();
|
2020-06-21 17:18:12 +00:00
|
|
|
if (ract.top > 0 && ract.bottom < window.innerHeight)
|
2020-01-26 08:58:15 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-08 13:13:05 +00:00
|
|
|
function handleReplyToLink(a) {
|
|
|
|
if (!a)
|
2020-01-11 10:51:33 +00:00
|
|
|
return;
|
2020-11-08 13:13:05 +00:00
|
|
|
var id = a.getAttribute("href");
|
2020-06-21 17:18:12 +00:00
|
|
|
if (!id || id[0] != "#")
|
2020-01-11 10:51:33 +00:00
|
|
|
return;
|
2020-11-08 13:13:05 +00:00
|
|
|
a.onmouseenter = function(event) {
|
2020-06-21 17:18:12 +00:00
|
|
|
var id = event.target.getAttribute("href");
|
2020-01-11 10:51:33 +00:00
|
|
|
var status = document.querySelector(id);
|
2020-06-21 17:18:12 +00:00
|
|
|
if (!status)
|
2020-01-11 10:51:33 +00:00
|
|
|
return;
|
2020-01-26 08:58:15 +00:00
|
|
|
if (isInView(status)) {
|
|
|
|
status.classList.add("highlight");
|
|
|
|
} else {
|
|
|
|
var copy = status.cloneNode(true);
|
|
|
|
copy.id = "reply-to-popup";
|
2020-04-14 09:32:53 +00:00
|
|
|
var ract = event.target.getBoundingClientRect();
|
|
|
|
if (ract.top > window.innerHeight / 2) {
|
|
|
|
copy.style.bottom = (window.innerHeight -
|
2020-06-21 17:18:12 +00:00
|
|
|
window.scrollY - ract.top) + "px";
|
2020-04-14 09:32:53 +00:00
|
|
|
}
|
2020-01-26 08:58:15 +00:00
|
|
|
event.target.parentElement.appendChild(copy);
|
|
|
|
}
|
2020-01-11 10:51:33 +00:00
|
|
|
}
|
2020-11-08 13:13:05 +00:00
|
|
|
a.onmouseleave = function(event) {
|
2020-01-11 10:51:33 +00:00
|
|
|
var popup = document.getElementById("reply-to-popup");
|
|
|
|
if (popup) {
|
2020-01-26 08:58:15 +00:00
|
|
|
event.target.parentElement.removeChild(popup);
|
|
|
|
} else {
|
2020-06-21 17:18:12 +00:00
|
|
|
var id = event.target.getAttribute("href");
|
2020-01-26 08:58:15 +00:00
|
|
|
document.querySelector(id)
|
|
|
|
.classList.remove("highlight");
|
2020-01-11 10:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 13:13:05 +00:00
|
|
|
function handleReplyLink(a) {
|
|
|
|
a.onmouseenter = function(event) {
|
2020-06-21 17:18:12 +00:00
|
|
|
var id = event.target.getAttribute("href");
|
2020-01-11 10:51:33 +00:00
|
|
|
var status = document.querySelector(id);
|
2020-06-21 17:18:12 +00:00
|
|
|
if (!status)
|
2020-01-11 10:51:33 +00:00
|
|
|
return;
|
2020-01-26 08:58:15 +00:00
|
|
|
if (isInView(status)) {
|
|
|
|
status.classList.add("highlight");
|
|
|
|
} else {
|
|
|
|
var copy = status.cloneNode(true);
|
|
|
|
copy.id = "reply-popup";
|
2020-04-14 09:32:53 +00:00
|
|
|
var ract = event.target.getBoundingClientRect();
|
|
|
|
if (ract.left > window.innerWidth / 2) {
|
|
|
|
copy.style.right = (window.innerWidth -
|
2020-06-21 17:18:12 +00:00
|
|
|
ract.right - 12) + "px";
|
2020-04-14 09:32:53 +00:00
|
|
|
}
|
2020-01-26 08:58:15 +00:00
|
|
|
event.target.parentElement.appendChild(copy);
|
|
|
|
}
|
2020-01-11 10:51:33 +00:00
|
|
|
}
|
2020-11-08 13:13:05 +00:00
|
|
|
a.onmouseleave = function(event) {
|
2020-01-11 10:51:33 +00:00
|
|
|
var popup = document.getElementById("reply-popup");
|
|
|
|
if (popup) {
|
2020-06-21 17:18:12 +00:00
|
|
|
event.target.parentElement.removeChild(popup);
|
2020-01-26 08:58:15 +00:00
|
|
|
} else {
|
2020-06-21 17:18:12 +00:00
|
|
|
var id = event.target.getAttribute("href");
|
|
|
|
document.querySelector(id).classList.remove("highlight");
|
2020-01-11 10:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:48:33 +00:00
|
|
|
function handleStatusLink(a) {
|
|
|
|
if (a.classList.contains("mention"))
|
2020-11-14 17:58:34 +00:00
|
|
|
a.removeAttribute("target");
|
|
|
|
else
|
|
|
|
a.target = "_blank";
|
2020-06-21 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:28:03 +00:00
|
|
|
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;
|
2022-01-02 08:55:21 +00:00
|
|
|
var imgX = 0;
|
|
|
|
var imgY = 0;
|
2021-04-09 12:28:03 +00:00
|
|
|
function handleImgPreview(a) {
|
|
|
|
a.onmouseenter = function(e) {
|
|
|
|
var mw = document.documentElement.clientWidth;
|
|
|
|
var mh = document.documentElement.clientHeight - 24;
|
2022-01-02 08:55:21 +00:00
|
|
|
imgX = e.clientX;
|
|
|
|
imgY = e.clientY;
|
2021-04-09 12:28:03 +00:00
|
|
|
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) {
|
2022-01-02 08:55:21 +00:00
|
|
|
setPos(imgPrev, imgX, imgY, mw, mh);
|
2021-04-09 12:28:03 +00:00
|
|
|
}
|
|
|
|
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;
|
2022-01-02 08:55:21 +00:00
|
|
|
imgX = e.clientX;
|
|
|
|
imgY = e.clientY;
|
|
|
|
setPos(imgPrev, imgX, imgY, mw, mh);
|
2021-04-09 12:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:16:06 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
2020-09-03 06:26:32 +00:00
|
|
|
checkCSRFToken();
|
|
|
|
checkAntiDopamineMode();
|
|
|
|
|
2020-01-08 18:16:06 +00:00
|
|
|
var statuses = document.querySelectorAll(".status-container");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var i = 0; i < statuses.length; i++) {
|
2020-06-21 17:18:12 +00:00
|
|
|
var s = statuses[i];
|
2020-01-08 18:16:06 +00:00
|
|
|
var id = s.dataset.id;
|
|
|
|
|
|
|
|
var likeForm = s.querySelector(".status-like");
|
|
|
|
handleLikeForm(id, likeForm);
|
|
|
|
|
|
|
|
var retweetForm = s.querySelector(".status-retweet");
|
|
|
|
handleRetweetForm(id, retweetForm);
|
2020-01-11 10:51:33 +00:00
|
|
|
|
2020-11-08 13:13:05 +00:00
|
|
|
var replyToLink = s.querySelector(".status-reply-to-link");
|
2020-01-11 10:51:33 +00:00
|
|
|
handleReplyToLink(replyToLink);
|
|
|
|
|
2020-11-08 13:13:05 +00:00
|
|
|
var replyLinks = s.querySelectorAll(".status-reply-link");
|
2020-02-01 15:06:06 +00:00
|
|
|
for (var j = 0; j < replyLinks.length; j++) {
|
|
|
|
handleReplyLink(replyLinks[j]);
|
|
|
|
}
|
2020-06-21 16:48:33 +00:00
|
|
|
|
|
|
|
var links = s.querySelectorAll(".status-content a");
|
|
|
|
for (var j = 0; j < links.length; j++) {
|
|
|
|
handleStatusLink(links[j]);
|
|
|
|
}
|
2020-02-01 15:06:06 +00:00
|
|
|
}
|
2020-11-14 17:58:34 +00:00
|
|
|
|
|
|
|
var links = document.querySelectorAll(".user-profile-decription a");
|
|
|
|
for (var j = 0; j < links.length; j++) {
|
|
|
|
links[j].target = "_blank";
|
|
|
|
}
|
2021-04-09 12:28:03 +00:00
|
|
|
|
|
|
|
var links = document.querySelectorAll(".status-media-container .img-link");
|
|
|
|
for (var j = 0; j < links.length; j++) {
|
|
|
|
handleImgPreview(links[j]);
|
|
|
|
}
|
2020-01-08 18:16:06 +00:00
|
|
|
});
|
2020-01-26 09:21:26 +00:00
|
|
|
|
|
|
|
// @license-end
|