Skip to content

Commit edb8469

Browse files
author
Azgaar
authored
Merge branch 'master' into dev
2 parents 1849668 + 66ed29c commit edb8469

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,4 +3762,5 @@
37623762
<script defer src="modules/ui/3d.js"></script>
37633763
<script defer src="libs/rgbquant.js"></script>
37643764
<script defer src="libs/jquery.ui.touch-punch.min.js"></script>
3765+
<script defer src="libs/publicstorage.js"></script>
37653766
</body>

libs/publicstorage.js

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// https://github.com/Highbrainer/jeevaneo-js-publicstorage. MIT
2+
const IFRAME_ROOT_URL = "https://publicstorage.neocities.org/shared-iframe.html";
3+
4+
class PublicStorageAccess {
5+
6+
constructor ({debug=false}={}) {
7+
this.uid = this.uniqueId();
8+
this.debug=debug;
9+
}
10+
11+
uniqueId() {
12+
function chr4(){
13+
return Math.random().toString(16).slice(-4);
14+
}
15+
return chr4() + chr4() +
16+
'-' + chr4() +
17+
'-' + chr4() +
18+
'-' + chr4() +
19+
'-' + chr4() + chr4() + chr4();
20+
}
21+
22+
_debug(msg) {
23+
if(this.debug) {
24+
if(console && console.debug) {
25+
console.debug(msg);
26+
}
27+
}
28+
}
29+
30+
prepareIFrame() {
31+
const that = this;
32+
const iframe = document.createElement("iframe");
33+
iframe.id=that.uid;
34+
iframe.src=IFRAME_ROOT_URL + "?uid=init-"+that.uid;
35+
iframe.style.cssText="display:none;";
36+
return new Promise(function(resolve, reject) {
37+
window.addEventListener('message', function mafunc(tkn) {
38+
39+
if (IFRAME_ROOT_URL.indexOf(tkn.origin)<0) {
40+
return;
41+
}
42+
43+
try {
44+
const packet = JSON.parse(tkn.data);
45+
46+
if(!(packet.frameId === "init-" + that.uid)) {
47+
// ignore
48+
return;
49+
}
50+
51+
if(packet.ready) {
52+
resolve(iframe);
53+
}
54+
} catch (e) {
55+
reject(tkn.data);
56+
}
57+
window.removeEventListener('message', mafunc);
58+
});
59+
onLoadThen().then(() => {
60+
document.getElementsByTagName("body")[0].appendChild(iframe);
61+
});
62+
63+
setTimeout(()=>reject(`Request ${that.uid} TIMEOUTED!`), 20000);
64+
});
65+
}
66+
67+
access(access, prop, value = null, level = "local") {
68+
69+
if(!(access === "get" || access === "set" || access === "delete")) {
70+
throw new Error("access can only be 'set', 'get' or 'delete' - not '" + access + "'");
71+
}
72+
73+
if (!prop) {
74+
throw new Error("Prop name is mandatory");
75+
}
76+
77+
if(!(level === "local" || level === "session")) {
78+
throw new Error("level can only be 'session' or 'local' - not '" + access + "'");
79+
}
80+
81+
const that = this;
82+
83+
const promise = new Promise(function(resolve, reject) {
84+
that.prepareIFrame().then(iframe => {
85+
window.addEventListener('message', function mafunc(tkn) {
86+
if (IFRAME_ROOT_URL.indexOf(tkn.origin)<0) {
87+
return;
88+
}
89+
try {
90+
var packet = JSON.parse(tkn.data);
91+
92+
if(!(packet.uid === that.uid)) {
93+
// ignore
94+
return;
95+
}
96+
resolve(packet.body);
97+
} catch (e) {
98+
reject(tkn.data);
99+
}
100+
iframe.parentNode.removeChild(iframe);
101+
window.removeEventListener('message', mafunc);
102+
});
103+
104+
const request = {uid:that.uid, access:access, prop:prop, value:value, level:level};
105+
iframe.contentWindow.postMessage(JSON.stringify(request), '*');
106+
setTimeout(()=>reject("TIMEOUTED!"), 20000);
107+
});
108+
});
109+
return promise;
110+
}
111+
112+
}
113+
114+
function __createDebugIFrame() {
115+
onLoadThen().then(function(){
116+
const iframe = document.createElement("iframe");
117+
iframe.src=IFRAME_ROOT_URL + "?for-debug-only";
118+
iframe.style.cssText="display:none;";
119+
document.getElementsByTagName("body")[0].appendChild(iframe);
120+
});
121+
}
122+
123+
class PublicStorage {
124+
125+
constructor({debug=false}={}) {
126+
if(debug) {
127+
__createDebugIFrame();
128+
}
129+
}
130+
131+
sessionGet(prop) {
132+
return new PublicStorageAccess().access("get", prop, null, "session");
133+
}
134+
sessionSet(prop, value) {
135+
return new PublicStorageAccess().access("set", prop, value, "session");
136+
}
137+
sessionUnset(prop) {
138+
return new PublicStorageAccess().access("delete", prop, null, "session");
139+
}
140+
localGet(prop) {
141+
return new PublicStorageAccess().access("get", prop, null, "local");
142+
}
143+
localSet(prop, value) {
144+
return new PublicStorageAccess().access("set", prop, value, "local");
145+
}
146+
localUnset(prop) {
147+
return new PublicStorageAccess().access("delete", prop, null, "local");
148+
}
149+
get(prop) {
150+
return this.localGet(prop);
151+
}
152+
set(prop, value) {
153+
return this.localSet(prop, value);
154+
}
155+
unset(prop) {
156+
return this.localUnset(prop);
157+
}
158+
}
159+
160+
const publicstorage = new PublicStorage();
161+
162+
function onLoadThen() {
163+
return new Promise(function(resolve, reject) {
164+
if (window) {
165+
if(document.getElementsByTagName('BODY')[0]) {
166+
resolve();
167+
} else {
168+
registerOnLoad(function unregisterme() {
169+
resolve();
170+
window.removeEventListener('load', unregisterme);
171+
});
172+
}
173+
}
174+
setTimeout(function() {reject(new Error("Timeout waiting for onLoad!"));}, 10000);
175+
});
176+
}
177+
178+
function registerOnLoad(lambda) {
179+
if (window.addEventListener) {
180+
window.addEventListener('load', lambda);
181+
} else if (window.attachEvent) {
182+
window.attachEvent('onload', lambda);
183+
}
184+
}
185+
186+
onLoadThen().then(() => window.publicstorage = publicstorage).catch(e=> console.error(e));
187+
//export {onLoadThen, PublicStorage, publicstorage as default}
188+
// module.exports = onLoadThen();

modules/ui/3d.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)