Skip to content

Commit 8f6dcfe

Browse files
committed
fix: DOMContentLoaded and load events in page loader
1 parent 5b6041c commit 8f6dcfe

4 files changed

Lines changed: 116 additions & 56 deletions

File tree

src/server.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ if (config.disguiseFiles) {
195195
if (!shouldNotHandle.test(path)) exemptDirs.push(path.slice(1));
196196
exemptPages = exemptPages.concat(exemptDirs);
197197
if (pages.default === 'login') exemptPages.push('');
198+
198199
app.addHook('preHandler', (req, reply, done) => {
199200
if (req.params.modified) return done();
200201
const reqPath = new URL(req.url, serverUrl).pathname.slice(
@@ -218,6 +219,7 @@ if (config.disguiseFiles) {
218219
if (req.params.path) req.params.path = getActualPath(req.params.path);
219220
if (req.params['*']) req.params['*'] = getActualPath(req.params['*']);
220221
reply.type(supportedTypes[disguise]);
222+
reply.header('Access-Control-Allow-Origin', 'null');
221223
}
222224
return done();
223225
});

views/assets/css/styles-1755147161.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ details[open] summary {
800800
transform: translateX(14px);
801801
}
802802

803+
#csel #newtab {
804+
padding: 2.25px 4.5px;
805+
margin: 0;
806+
}
807+
803808
select {
804809
margin: 0 0 0 10px;
805810
padding: 10px;

views/assets/js/loader.js

Lines changed: 93 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,102 @@
11
(() => {
2-
const _addEventListener = addEventListener,
3-
windowEventListeners = [],
2+
const windowEventListeners = [],
43
documentEventListeners = [],
54
loadedModules = [];
5+
let _addEventListener = addEventListener,
6+
_document = document,
7+
_window = window,
8+
origin = location;
69
_addEventListener('keydown', (event) => {
710
if (event.ctrlKey && event.code === 'KeyM' && event.isTrusted) {
811
if (localStorage.getItem('{{hu-lts}}-loader-key') !== navigator.userAgent)
912
localStorage.setItem('{{hu-lts}}-loader-key', navigator.userAgent);
1013
else localStorage.removeItem('{{hu-lts}}-loader-key');
11-
location.reload();
14+
_window.location.reload();
1215
}
1316
});
14-
Window.prototype.addEventListener = (...args) => {
15-
windowEventListeners.push([...args]);
16-
return _addEventListener(...args);
17-
};
18-
Document.prototype.addEventListener = (...args) => {
19-
documentEventListeners.push([...args]);
20-
return _addEventListener.bind(document)(...args);
17+
const setListeners = () => {
18+
const currentWindow = _window,
19+
currentDoc = _document;
20+
currentWindow.Window.prototype.addEventListener = (...args) => {
21+
windowEventListeners.push([...args]);
22+
return _addEventListener.bind(currentWindow)(...args);
23+
};
24+
currentWindow.Document.prototype.addEventListener = (...args) => {
25+
documentEventListeners.push([...args]);
26+
return _addEventListener.bind(currentDoc)(...args);
27+
};
2128
};
22-
const displayErrorPage = (overwrite = false) => {
23-
document.body.removeAttribute('style');
24-
if (overwrite) document.body.replaceWith(document.createElement('body'));
25-
document.body.insertAdjacentHTML(
29+
setListeners();
30+
const displayErrorPage = (overwrite = false, currentDoc = _document) => {
31+
currentDoc.body.removeAttribute('style');
32+
if (overwrite)
33+
currentDoc.body.replaceWith(currentDoc.createElement('body'));
34+
currentDoc.body.insertAdjacentHTML(
2635
'afterbegin',
2736
'<center><h1>403 Forbidden</h1></center><center>You don’t have permission to access this page.</center><hr><center>nginx</center>'
2837
);
29-
let head = document.createElement('head'),
30-
title = document.createElement('title');
38+
let head = currentDoc.createElement('head'),
39+
title = currentDoc.createElement('title');
3140
title.textContent = '500 Internal Server Error';
3241
head.appendChild(title);
33-
document.head.replaceWith(head);
34-
if (document.currentScript) document.currentScript.remove();
42+
currentDoc.head.replaceWith(head);
43+
if (currentDoc.currentScript) currentDoc.currentScript.remove();
3544
};
36-
if (localStorage.getItem('{{hu-lts}}-loader-key') !== navigator.userAgent)
45+
if (
46+
_window.localStorage.getItem('{{hu-lts}}-loader-key') !==
47+
navigator.userAgent
48+
)
3749
return displayErrorPage();
3850
const lastUpdated = '{{cacheVal}}',
3951
retrieveUrl = (pathname) => {
40-
let capturedUrl = new URL(pathname, location),
52+
let capturedUrl = new URL(pathname, origin),
4153
capturedParams = new URLSearchParams(capturedUrl.search);
4254
capturedParams.set('cache', lastUpdated);
4355
capturedUrl.search = capturedParams.toString();
4456
return capturedUrl;
4557
};
4658

59+
const loadAttachments = () => {
60+
if (_document.readyState === 'complete') loadPage()();
61+
else addEventListener('load', loadPage());
62+
_addEventListener('popstate', () => {
63+
if (_window.location.href.includes('#')) return;
64+
_window.console.clear();
65+
loadPage(location, false)();
66+
});
67+
};
68+
4769
const loadPage =
48-
(destination = location, pushState = true) =>
70+
(destination = origin, pushState = true) =>
4971
() => {
50-
fetch(
51-
retrieveUrl(
52-
destination.pathname.replace(/\/+/g, '/').replace(/\/$/, '') + '.ico'
53-
),
54-
{ mode: 'same-origin' }
55-
)
72+
_window
73+
.fetch(
74+
retrieveUrl(
75+
destination.pathname.replace(/\/+/g, '/').replace(/\/$/, '') +
76+
'.ico'
77+
),
78+
{ mode: 'same-origin' }
79+
)
5680
.then((response) => {
5781
let i = windowEventListeners.length - 1;
5882
for (; i >= 0; i--) {
59-
removeEventListener(...windowEventListeners[i]);
83+
_window.removeEventListener(...windowEventListeners[i]);
6084
windowEventListeners.pop();
6185
}
6286
for (i = documentEventListeners.length - 1; i >= 0; i--) {
63-
document.removeEventListener(...documentEventListeners[i]);
87+
_document.removeEventListener(...documentEventListeners[i]);
6488
documentEventListeners.pop();
6589
}
66-
if (destination !== location && pushState) {
67-
console.clear();
90+
if (destination !== _window.location && pushState) {
91+
_window.console.clear();
6892
if (response.status === 200) {
69-
history.pushState({}, '', retrieveUrl(destination));
70-
} else return location.assign(new URL(destination, location));
93+
if (_window === window)
94+
_window.history.pushState({}, '', retrieveUrl(destination));
95+
} else return _window.location.assign(new URL(destination, origin));
7196
}
7297
response.blob().then((blob) => {
73-
new Response(
74-
blob.stream().pipeThrough(new DecompressionStream('gzip'))
98+
new _window.Response(
99+
blob.stream().pipeThrough(new _window.DecompressionStream('gzip'))
75100
)
76101
.text()
77102
.then((text) => {
@@ -111,7 +136,23 @@
111136
if (replacement.childNodes.length > 0)
112137
loadNextScript(isDefer, replacement)();
113138
} else if (!isDefer && !waitForHead) loadNextScript(true)();
114-
else reachedEnd = true;
139+
else {
140+
reachedEnd = true;
141+
if (waitForHead) return;
142+
[
143+
...windowEventListeners,
144+
...documentEventListeners,
145+
].forEach((listenerParams) => {
146+
if (listenerParams[0] === 'DOMContentLoaded')
147+
listenerParams[1](
148+
new _window.Event('DOMContentLoaded')
149+
);
150+
});
151+
windowEventListeners.forEach((listenerParams) => {
152+
if (listenerParams[0] === 'load')
153+
listenerParams[1](new _window.Event('load'));
154+
});
155+
}
115156
};
116157
const recursiveClone = (node) => {
117158
if (node.nodeType !== Node.ELEMENT_NODE) return node;
@@ -149,7 +190,7 @@
149190
event.preventDefault();
150191
if (attrValue === '{{route}}{{/}}')
151192
attrValue = '{{route}}{{/index}}';
152-
loadPage(new URL(attrValue, location))();
193+
loadPage(new URL(attrValue, origin))();
153194
});
154195
else if (nodeName === 'link') {
155196
src = retrieveUrl(node.href);
@@ -213,6 +254,17 @@
213254
headScripts++;
214255
}
215256
}
257+
/*
258+
if (node.id === 'newtab')
259+
elementCopy.addEventListener('click', () => {
260+
origin = new URL(location.href);
261+
_window = open();
262+
_addEventListener = _addEventListener.bind(_window);
263+
_document = _window.document;
264+
setListeners();
265+
loadAttachments();
266+
});
267+
*/
216268
return elementCopy;
217269
};
218270
let currentType = currentDoc.doctype,
@@ -253,22 +305,16 @@
253305

254306
loadNextScript(false)();
255307
})(
256-
document,
257-
new DOMParser().parseFromString(text, 'text/html')
308+
_document,
309+
new _window.DOMParser().parseFromString(text, 'text/html')
258310
);
259311
});
260312
});
261313
})
262314
.catch((error) => {
263-
console.log(error);
315+
_window.console.log(error);
264316
displayErrorPage(true);
265317
});
266318
};
267-
if (document.readyState === 'complete') loadPage()();
268-
else addEventListener('load', loadPage());
269-
_addEventListener('popstate', () => {
270-
if (location.href.includes('#')) return;
271-
console.clear();
272-
loadPage(location, false)();
273-
});
319+
loadAttachments();
274320
})();

views/pages/misc/deobf/settings.html

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
<div class="csel-container-left">
88
<p class="cseltitle">{{mask}}{{Browser Tab Cloak}}</p>
99
<form id="titleform" class="cloakform">
10-
<input type="text" placeholder="Enter a tab title here..." spellcheck="false" />
10+
<input
11+
type="text"
12+
placeholder="Enter a tab title here..."
13+
spellcheck="false"
14+
/>
1115
<input type="submit" value="Apply" />
1216
</form>
1317
<form id="iconform" class="cloakform">
14-
<input type="text" placeholder="Enter an icon URL here..." spellcheck="false" />
18+
<input
19+
type="text"
20+
placeholder="Enter an icon URL here..."
21+
spellcheck="false"
22+
/>
1523
<input type="submit" value="Apply" />
1624
</form>
17-
<a
18-
href="{{route}}{{/questions}}"
19-
>{{mask}}{{Find Icon URL}}</a
20-
>
25+
<a href="{{route}}{{/questions}}">{{mask}}{{Find Icon URL}}</a>
2126
<p class="cseltitle">{{mask}}{{Wisp Protocol Transport}}</p>
2227
<div class="radio-group {{wisp-transport}}-list">
2328
<label>
@@ -60,7 +65,7 @@
6065
<label>
6166
<p>
6267
Enable Autocomplete
63-
<span class="default-badge">Proxied Suggestions</span>
68+
<span class="default-badge">{{mask}}{{Proxied Suggestions}}</span>
6469
</p>
6570
<input type="checkbox" class="switch useac" checked />
6671
</label>
@@ -83,7 +88,7 @@
8388
</label>
8489
<label>
8590
<p>Dark</p>
86-
<input type="radio" name="theme" value="dark" checked/>
91+
<input type="radio" name="theme" value="dark" checked />
8792
</label>
8893
</div>
8994
<p class="cseltitle">Icon Presets</p>
@@ -98,7 +103,9 @@
98103
<div class="radio-group">
99104
<label>
100105
<p>
101-
{{mask}}{{Eruda Devtools}}<span class="alt-badge">C<wbr>TRL+SH<wbr>IFT+I</span>
106+
{{mask}}{{Eruda Devtools}}<span class="alt-badge"
107+
>C<wbr />TRL+SH<wbr />IFT+I</span
108+
>
102109
</p>
103110
<input type="checkbox" class="switch eruda" />
104111
</label>

0 commit comments

Comments
 (0)