Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/static/overchan.js

556 lines
14 KiB
JavaScript
Raw Normal View History

2017-04-19 16:27:40 +05:00
2017-04-19 20:05:32 +05:00
var _onreadyfuncs = [];
2017-09-30 17:01:21 +05:00
var onready = function(f) {
2017-10-01 01:55:22 +05:00
_onreadyfuncs.push(function() {f();});
2017-09-30 17:01:21 +05:00
};
2017-04-19 20:05:32 +05:00
2017-09-30 17:01:21 +05:00
var ready = function() {
2017-10-01 01:55:22 +05:00
for(var idx = 0; idx < _onreadyfuncs.length; idx++) _onreadyfuncs[idx]();
2017-09-30 17:01:21 +05:00
};
2017-04-19 20:05:32 +05:00
2018-10-26 16:32:17 +05:00
var nntpchan_mod_delete = function(longhash) {
2018-10-26 16:28:24 +05:00
var elem = document.getElementById(longhash);
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if(ajax.readyState == 4)
{
if(ajax.status == 200)
{
// success (?)
var j = JSON.parse(ajax.responseText);
2018-10-26 16:36:27 +05:00
if(j.deleted.length > 0)
2018-10-26 16:28:24 +05:00
{
2018-10-26 16:36:59 +05:00
elem.appendChild(document.createTextNode("deleted: " + j.deleted.join(",")));
2018-10-26 16:28:24 +05:00
}
}
else
{
elem.innerHTML = "post not deleted from server: "+ ajax.statusText;
}
}
};
ajax.open("GET", "/mod/del/"+longhash);
ajax.send();
elem.innerHTML = "";
};
2017-04-19 20:05:32 +05:00
2017-09-30 17:01:21 +05:00
var quickreply = function(shorthash, longhash, url) {
2017-10-01 01:55:22 +05:00
if (!window.location.pathname.startsWith("/t/"))
{
window.location.href = url;
return;
}
var elem = document.getElementById("comment");
if(!elem) return;
elem.value += ">>" + shorthash + "\n";
2017-09-30 17:01:21 +05:00
};
2017-04-19 20:05:32 +05:00
2017-09-30 17:01:21 +05:00
var get_storage = function() {
2017-10-01 01:55:22 +05:00
var st = null;
if (window.localStorage) {
st = window.localStorage;
} else if (localStorage) {
st = localStorage;
}
return st;
2017-09-30 17:01:21 +05:00
};
2017-04-19 20:05:32 +05:00
2017-09-30 17:01:21 +05:00
var setSrcQuery = function(e, q) {
2017-09-30 16:35:01 +05:00
var src = e.src;
var p = src.indexOf('?');
if (p >= 0) {
src = src.substr(0, p);
}
e.src = src + "?" + q
2017-09-30 17:01:21 +05:00
};
2017-04-19 20:05:32 +05:00
2017-09-30 17:01:21 +05:00
var reloadImg = function(el) {
2017-09-30 16:35:01 +05:00
setSrcQuery(el, "reload=" + (new Date()).getTime());
return false;
2017-09-30 17:01:21 +05:00
};
2017-09-30 18:00:10 +05:00
var reloadThreadJSON = function(ourPost) {
2017-09-30 17:42:21 +05:00
var url = window.location.pathname + "/json";
2017-09-30 17:32:51 +05:00
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if(ajax.readyState == 4) {
if(ajax.status == 404) {
console.log("thread gone");
} else if (ajax.status == 200) {
2017-09-30 17:40:18 +05:00
var rootelem = document.getElementById("thread_"+window.location.pathname.split("/")[2]);
2017-09-30 17:32:51 +05:00
var posts = JSON.parse(ajax.responseText);
for(var idx = 0; idx < posts.length; idx ++ )
{
2017-09-30 17:40:18 +05:00
var id = posts[idx].HashLong;
var e = document.getElementById(id);
if(!e) {
e = document.createElement("div");
e.innerHTML = posts[idx].PostMarkup;
rootelem.appendChild(e.childNodes[0]);
e.remove();
2017-09-30 18:00:10 +05:00
if(ourPost && posts[idx].Message_id == ourPost) {
// focus on our post
2017-09-30 18:02:59 +05:00
window.location.hash = id;
2017-09-30 18:00:10 +05:00
}
2017-09-30 17:40:18 +05:00
}
2017-09-30 17:32:51 +05:00
}
}
}
};
ajax.open("GET", url);
ajax.send();
}
2017-09-30 17:01:21 +05:00
// form resubmit
onready(function() {
var submitPost = function(form, elem, cb) {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
2017-09-30 17:15:55 +05:00
var err = "unknown error";
var j = null;
try {
j = JSON.parse(ajax.responseText);
err = j.error || err;
} catch (ex) {
err = "error parsing reply: "+ ex;
}
2017-09-30 17:05:27 +05:00
if(ajax.status == 201) {
2017-09-30 17:01:21 +05:00
// success
2017-09-30 17:46:46 +05:00
form.reset();
2017-09-30 17:15:55 +05:00
cb(null, j);
2017-09-30 17:05:27 +05:00
} else if (ajax.status == 200) {
2017-09-30 17:15:55 +05:00
cb(err, j);
2017-09-30 17:01:21 +05:00
} else {
2017-09-30 17:15:55 +05:00
cb("http "+ajax.status, j);
2017-09-30 17:01:21 +05:00
}
} else {
2017-10-01 20:55:23 +05:00
elem.value += ".";
2017-09-30 17:01:21 +05:00
}
};
2017-10-01 20:46:28 +05:00
var data = new FormData();
data.append("message", document.getElementById("comment").value);
var inputs = form.getElementsByTagName("input");
2017-10-01 20:59:41 +05:00
for(var idx = 0; idx < inputs.length; idx++)
2017-10-01 20:46:28 +05:00
{
2017-10-01 20:59:41 +05:00
var input = inputs[idx];
2017-10-01 20:58:20 +05:00
console.log(input);
2017-10-01 20:46:28 +05:00
if(input.files)
{
2017-10-01 21:01:18 +05:00
for (var i =0 ; i < input.files.length; i++)
{
var file = input.files[i];
2017-10-01 20:46:28 +05:00
data.append(input.name, file, file.name);
2017-10-01 21:01:18 +05:00
}
2017-10-01 20:46:28 +05:00
}
else if(input.name)
data.append(input.name, input.value);
}
2017-10-01 20:55:23 +05:00
console.log("posting...");
ajax.open("POST", form.action+"/json");
2017-10-01 20:46:28 +05:00
ajax.send(data);
2017-09-30 17:01:21 +05:00
};
var elems = document.getElementsByClassName("postbutton");
if(elems && elems.length > 0 && elems[0]) {
var e = elems[0];
var parent = e.parentElement;
2017-09-30 17:03:33 +05:00
var origText = e.value;
2017-09-30 17:01:21 +05:00
e.remove();
2017-10-01 20:55:23 +05:00
e = document.createElement("input");
e.type = "button";
2017-09-30 17:01:21 +05:00
parent.appendChild(e);
2017-10-01 20:55:23 +05:00
e.value = origText;
e.onclick = function(ev) {
console.log("clicked post");
2017-09-30 17:01:21 +05:00
e.disabled = true;
2017-10-01 20:55:23 +05:00
e.value = "posting ";
2017-09-30 17:15:55 +05:00
submitPost(document.forms[0], e, function(err, j) {
2017-09-30 17:46:46 +05:00
if(err) {
var captcha = document.getElementById("captcha_solution");
if(captcha) {
captcha.value = "";
}
}
2017-09-30 17:07:46 +05:00
var msg = err || "posted";
2017-09-30 17:32:51 +05:00
console.log(msg, j.url);
2017-10-01 20:55:23 +05:00
e.value = msg;
2017-09-30 18:06:10 +05:00
if(window.location.pathname === j.url) {
2017-09-30 18:00:10 +05:00
reloadThreadJSON(j.message_id);
2017-09-30 21:10:07 +05:00
} else if (j && j.url) {
2017-09-30 17:32:51 +05:00
// do redirect
window.location.pathname = j.url;
2017-09-30 21:11:58 +05:00
return;
2017-09-30 17:01:21 +05:00
}
2017-09-30 21:13:28 +05:00
var img = document.getElementById("captcha_img");
if (img) {
reloadImg(img);
}
2017-09-30 21:11:58 +05:00
setTimeout(function() {
e.disabled = false;
2017-10-01 20:55:23 +05:00
e.value = origText;
2017-09-30 21:11:58 +05:00
}, 1000);
2017-09-30 17:01:21 +05:00
});
}
}
2017-10-01 01:55:22 +05:00
2017-09-30 17:01:21 +05:00
});
2017-09-30 16:35:01 +05:00
// captcha reload
onready(function(){
2017-09-30 17:01:21 +05:00
2017-09-30 16:35:01 +05:00
var e = document.getElementById("captcha_img");
if (e) {
e.onclick = function() {
2017-09-30 17:01:21 +05:00
reloadImg(e);
2017-09-30 16:35:01 +05:00
};
}
});
2017-04-19 20:05:32 +05:00
2018-05-17 04:39:14 +05:00
// inline reply expand
onready(function() {
var fetchpost = function(url, cb)
{
var parts = url.split("#");
var base = parts[0];
var h = parts[1];
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function()
{
if(ajax.readyState == 4)
{
if(ajax.status == 200)
{
var j = JSON.parse(ajax.responseText);
for(var idx =0 ; idx < j.length; idx ++)
{
if (j[idx].HashLong == h) {
cb(j[idx]);
return;
}
}
cb(null);
}
else
{
cb(null);
}
}
};
ajax.open("GET", base +"json");
ajax.send();
};
2018-05-17 04:50:02 +05:00
var elems = document.getElementsByClassName("backlink");
var inj = function(elem)
2018-05-17 04:39:14 +05:00
{
2018-05-17 04:50:02 +05:00
var showhover = function(parent, url, id)
{
fetchpost(url, function(post) {
var wrapper = document.createElement("div");
var e = document.createElement("div");
e.innerHTML = post.PostMarkup || "post not found"
wrapper.setAttribute('id', id);
wrapper.setAttribute("class", "hover");
wrapper.appendChild(e);
var cl = document.createElement("div");
cl.innerHTML = "[X]";
cl.onclick = function() {
wrapper.remove();
};
wrapper.appendChild(cl);
parent.appendChild(wrapper);
});
};
2018-05-17 04:39:14 +05:00
2018-05-17 04:50:02 +05:00
var hidehover = function(parent, id)
{
var hover = document.getElementById(id);
if(hover) hover.remove();
};
2018-05-17 04:39:14 +05:00
2018-05-17 04:50:02 +05:00
var parent = elem.parentNode.parentNode.id;
2018-05-17 04:39:14 +05:00
var wrapper = document.createElement("div");
elem.parentNode.insertBefore(wrapper, elem);
var el = elem.cloneNode(true);
elem.remove();
2018-05-17 04:50:02 +05:00
var parts = el.href.split("#");
2018-05-17 04:39:14 +05:00
wrapper.appendChild(el);
2018-05-17 04:50:02 +05:00
var id = "hover_"+parts[1]+"_"+parent;
console.log(id);
el.onpointerenter = function() {
showhover(wrapper, el.href, id);
2018-05-17 04:39:14 +05:00
};
2018-05-17 04:50:02 +05:00
el.onpointerleave = function()
2018-05-17 04:39:14 +05:00
{
hidehover(wrapper, id);
};
}
2018-05-17 04:50:02 +05:00
for (var idx = 0 ; idx < elems.length ; idx++) inj(elems[idx]);
2018-05-17 04:39:14 +05:00
});
2017-04-19 20:05:32 +05:00
// rewrite all images to add inline expand
onready(function() {
2017-10-01 01:55:22 +05:00
// is the filename matching an image?
var filenameIsImage = function(fname) {
return /\.(gif|jpeg|jpg|png|webp)/.test(fname.toLowerCase());
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// setup image inlining for 1 element
var setupInlineImage = function(thumb, url) {
if(thumb.inlineIsSetUp) return;
thumb.inlineIsSetUp = true;
var img = thumb.querySelector("img.image");
var expanded = false;
var oldurl = img.src;
thumb.onclick = function() {
if (expanded) {
img.setAttribute("class", "image");
img.src = oldurl;
expanded = false;
} else {
img.setAttribute("class", "expanded-thumbnail");
img.src = url;
expanded = true;
}
return false;
2017-04-19 20:05:32 +05:00
};
2017-10-01 01:55:22 +05:00
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// set up image inlining for all applicable children in an element
var setupInlineImageIn = function(element) {
var thumbs = element.querySelectorAll("a.image_link");
for ( var i = 0 ; i < thumbs.length ; i++ ) {
var url = thumbs[i].href;
if (filenameIsImage(url)) {
// match
setupInlineImage(thumbs[i], url);
}
2017-04-19 20:05:32 +05:00
}
2017-10-01 01:55:22 +05:00
};
// Setup Javascript events for document
setupInlineImageIn(document);
// Setup Javascript events via updater
if (window.MutationObserver) {
var observer = new MutationObserver(function(mutations) {
for (var i = 0; i < mutations.length; i++) {
var additions = mutations[i].addedNodes;
if (additions == null) continue;
for (var j = 0; j < additions.length; j++) {
var node = additions[j];
if (node.nodeType == 1) {
setupInlineImageIn(node);
}
}
}
});
observer.observe(document.body, {childList: true, subtree: true});
}
2017-04-19 20:05:32 +05:00
});
// set up post hider
onready(function() {
2017-10-01 01:55:22 +05:00
var get_hidden_posts = function() {
var st = get_storage();
var prefix = "nntpchan_hide_post_";
return {
all : function() {
var msgids = [];
for ( var k in st) {
if (k.indexOf(prefix) == 0) {
var m = k.substring(prefix.length);
msgids.push(m);
}
}
return msgids;
},
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
add : function (msg) {
st[prefix+msg] = "post";
},
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
del : function (msg) {
st.removeItem(prefix+msg);
}
2017-04-19 20:05:32 +05:00
};
2017-10-01 01:55:22 +05:00
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// is a post elem an OP?
var postIsOP = function(elem) {
var ds = elem.dataset;
return ds && ds.rootmsgid == ds.msgid ;
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
var hide_elem = function(elem, fade) {
if (elem.style) {
elem.style.display = "none";
} else {
elem.style = {display: "none" };
}
elem.dataset.userhide = "yes";
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
var unhide_elem = function(elem) {
elem.style = "";
elem.dataset.userhide = "no";
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// return true if element is hidden
var elemIsHidden = function(elem) {
return elem.dataset && elem.dataset.userhide == "yes";
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// hide a post
var hidepost = function(elem, nofade) {
console.log("hidepost("+elem.dataset.msgid+")");
var posts = get_hidden_posts();
if (posts) {
// add to persitant hide
posts.add(elem.dataset.msgidhash);
}
if(postIsOP(elem)) {
// hide thread it's an OP
var thread = document.getElementById("thread_"+elem.dataset.rootmsgidhash);
if (thread) {
var e = thread.getElementsByClassName("post");
for ( var idx = 0; idx < e.length ; idx ++ ) {
if (e[idx].dataset.msgid == elem.dataset.msgid) continue; // don't apply
hidepost(e[idx]);
2017-04-19 20:05:32 +05:00
}
2017-10-01 01:55:22 +05:00
}
}
// hide attachments and post body
var es = elem.getElementsByClassName("image");
for (var idx = 0; idx < es.length ; idx ++ ) {
hide_elem(es[idx]);
}
es = elem.getElementsByClassName("message_span");
for (var idx = 0; idx < es.length ; idx ++ ) {
hide_elem(es[idx]);
}
es = elem.getElementsByClassName("topicline");
for (var idx = 0; idx < es.length ; idx ++ ) {
hide_elem(es[idx]);
}
elem.dataset.userhide = "yes";
elem.setHideLabel("[show]");
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// unhide a post
var unhidepost = function(elem) {
console.log("unhidepost("+elem.dataset.msgid+")");
var posts = get_hidden_posts();
if (posts) {
// remove from persiting hide
posts.del(elem.dataset.msgidhash);
}
if(postIsOP(elem)) {
var thread = document.getElementById("thread_"+elem.dataset.rootmsgidhash);
if(thread) {
var e = thread.getElementsByClassName("post");
for ( var idx = 0; idx < e.length ; idx ++ ) {
if(e[idx].dataset.msgid == elem.dataset.msgid) continue;
unhidepost(e[idx]);
2017-04-19 20:05:32 +05:00
}
2017-10-01 01:55:22 +05:00
}
}
// unhide attachments and post body
var es = elem.getElementsByClassName("image");
for (var idx = 0; idx < es.length ; idx ++ ) {
unhide_elem(es[idx]);
}
es = elem.getElementsByClassName("message_span");
for (var idx = 0; idx < es.length ; idx ++ ) {
unhide_elem(es[idx]);
}
es = elem.getElementsByClassName("topicline");
for (var idx = 0; idx < es.length ; idx ++ ) {
unhide_elem(es[idx]);
}
elem.dataset.userhide = "no";
elem.setHideLabel("[hide]");
};
2017-04-19 20:05:32 +05:00
2017-10-01 01:55:22 +05:00
// hide a post given a callback that checks each post
var hideposts = function(check_func) {
var es = document.getElementsByClassName("post");
for ( var idx = 0; idx < es.length ; idx ++ ) {
var elem = es[idx];
if(check_func && elem && check_func(elem)) {
hidepost(elem);
}
2017-04-19 20:05:32 +05:00
}
2017-10-01 01:55:22 +05:00
};
// unhide all posts given callback
// if callback is null unhide all
var unhideall = function(check_func) {
var es = document.getElementsByClassName("post");
for (var idx=0 ; idx < es.length; idx ++ ) {
var elem = es[idx];
if(!check_func) { unhide(elem); }
else if(check_func(elem)) { unhide(elem); }
2017-04-19 20:05:32 +05:00
}
2017-10-01 01:55:22 +05:00
};
// inject posthide into page
var inject = function (elem) {
var hider = document.createElement("a");
hider.setAttribute("class", "hider");
elem.setHideLabel = function (txt) {
var e_hider = hider;
e_hider.innerHTML = txt;
};
elem.hidepost = function() {
var e_self = elem;
var e_hider = hider;
hidepost(e_self);
};
elem.unhidepost = function() {
var e_self = elem;
var e_hider = hider;
unhidepost(e_self);
};
elem.isHiding = function() {
var e_self = elem;
return elemIsHidden(e_self);
};
hider.appendChild(document.createTextNode("[hide]"));
hider.onclick = function() {
var e_self = elem;
if(e_self.isHiding()) {
e_self.unhidepost();
} else {
e_self.hidepost();
}
};
elem.appendChild(hider);
};
var posts = document.getElementsByClassName("post");
for (var idx = 0 ; idx < posts.length; idx++ ) {
inject(posts[idx]);
}
// apply persiting hidden posts
posts = get_hidden_posts();
if(posts) {
var all = posts.all();
for ( var idx = 0 ; idx < all.length; idx ++ ) {
var id = all[idx];
var elem = document.getElementById(id);
if(elem)
hidepost(elem, true);
}
}
2017-04-19 20:05:32 +05:00
});