summaryrefslogtreecommitdiff
path: root/static/js/controllers/base_controller.js
blob: c5b562f3805933b48d5663adb41c3e1c13988d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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();
    }

    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;
	}
    }
}