forked from XXIIVV/webring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.js
74 lines (60 loc) · 1.74 KB
/
portal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Portal(sites)
{
this.el = document.createElement("pre");
this.sites = sites;
this.install = function()
{
document.body.appendChild(this.el);
}
this.start = function()
{
this.install();
this.el.innerHTML = window.location.hash ? this.redirect() : this.directory();
}
this.directory = function()
{
var html = ""
for(id in this.sites){
var site = this.sites[id]
html += `${id}) <a href='${site}'>${site}</a>\n`
}
html += `\n<a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a>`
return html
}
this.reload = function()
{
setTimeout(()=>{ window.location.reload() },500)
}
this.location = function()
{
return window.location.hash.replace("#","").trim();
}
this.locate = function()
{
var hash = this.location();
if(hash == "random"){
return Math.floor(Math.random()*this.sites.length)
}
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);
var html = ""
html = `Redirecting to ${target}
<meta http-equiv="refresh" content="3; url=${target}">
<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>`
return html
}
}