export class BaseController { constructor() { this.comments_server_url = document.getElementById('comments-client').src; this.origin = window.location.origin; this.protocol = this.getProtocol(); this.domain = this.getDomain(); this.post = window.location.pathname; this.comments_server_host = `${this.protocol}//${this.domain}`; this.parser = new DOMParser(); } async submit(event, route, form = new FormData(event.target)) { event.preventDefault(); try { const response = await fetch(route, { method: "POST", body: form }); response.text().then((response_document) => { this.renderSubmitResponse(response_document); }); } catch(error) {} } getDomain() { return this.comments_server_url.split('/')[2]; } getProtocol() { return this.comments_server_url.split('/')[0]; } htmlFromResponse(body) { return this.parser.parseFromString(body, "text/html"); } async get(route) { try { const response = await fetch(`${this.comments_server_host}${route}`); return response.text().then(this.htmlFromResponse.bind(this)); } catch(error) { return null; } } }