Clean up JS code

This commit is contained in:
Nikita Karamov 2021-08-14 21:56:17 +02:00
parent 2636bedd37
commit 4b545f4a14
No known key found for this signature in database
GPG key ID: 7FCADEDBB1AEBE44
2 changed files with 58 additions and 50 deletions

View file

@ -73,14 +73,14 @@
></textarea> ></textarea>
</section> </section>
<section> <section>
<datalist id="instances_list"></datalist> <datalist id="instanceDatalist"></datalist>
<label for="instance">Choose your Mastodon instance</label> <label for="instance">Choose your Mastodon instance</label>
<input <input
type="url" type="url"
name="instance" name="instance"
id="instance" id="instance"
placeholder="https://" placeholder="https://"
list="instances_list" list="instanceDatalist"
required required
/> />
</section> </section>

View file

@ -16,6 +16,18 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
const INSTANCE_LIST_URL = "https://api.joinmastodon.org/servers";
const $instance = document.getElementById("instance");
const $instanceDatalist = document.getElementById("instanceDatalist");
const $form = document.getElementById("form");
/**
* Adds missing "https://" and ending slash to the URL
*
* @param {string} url URL to normalize
* @return {string} normalized URL
*/
function normalizeUrl(url) { function normalizeUrl(url) {
if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) { if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
url = "https://" + url; url = "https://" + url;
@ -26,72 +38,68 @@ function normalizeUrl(url) {
return url; return url;
} }
const instance = document.getElementById("instance"); function onLoadInstancesError() {
const instances_list = document.getElementById("instances_list"); console.error("Couldn't load instance list");
}
var prefillInstance = window.localStorage.getItem("mastodon_instance"); function onLoadInstancesSuccess() {
if (this.status >= 400) {
return onLoadInstancesError();
}
var paramPairs = window.location.search.substr(1).split("&"); const currentInstance = $instance.value;
var paramPairsLength = paramPairs.length; const instanceDomains = JSON.parse(this.responseText).map((i) => i.domain);
for (var i = 0; i < paramPairsLength; i++) { if (currentInstance && instanceDomains.indexOf(currentInstance) < 0) {
var paramPair = paramPairs[i].split("="); instanceDomains.push(currentInstance);
if (paramPair[0] === "text") { }
document.getElementById("text").value = decodeURIComponent(paramPair[1]); instanceDomains.sort();
} else if (paramPair[0] === "instance") {
prefillInstance = decodeURIComponent(paramPair[1]); for (let i = 0; i < instanceDomains.length; i++) {
const $option = document.createElement("option");
$option.value = normalizeUrl(instanceDomains[i]);
$instanceDatalist.appendChild($option);
} }
} }
function instances_loading_error() { function loadInstances() {
console.error("Failed to fetch servers list from joinmastodon."); if ($instanceDatalist.children.length === 0) {
const request = new XMLHttpRequest();
request.addEventListener("load", onLoadInstancesSuccess);
request.addEventListener("error", onLoadInstancesError);
request.open("GET", INSTANCE_LIST_URL);
request.send();
}
} }
function instances_loaded() { const prefillInstance = window.localStorage.getItem("mastodon_instance");
if (this.status !== 200) {
instances_loading_error();
return;
}
const servers = JSON.parse(this.responseText); const URLParams = window.location.search.substr(1).split("&");
for (let i = 0; i < URLParams.length; i++) {
const chosen_instance = instance.value; const URLParamPair = URLParams[i].split("=");
const domains = servers.map((obj) => obj.domain); if (URLParamPair[0] === "text") {
if (chosen_instance && domains.indexOf(chosen_instance) === -1) { document.getElementById("text").value = decodeURIComponent(URLParamPair[1]);
domains.push(chosen_instance); } else if (URLParamPair[0] === "instance") {
} prefillInstance = decodeURIComponent(URLParamPair[1]);
domains.sort();
for (const domain of domains) {
const opt = document.createElement("option");
opt.value = normalizeUrl(domain);
instances_list.appendChild(opt);
} }
} }
if (prefillInstance != null) { if (prefillInstance != null) {
instance.value = normalizeUrl(prefillInstance); $instance.value = normalizeUrl(prefillInstance);
} }
instance.addEventListener("focus", function (e) { $instance.addEventListener("focus", loadInstances);
if (instances_list.children.length === 0) {
const req = new XMLHttpRequest();
req.addEventListener("load", instances_loaded);
req.addEventListener("error", instances_loading_error);
req.open("GET", "https://api.joinmastodon.org/servers");
req.send();
}
});
document.getElementById("form").addEventListener("submit", function (e) { $form.addEventListener("submit", function (e) {
e.preventDefault(); e.preventDefault();
var text = e.target.elements["text"].value; const text = e.target.elements["text"].value;
var instance = normalizeUrl(e.target.elements["instance"].value); const instanceURL = normalizeUrl(e.target.elements["instance"].value);
var remember = e.target.elements["remember"].checked; const remember = e.target.elements["remember"].checked;
if (remember) { if (remember) {
window.localStorage.setItem("mastodon_instance", instance); window.localStorage.setItem("mastodon_instance", instanceURL);
} }
var shareUrl = instance + "share?text=" + encodeURIComponent(text); window.location.href = instanceURL + "share?text=" + encodeURIComponent(text);
window.location.href = shareUrl;
}); });