Created
October 8, 2024 23:13
-
-
Save Viiprogrammer/d6a3278eeabbca545c5917b41d31c135 to your computer and use it in GitHub Desktop.
Swagger auto hot reload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function waitForElement (selector, timeout) { | |
return new Promise(resolve => { | |
if (document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)) | |
} | |
// eslint-disable-next-line no-undef | |
const observer = new MutationObserver(mutations => { | |
if (document.querySelector(selector)) { | |
observer.disconnect() | |
resolve(document.querySelector(selector)) | |
} | |
}) | |
// If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336 | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}) | |
if (timeout) { | |
setTimeout(() => { | |
observer.disconnect() | |
resolve(null) | |
}, timeout) | |
} | |
}) | |
} | |
let tmp = '' | |
async function hotReload () { | |
const { value: url } = document.querySelector('#download-url-input') | |
const response = await fetch(url).then(x => x.text()) | |
// eslint-disable-next-line no-undef | |
const { hash } = location | |
if (tmp === '') tmp = response | |
if (tmp !== response) { | |
document.querySelector('.download-url-button')?.click() | |
if (hash) { | |
waitForElement(`[href="${hash}"]`, 1500) | |
.then(x => x?.scrollIntoView()) | |
} | |
tmp = response | |
} | |
} | |
setInterval(hotReload, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment