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
|
|
|
|
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(
|
2019-03-15 19:46:18 +01:00
|
|
|
'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 + '`);')();
|
2019-03-15 19:46:18 +01:00
|
|
|
|
2022-03-04 14:03:08 +01:00
|
|
|
async function replace_buttons(){
|
2019-03-15 21:09:22 +01:00
|
|
|
let known = known_load();
|
2019-03-15 19:46:18 +01:00
|
|
|
|
2022-03-04 14:03:08 +01:00
|
|
|
known = normalize_known(known);
|
|
|
|
known_save(known);
|
2019-03-15 19:46:18 +01:00
|
|
|
|
2019-03-15 20:25:45 +01:00
|
|
|
let filtered_top_instances = []
|
2021-12-09 16:27:14 +01:00
|
|
|
for(let instance of top_instances){
|
2019-03-15 20:25:45 +01:00
|
|
|
let found = false;
|
2021-12-09 16:27:14 +01:00
|
|
|
for(let k of known_instances){
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:27:14 +01:00
|
|
|
let instances = known_instances.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){
|
2021-12-09 16:27:14 +01:00
|
|
|
html += template(first, instance['instance'])
|
2019-03-15 19:46:18 +01:00
|
|
|
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);
|
2019-03-15 19:46:18 +01:00
|
|
|
|
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);
|
2019-03-15 19:46:18 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:27:14 +01:00
|
|
|
init_buttons();
|
2019-03-15 19:46:18 +01:00
|
|
|
})();
|