mlmym/public/utils.js

401 lines
13 KiB
JavaScript
Raw Normal View History

function request(url, params, callback, errorcallback = function(){}) {
2023-07-03 00:11:13 +00:00
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState != 4 ) { return }
if (xmlHttp.status == 200) {
return callback(xmlHttp.responseText);
}
errorcallback(xmlHttp.responseText);
2023-07-03 00:11:13 +00:00
}
var method = "GET"
if (params) method = "POST"
xmlHttp.open(method, url, true);
if (method = "POST")
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlHttp.send(params);
}
function postClick(e) {
e = e || window.event;
if (e.target.className.indexOf("expando-button") == -1) { return }
var targ = e.currentTarget || e.srcElement || e;
if (targ.nodeType == 3) targ = targ.parentNode;
var bdy = targ.getElementsByClassName("expando")[0]
var btn = targ.getElementsByClassName("expando-button")[0]
if (bdy.className.indexOf("open")>-1) {
bdy.className = 'expando';
btn.className = "expando-button"
2023-07-04 02:43:16 +00:00
targ.getElementsByClassName("embed")[0].innerHTML = ""
2023-07-03 00:11:13 +00:00
} else {
bdy.className = 'expando open';
btn.className = "expando-button open"
2023-07-04 02:43:16 +00:00
var url = targ.getElementsByClassName("url")[0].href
2023-07-10 15:53:15 +00:00
if (id = parseYoutube(url)) {
targ.getElementsByClassName("embed")[0].innerHTML = youtubeIframe(id)
2023-07-04 02:43:16 +00:00
}
2023-07-03 00:11:13 +00:00
}
}
function commentClick(e) {
e = e || window.event;
var targ = e.currentTarget || e.srcElement || e;
if (targ.nodeType == 3) targ = targ.parentNode;
2023-07-05 14:20:07 +00:00
if (e.target.name=="submit") {
2023-07-03 00:11:13 +00:00
e.preventDefault()
var form = e.target.parentNode
if (form) {
data = new FormData(form)
if (("c"+data.get("commentid")) == targ.id) {
} else if (("c"+data.get("parentid")) == targ.id) {
targ = form
} else { return }
2023-07-03 00:11:13 +00:00
params = new URLSearchParams(data).toString()
params += "&" + e.target.name + "=" + e.target.value
params += "&xhr=1"
e.target.disabled = "disabled"
request(targ.target || "", params,
function(res){
targ.outerHTML = res
2023-07-11 14:21:57 +00:00
setup()
},
function(res){
e.target.disabled = ""
})
2023-07-03 00:11:13 +00:00
}
return false
}
if (e.target.className.indexOf("minimize") != -1) {
if (e.target.getAttribute("for") != targ.id) { return }
e.preventDefault()
var btn = targ.getElementsByClassName("minimize")[0]
var children = targ.getElementsByClassName("children")[0]
if (targ.className.indexOf("hidden") == -1) {
targ.className = "comment hidden"
btn.innerHTML = "[+]"
} else {
targ.className = "comment"
btn.innerHTML = "[-]"
}
return false
}
2023-07-05 19:26:26 +00:00
if (e.target.className.indexOf("hidechildren") != -1) {
if (e.target.getAttribute("for") != targ.id) { return }
e.preventDefault()
var btn = targ.getElementsByClassName("hidechildren")[0]
var children = targ.getElementsByClassName("children")[0]
if (children.className.indexOf("hidden") == -1) {
children.className = "children hidden"
btn.className = "hidechildren hidden"
} else {
children.className = "children"
btn.className = "hidechildren"
}
return false
}
if ((e.target.className.indexOf("edit") != -1) ||
2023-07-03 00:11:13 +00:00
(e.target.className.indexOf("source") != -1) ||
(e.target.className.indexOf("reply") != -1)) {
var id = targ.id
if (e.target.getAttribute("for") != id) { return }
e.preventDefault()
request(e.target.href+"&xhr",false, function(res){
targ.outerHTML = res
2023-07-11 14:21:57 +00:00
setup()
2023-07-03 00:11:13 +00:00
})
return false
}
if (e.target.className.indexOf("loadmore") != -1) {
var id = targ.id
if (e.target.getAttribute("for") != id) { return }
e.preventDefault()
var comments = targ.getElementsByClassName("comment")
var skip = []
for (var i = 0; i < comments.length; i++) {
skip.push(comments[i].id)
}
request(e.target.href+"&xhr",false, function(res){
var parent = e.target.parentNode
parent.innerHTML = res
parent.innerHTML = parent.getElementsByClassName("children")[0].innerHTML
var comments = parent.getElementsByClassName("comment")
for (var i = 0; i < skip.length; i++) {
for (var c = 0; c < comments.length; c++) {
if (skip[i] == comments[c].id) {
comments[c].remove()
}
}
}
parent.outerHTML = parent.innerHTML
setup()
})
return false
}
2023-07-03 00:11:13 +00:00
}
function loadMoreComments(e) {
e.preventDefault()
page = e.target.getAttribute("data-page")
var urlParams = new URLSearchParams(window.location.search);
urlParams.set("xhr", "1")
urlParams.set("page", page)
e.target.innerHTML = "loading"
e.target.className = "loading"
request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "",
function(res){
if (res.trim()) {
e.target.parentNode.outerHTML = res + '<div class="morecomments"><a id="lmc" href="" data-page="'+(parseInt(page)+1)+'">load more comments</a></div>'
setup()
} else {
2023-07-21 00:28:07 +00:00
e.target.parentNode.innerHTML = ""
}
}, function() {
e.target.innerHTML = "loading failed"
})
return false;
}
2023-07-05 22:34:46 +00:00
function loadMore(e) {
e.preventDefault()
page = e.target.getAttribute("data-page")
e.target.disabled="disabled"
e.target.value="loading"
var urlParams = new URLSearchParams(window.location.search);
urlParams.set("xhr", "1")
urlParams.set("page", page)
request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "",
function(res){
if (res.trim()) {
2023-07-10 15:53:15 +00:00
e.target.outerHTML = res + '<input id="loadmore" type="submit" data-page="'+(parseInt(page)+1)+'" value="load more">'
if (showimages = document.getElementById("showimages")) {
if (showimages.className == "selected") {
2023-07-10 15:53:15 +00:00
toggleImages(true)
}
2023-07-07 18:51:11 +00:00
}
var loadmore = document.getElementById("loadmore")
2023-07-10 15:53:15 +00:00
loadmore.className = "show"
loadmore.addEventListener("click", loadMore)
setup()
2023-07-07 18:51:11 +00:00
}
else {
e.target.outerHTML = '<input id="end" type="submit" value="" disabled>'
}
},
function(res) {
2023-07-10 15:53:15 +00:00
e.target.outerHTML = '<input id="loadmore" type="submit" data-page="'+parseInt(page)+'" value="loading failed">'
var loadmore = document.getElementById("loadmore")
loadmore.className = "show"
loadmore.addEventListener("click", loadMore)
2023-07-05 22:34:46 +00:00
}
)
2023-07-05 22:34:46 +00:00
return false;
}
2023-07-05 19:26:26 +00:00
function hideAllChildComments(e) {
e.preventDefault()
var comments = document.getElementsByClassName("comment")
2023-07-11 17:59:30 +00:00
if (e.target.innerHTML == "hide all child comments") {
e.target.innerHTML = "show all child comments"
} else {
e.target.innerHTML = "hide all child comments"
}
2023-07-05 19:26:26 +00:00
for (var i = 0; i < comments.length; i++) {
var comment = comments[i]
var btn = comment.getElementsByClassName("hidechildren")
if (!btn.length) { continue }
btn = btn[0]
if (btn.getAttribute("for") != comment.id) { continue }
var children = comment.getElementsByClassName("children")[0]
2023-07-11 17:59:30 +00:00
if (e.target.innerHTML == "show all child comments") {
2023-07-05 19:26:26 +00:00
children.className = "children hidden"
btn.className = "hidechildren hidden"
} else {
children.className = "children"
btn.className = "hidechildren"
}
}
return false
}
2023-07-03 00:11:13 +00:00
function formSubmit(e) {
e = e || window.event;
var targ = e.currentTarget || e.srcElement || e;
e.preventDefault()
var data = new FormData(targ)
params = new URLSearchParams(data).toString()
params += "&" + e.submitter.name + "=" + e.submitter.value
params += "&xhr=1"
e.submitter.disabled = "disabled"
request(targ.target, params,
function(res){
2023-07-20 23:36:12 +00:00
if (data.get("op") == "read_post") {
document.getElementById("p"+data.get("postid")).remove()
return
}
targ.outerHTML = res
2023-07-11 14:21:57 +00:00
setup()
},
function(res){
e.submitter.disabled = ""
}
)
2023-07-03 00:11:13 +00:00
return false
}
2023-07-04 02:43:16 +00:00
2023-07-10 15:53:15 +00:00
function openSettings(e) {
e.preventDefault()
2023-07-10 15:53:15 +00:00
var settings = document.getElementById("settingspopup")
settings.className = "open"
request(e.target.href + "?xhr=1", "", function(res) {
settings.innerHTML = res
var options = document.getElementsByClassName("scripting")
for (var i = 0; i < options.length; i++) {
var input = options[i].getElementsByTagName('input')
if (!input.length) { continue }
if (localStorage.getItem(input[0].name) == "true") {
input[0].checked = "checked"
}
}
2023-07-10 15:53:15 +00:00
document.getElementById("settings").addEventListener("submit", saveSettings)
document.getElementById("closesettings").addEventListener("click", closeSettings)
})
return false
}
2023-07-10 15:53:15 +00:00
function closeSettings(e) {
e.preventDefault()
2023-07-10 15:53:15 +00:00
var settings = document.getElementById("settingspopup")
settings.className = ""
return false
}
2023-07-10 15:53:15 +00:00
function saveSettings(e) {
e = e || window.event;
var targ = e.currentTarget || e.srcElement || e;
var data = new FormData(targ)
e.preventDefault()
var params = new URLSearchParams(data).toString()
request(targ.target, params, function(res) {
["endlessScrolling", "autoLoad"].map(function(x) {
localStorage.setItem(x, data.get(x)=="on")
})
window.location.reload()
})
return false;
}
2023-07-10 15:53:15 +00:00
function parseYoutube(url){
2023-07-05 15:00:14 +00:00
if (url.indexOf("youtu") == -1) return false
2023-07-04 02:43:16 +00:00
var regExp = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
var match = url.match(regExp);
2023-07-04 03:36:25 +00:00
if (match && match.length > 1) {
2023-07-04 02:43:16 +00:00
return match[1]
}
return false
}
2023-07-10 15:53:15 +00:00
function youtubeIframe(id) {
2023-07-04 02:43:16 +00:00
return '<iframe width="560" height="315" src="https://www.youtube.com/embed/'+id+'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>'
}
2023-07-10 15:53:15 +00:00
function showImages(e) {
2023-07-07 18:51:11 +00:00
e = e || window.event;
e.preventDefault()
var targ = e.currentTarget || e.srcElement || e;
var parent = targ.parentNode
if (parent.className == "") {
parent.className = "selected"
2023-07-10 15:53:15 +00:00
toggleImages(true)
2023-07-07 18:51:11 +00:00
} else {
parent.className = ""
2023-07-10 15:53:15 +00:00
toggleImages(false)
2023-07-07 18:51:11 +00:00
}
return false
}
2023-07-10 15:53:15 +00:00
function toggleImages(open) {
2023-07-07 18:51:11 +00:00
var posts = document.getElementsByClassName("post")
for (var i = 0; i < posts.length; i++) {
var btn = posts[i].getElementsByClassName("expando-button")[0]
if (btn.className.indexOf("hidden") != -1) { continue }
var img = posts[i].getElementsByClassName("image")
if (!img.length) { continue }
var bdy = posts[i].getElementsByClassName("expando")[0]
if (open) {
bdy.className = 'expando open showimage';
btn.className = "expando-button open"
} else {
bdy.className = 'expando';
btn.className = "expando-button"
}
}
}
2023-07-10 15:53:15 +00:00
function setup() {
if (showimages = document.getElementById("se")) {
showimages.addEventListener("click", showImages)
}
if (settings = document.getElementById("opensettings")) {
settings.addEventListener("click", openSettings)
}
if (hidechildren = document.getElementById("hidechildren")){
hidechildren.addEventListener("click", hideAllChildComments)
}
if (lmc = document.getElementById("lmc")){
2023-07-13 13:45:51 +00:00
var pager = document.getElementsByClassName("pager")
if (pager.length) {
2023-07-11 17:49:29 +00:00
pager[0].style.display = "none";
}
lmc.addEventListener("click", loadMoreComments)
}
2023-07-09 02:30:22 +00:00
var posts = document.getElementsByClassName("post")
for (var i = 0; i < posts.length; i++) {
2023-07-10 15:53:15 +00:00
posts[i].addEventListener("click", postClick)
2023-07-12 15:23:13 +00:00
var forms = posts[i].getElementsByClassName("link-btn")
for (var f = 0; f < forms.length; f++) {
forms[f].addEventListener("submit", formSubmit)
2023-07-10 15:53:15 +00:00
}
2023-07-09 02:30:22 +00:00
var url = posts[i].getElementsByClassName("url")[0].href
2023-07-10 15:53:15 +00:00
if (id = parseYoutube(url)) {
2023-07-09 02:30:22 +00:00
var btn = posts[i].getElementsByClassName("expando-button")[0]
if (btn.className.indexOf("open") > -1) {
2023-07-10 15:53:15 +00:00
posts[i].getElementsByClassName("embed")[0].innerHTML = youtubeIframe(id)
2023-07-09 02:30:22 +00:00
} else {
btn.className = "expando-button"
}
2023-07-04 02:43:16 +00:00
}
}
2023-07-10 15:53:15 +00:00
var comments = document.getElementsByClassName("comment")
for (var i = 0; i < comments.length; i++) {
comments[i].addEventListener("click", commentClick)
}
2023-07-04 02:43:16 +00:00
}
2023-07-10 15:53:15 +00:00
setup()
2023-07-07 16:30:07 +00:00
if (localStorage.getItem("endlessScrolling") == "true") {
var pager = document.getElementsByClassName("pager")
if (pager.length) pager[0].className = "pager hidden"
var loadmore = document.getElementById("loadmore")
2023-07-10 15:53:15 +00:00
if (loadmore) {
loadmore.className = "show"
loadmore.addEventListener("click", loadMore)
}
}
if (localStorage.getItem("autoLoad") == "true") {
window.onscroll = function(e) {
if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) {
if (localStorage.getItem("endlessScrolling") == "true") {
if (loadmore = document.getElementById("loadmore")) {
loadmore.click()
}
2023-07-07 16:30:07 +00:00
}
if (lmc = document.getElementById("lmc")) {
lmc.click()
}
}
};
}
2023-07-10 15:53:15 +00:00
// delete cookies without HTTPOnly
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;SameSite=None;Secure";
}