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

75 lines
2.7 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 replace_buttons(){
let known = known_load();
known = normalize_known(known);
known_save(known);
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();
})();