2019-03-15 21:27:10 +01:00
|
|
|
import {SLOTS, normalize_known, known_load, known_save} from './known_instances.js';
|
2019-03-15 20:25:45 +01:00
|
|
|
|
2019-03-15 21:09:22 +01:00
|
|
|
(function instance_buttons(){
|
2019-03-15 19:46:18 +01:00
|
|
|
|
|
|
|
const container = document.querySelector('#mastodon_instance_buttons');
|
|
|
|
const button_template = Function('first', 'instance',
|
|
|
|
'return `' + document.querySelector('#instance_button_template').innerHTML + '`;');
|
|
|
|
const another_button_template = Function(
|
|
|
|
'return `' +
|
|
|
|
document.querySelector('#another_instance_button_template').innerHTML + '`;');
|
|
|
|
const top_instances =
|
|
|
|
Function('return JSON.parse(`' + document.querySelector('#top_instances').innerHTML + '`);')();
|
|
|
|
|
|
|
|
async function get_known(){
|
2019-03-15 21:09:22 +01:00
|
|
|
let known = known_load();
|
2019-03-15 19:46:18 +01:00
|
|
|
if(!known){
|
|
|
|
let resp = await fetch('/api/known_instances');
|
|
|
|
if(resp.ok && resp.headers.get('content-type') == 'application/json'){
|
|
|
|
known = await resp.json();
|
|
|
|
}
|
|
|
|
else {
|
2019-03-15 20:25:45 +01:00
|
|
|
known = [{
|
|
|
|
"instance": "mastodon.social",
|
|
|
|
"hits": 0
|
|
|
|
}];
|
2019-03-15 19:46:18 +01:00
|
|
|
}
|
2019-03-15 21:09:22 +01:00
|
|
|
known_save(known)
|
2019-03-15 19:46:18 +01:00
|
|
|
fetch('/api/known_instances', {method: 'DELETE'})
|
|
|
|
}
|
|
|
|
|
|
|
|
return known;
|
|
|
|
}
|
|
|
|
|
2019-03-15 20:25:45 +01:00
|
|
|
|
2019-03-15 19:46:18 +01:00
|
|
|
async function replace_buttons(){
|
|
|
|
let known = await get_known();
|
|
|
|
|
2019-03-15 21:27:10 +01:00
|
|
|
known = normalize_known(known);
|
2019-03-15 21:09:22 +01:00
|
|
|
known_save(known);
|
2019-03-15 20:25:45 +01:00
|
|
|
|
|
|
|
let filtered_top_instances = []
|
|
|
|
for(let instance of top_instances){
|
|
|
|
let found = false;
|
|
|
|
for(let k of known){
|
2019-03-15 21:23:20 +01:00
|
|
|
if(k['instance'] == instance['instance']){
|
2019-03-15 20:25:45 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found){
|
|
|
|
filtered_top_instances.push(instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let instances = known.concat(filtered_top_instances).slice(0, SLOTS);
|
2019-03-15 19:46:18 +01:00
|
|
|
|
|
|
|
let html = '';
|
|
|
|
|
|
|
|
let first = true;
|
|
|
|
for(let instance of instances){
|
|
|
|
html += button_template(first, instance['instance'])
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
html += another_button_template();
|
|
|
|
|
|
|
|
container.innerHTML = html;
|
|
|
|
}
|
|
|
|
|
|
|
|
replace_buttons();
|
|
|
|
})();
|