(Un)follow and boost/like buttons via javascript

Works on #78
This commit is contained in:
Mouse Reeve
2020-03-21 14:29:39 -07:00
parent 1ec515034c
commit e0bd8200ad
8 changed files with 49 additions and 37 deletions

View File

@ -6,10 +6,14 @@ function hide_element(element) {
function interact(e) {
e.preventDefault();
ajaxPost(e.target);
if (e.target.className.includes('active')) {
e.target.className = '';
} else {
e.target.className += ' active';
var identifier = e.target.getAttribute('data-id');
var elements = document.getElementsByClassName(identifier);
for (var i = 0; i < elements.length; i++) {
if (elements[i].className.includes('hidden')) {
elements[i].className = elements[i].className.replace('hidden', '');
} else {
elements[i].className += ' hidden';
}
}
return true;
}
@ -21,26 +25,9 @@ function comment(e) {
return true;
}
function ajaxPost(form, callback) {
// jeez. https://stackoverflow.com/questions/33021995
var url = form.action;
var xhr = new XMLHttpRequest();
var params = [].filter.call(form.elements, function(el) {
return typeof(el.checked) === 'undefined' || el.checked;
})
.filter(function(el) { return !!el.name; })
.filter(function(el) { return el.disabled; })
.map(function(el) {
return encodeURIComponent(el.name) + '=' + encodeURIComponent(el.value);
}).join('&');
xhr.open('POST', url);
xhr.setRequestHeader('Content-type', 'application/x-form-urlencoded');
xhr.setRequestHeader('X-CSRFToken', csrf_token);
if (callback) {
xhr.onload = callback.bind(xhr);
}
xhr.send(params);
function ajaxPost(form) {
fetch(form.action, {
method : "POST",
body: new FormData(form)
});
}

View File

@ -0,0 +1 @@
module.exports=function(e,n){return n=n||{},new Promise(function(t,r){var s=new XMLHttpRequest,o=[],u=[],i={},a=function(){return{ok:2==(s.status/100|0),statusText:s.statusText,status:s.status,url:s.responseURL,text:function(){return Promise.resolve(s.responseText)},json:function(){return Promise.resolve(JSON.parse(s.responseText))},blob:function(){return Promise.resolve(new Blob([s.response]))},clone:a,headers:{keys:function(){return o},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var l in s.open(n.method||"get",e,!0),s.onload=function(){s.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){o.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(a())},s.onerror=r,s.withCredentials="include"==n.credentials,n.headers)s.setRequestHeader(l,n.headers[l]);s.send(n.body||null)})};