Skip to content

Commit

Permalink
fix: loader
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jan 24, 2022
1 parent 69b5300 commit fdb9050
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion userscript.meta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Krunker Editor+
// @version 4.13.1
// @version 4.13.2
// @author Jakob#8686
// @downloadURL https://github.com/j4k0xb/Krunker-Editor-Plus/raw/master/userscript.user.js
// @updateURL https://github.com/j4k0xb/Krunker-Editor-Plus/raw/master/userscript.meta.js
Expand Down
61 changes: 35 additions & 26 deletions userscript.user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Krunker Editor+
// @version 4.13.1
// @version 4.13.2
// @author Jakob#8686
// @description Custom features for the Krunker Map Editor
// @match *://*.krunker.io/editor.html*
Expand All @@ -13,47 +13,56 @@
// @updateURL https://github.com/j4k0xb/Krunker-Editor-Plus/raw/master/userscript.meta.js
// ==/UserScript==

async function patch(elem) {
observer.disconnect();
const src = elem.textContent;
elem.textContent = '';
const bundle = loadBundle();
let script;

/* eslint-disable */
eval(await loadBundle());
dispatchEvent(new CustomEvent('editor-plus-init', { detail: src }));
}
document.addEventListener('DOMContentLoaded', async () => {
eval(await bundle); // eslint-disable-line
dispatchEvent(
new CustomEvent('editor-plus-init', { detail: script.textContent })
);
});

const observer = new MutationObserver(mutations => {
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (
node.textContent &&
node.textContent.includes('Krunker.io') &&
node.nodeType === Node.TEXT_NODE
) {
observer.disconnect();
script = node.parentElement;
script.type = 'text/blocked';
}
}
}
});

observer.observe(document, { childList: true, subtree: true });

async function loadBundle() {
const commitURL = 'https://api.github.com/repos/j4k0xb/Krunker-Editor-Plus/commits?per_page=1';
const commitURL =
'https://api.github.com/repos/j4k0xb/Krunker-Editor-Plus/commits?per_page=1';
const lastModified = GM_getValue('lastModified') || 0;

const commit = (await (await fetch(commitURL, { cache: 'no-store' })).json())[0].commit;
const commit = (
await (await fetch(commitURL, { cache: 'no-store' })).json()
)[0].commit;
const commitDate = Date.parse(commit.committer.date);

if (commitDate != lastModified) return updateBundle(commitDate);
if (commitDate !== lastModified) return updateBundle(commitDate);

return GM_getValue('bundle');
}

async function updateBundle(date) {
const bundleURL = 'https://raw.githubusercontent.com/j4k0xb/Krunker-Editor-Plus/master/bundle.js';
const bundleURL =
'https://raw.githubusercontent.com/j4k0xb/Krunker-Editor-Plus/master/bundle.js';
const bundle = await (await fetch(bundleURL, { cache: 'no-store' })).text();

GM_setValue('bundle', bundle);
GM_setValue('lastModified', date);

return bundle;
}

const observer = new MutationObserver(mutations => {
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (node.textContent && node.textContent.includes('Krunker.io')) return patch(node);
}
}
});

observer.observe(document, { childList: true, subtree: true });
document.addEventListener('beforescriptexecute', ({ target }) => {
if (target.textContent && target.textContent.includes('Krunker.io')) patch(target)
});

0 comments on commit fdb9050

Please sign in to comment.