forget-cancellare-vecchi-toot/assets/instance_buttons.js

97 lines
3.4 KiB
JavaScript
Raw Normal View History

import {SLOTS, normalize_known, known_load, known_save} from './known_instances.js';
(function instance_buttons(){
2021-11-10 07:52:01 +01:00
const mastodon_container = document.querySelector('#mastodon_instance_buttons');
2021-11-09 09:23:22 +01:00
const mastodon_button_template = Function('first', 'instance',
'return `' + document.querySelector('#mastodon_instance_button_template').innerHTML + '`;');
const mastodon_another_button_template = Function(
'return `' +
2021-11-09 09:23:22 +01:00
document.querySelector('#mastodon_another_instance_button_template').innerHTML + '`;');
const mastodon_top_instances =
Function('return JSON.parse(`' + document.querySelector('#mastodon_top_instances').innerHTML + '`);')();
2021-12-09 16:27:14 +01:00
const misskey_container = document.querySelector('#misskey_instance_buttons');
2021-11-09 09:23:22 +01:00
const misskey_button_template = Function('first', 'instance',
'return `' + document.querySelector('#misskey_instance_button_template').innerHTML + '`;');
const misskey_another_button_template = Function(
'return `' +
document.querySelector('#misskey_another_instance_button_template').innerHTML + '`;');
const misskey_top_instances =
Function('return JSON.parse(`' + document.querySelector('#misskey_top_instances').innerHTML + '`);')();
async function get_known(){
let known = known_load();
if(!known){
let resp = await fetch('/api/known_instances');
if(resp.ok && resp.headers.get('content-type') == 'application/json'){
known = await resp.json();
}
else {
2021-12-09 16:27:14 +01:00
known = {
mastodon:[{
"instance": "mastodon.social",
"hits": 0
}],
misskey:[
"instance": "misskey.io",
"hits": 0
],
};
}
known_save(known)
fetch('/api/known_instances', {method: 'DELETE'})
}
return known;
}
2021-12-09 16:27:14 +01:00
function replace_buttons(top_instances, known_instances, container,
template, template_another_instance){
let filtered_top_instances = []
2021-12-09 16:27:14 +01:00
for(let instance of top_instances){
let found = false;
2021-12-09 16:27:14 +01:00
for(let k of known_instances){
if(k['instance'] == instance['instance']){
found = true;
break;
}
}
if(!found){
filtered_top_instances.push(instance)
}
}
2021-12-09 16:27:14 +01:00
let instances = known_instances.concat(filtered_top_instances).slice(0, SLOTS);
let html = '';
let first = true;
for(let instance of instances){
2021-12-09 16:27:14 +01:00
html += template(first, instance['instance'])
first = false;
}
2021-12-09 16:27:14 +01:00
html += template_another_instance();
container.innerHTML = html;
}
async function init_buttons(){
let known = await get_known();
known.mastodon = normalize_known(known.mastodon);
known.misskey = normalize_known(known.misskey);
known_save(known);
2021-12-09 16:27:14 +01:00
replace_buttons(mastodon_top_instances, known.mastodon,
mastodon_container, mastodon_button_template,
mastodon_another_button_template);
replace_buttons(misskey_top_instances, known.misskey,
misskey_container, misskey_button_template,
misskey_another_button_template);
}
2021-12-09 16:27:14 +01:00
init_buttons();
})();