webring/portal.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-05-17 00:05:47 +03:00
function Portal(sites)
{
2018-05-17 00:41:09 +03:00
this.el = document.createElement("pre");
2018-05-17 00:05:47 +03:00
this.sites = sites;
this.install = function()
{
2018-05-17 00:41:09 +03:00
document.body.appendChild(this.el);
2018-05-17 00:05:47 +03:00
}
this.start = function()
{
2018-05-17 00:41:09 +03:00
this.install();
this.el.innerHTML = window.location.hash ? this.redirect() : this.directory();
2018-05-17 00:22:20 +03:00
}
this.directory = function()
{
2018-05-17 00:41:09 +03:00
var html = ""
for(id in this.sites){
var site = this.sites[id]
html += `${id}) <a href='${site}'>${site}</a>\n`
}
2018-05-17 02:36:32 +03:00
html += `\n<a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a>`
2018-05-17 00:41:09 +03:00
return html
2018-05-17 00:22:20 +03:00
}
2018-05-17 02:36:32 +03:00
this.reload = function()
{
setTimeout(()=>{ window.location.reload() },500)
}
2018-05-17 00:22:20 +03:00
this.location = function()
{
2018-05-17 00:41:09 +03:00
return window.location.hash.replace("#","").trim();
2018-05-17 00:22:20 +03:00
}
this.locate = function()
{
2018-05-17 00:41:09 +03:00
var hash = this.location();
if(hash == "random"){
return Math.floor(Math.random()*this.sites.length)
}
2018-05-17 00:22:20 +03:00
for(var id in this.sites){
var site = this.sites[id];
if(site.indexOf(hash) >-1){
return parseInt(id)
}
}
return -1
}
this.next = function(loc)
{
return loc == this.sites.length-1 ? this.sites[0] : this.sites[loc+1];
}
this.redirect = function()
{
var location = this.locate();
var target = this.next(location);
2018-05-17 00:41:09 +03:00
var html = ""
2018-05-17 00:05:47 +03:00
2018-05-17 00:41:09 +03:00
html = `Redirecting to ${target}
<meta http-equiv="refresh" content="30; url=${target}">
2018-05-17 03:36:31 +03:00
<a href='' onClick="window.location.reload()">Directory</a> | <a href='#${target}' onClick="portal.reload('random')">Skip</a> | <a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a>`
2018-05-17 00:41:09 +03:00
return html
2018-05-17 00:05:47 +03:00
}
}