-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoptions.js
84 lines (76 loc) · 2.31 KB
/
options.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function getFlagEmoji(countryCode) {
const codePoints = countryCode
.toUpperCase()
.split('')
.map(char => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
function save(e) {
e.preventDefault();
console.log(document.getElementById("instance").value)
browser.storage.sync.set({
instance: document.getElementById("instance").value
});
document.getElementById("savebtn").textContent = "✅ Saved"
setTimeout(
function() {
document.getElementById("savebtn").textContent = "Save"
},
1500
);
}
function loadAvailableInstances() {
let dropdown = document.getElementById('instance');
dropdown.length = 0;
dropdown.selectedIndex = 0;
// Load from localstorage
function setCurrentChoice(result) {
let instance = result.instance;
if(instance == undefined) {
instance = "https://libreddit.privacydev.net"; // Fallback/default
}
let option;
option = document.createElement('option');
option.text = "➡️ " + instance;
option.value = instance;
dropdown.add(option);
dropdown.selectedIndex = 1;
}
function onError(error) {
console.log(`Error: ${error}`);
}
let getting = browser.storage.sync.get("instance");
getting.then(setCurrentChoice, onError);
// Load from github
const url = 'https://raw.githubusercontent.com/redlib-org/redlib-instances/main/instances.json';
fetch(url)
.then(
function(response) {
if (response.status !== 200) {
console.warn('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
response.json().then(function(data) {
let option;
data = data.instances;
for (let i = 0; i < data.length; i++) {
const url = data[i].url;
const country = data[i].country;
if (url != null){
option = document.createElement('option');
option.text = getFlagEmoji(country) + " " + url;
option.value = url;
dropdown.add(option);
}
}
});
}
)
.catch(function(err) {
console.error('Fetch Error -', err);
});
}
document.querySelector("form").addEventListener("submit", save);
document.addEventListener("DOMContentLoaded", loadAvailableInstances);
document.addEventListener("DOMContentLoaded", restoreOptions);