on 00a6e7b help sign in
webring/portal.js
74 lines 1.8 kB history raw
1function Portal(sites)
2{
3 this.el = document.createElement("pre");
4 this.sites = sites;
5
6 this.install = function()
7 {
8 document.body.appendChild(this.el);
9 }
11 this.start = function()
12 {
13 this.install();
14 this.el.innerHTML = window.location.hash ? this.redirect() : this.directory();
15 }
17 this.directory = function()
18 {
19 var html = ""
21 for(id in this.sites){
22 var site = this.sites[id]
23 html += `${id}) <a href='${site}'>${site}</a>\n`
24 }
25 html += `\n<a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a>`
27 return html
28 }
30 this.reload = function()
31 {
32 setTimeout(()=>{ window.location.reload() },500)
33 }
35 this.location = function()
36 {
37 return window.location.hash.replace("#","").trim();
38 }
40 this.locate = function()
41 {
42 var hash = this.location();
44 if(hash == "random"){
45 return Math.floor(Math.random()*this.sites.length)
46 }
48 for(var id in this.sites){
49 var site = this.sites[id];
50 if(site.indexOf(hash) >-1){
51 return parseInt(id)
52 }
53 }
54 return -1
55 }
57 this.next = function(loc)
58 {
59 return loc == this.sites.length-1 ? this.sites[0] : this.sites[loc+1];
60 }
62 this.redirect = function()
63 {
64 var location = this.locate();
65 var target = this.next(location);
67 var html = ""
69 html = `Redirecting to ${target}
70 <meta http-equiv="refresh" content="3; url=${target}">
71<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>`
72 return html
73 }