GamUsino = function(custom){ const self = this, default_settings = { nulls : false, default_value : null, autostart : true, timeout : 2000 }, settings = {}; let started = false, ajax_timeout = 2000; const constructor = () => {}; this.start = callback => { const end = status => typeof callback == "function" && callback(status); if(started){ end(false); return false; }; started = true; ajax_timeout = self.settings(["ajax_timeout", "timeout"]); end(true); return true; }; this.nulls = nulls => typeof nulls == "boolean" ? nulls : self.settings("nulls", null, false, false); this.default_value = (_default, nulls) => _default !== undefined && (self.nulls(nulls) || _default !== null) ? _default : self.settings("default_value", null, null, true); this.settings = (keys, inputs, _default, nulls) => { const m = (keys = (typeof keys == "object" && keys instanceof Array ? keys : keys).filter(key => key && typeof key == "string")).length; if(m){ const l = (inputs = (typeof inputs == "object" ? inputs instanceof Array ? inputs : [inputs] : []).concat(custom, settings, default_settings)).length; nulls = self.nulls(nulls); for(let i = 0; i < l; i ++) if(inputs[i] && typeof inputs[i] == "object") for(let j = 0; j < m; j ++) if(inputs[i][keys[j]] !== undefined && (nulls || inputs[i][keys[j]] !== null)) return inputs[i][keys[j]]; }; return self.default_value(_default, nulls); }; this.send = (data, callback) => { let ended = false; const ajax = new XMLHttpRequest(), date = Date.now(), end = error => !ended && (ended = true) && typeof callback == "function" && callback(ajax.responseText && JSON.parse(ajax.responseText) || ajax.responseText, ajax.status, ajax.readyState, error == "OK", error); ajax.open("post", "/api.php", true); ajax.timeout = ajax_timeout; ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded,charset=utf-8"); ajax.onreadystatechange = () => { if(ended) return; if(ajax.readyState == 4) end([301, 302, 304].includes(ajax.stauts) || (ajax.status >= 200 && ajax.status < 300) ? "OK" : "HTTP_ERROR"); else if(Date.now() - date > ajax_timeout) end("FORCED_TIMEOUT"); }; ajax.send("GamUsino=" + btoa(JSON.stringify(data))); ajax.onabort = () => end("ABORTED"); ajax.ontimeout = () => end("TIMEOUT"); ajax.onerror = () => end("ERROR"); return ajax; }; this.confirm = (message, ok_action, no_action) => { const action = confirm(message) ? ok_action : no_action; typeof action == "function" && action(); }; this.add_poll = (item, event) => { const poll_name = item.parentNode.parentNode.querySelector("input").value; if(poll_name) self.confirm("add_poll_sure", () => { self.send({ action : "add_poll", name : poll_name }, response => { console.log(response); }); }); }; constructor(); };