Last active
August 17, 2024 10:39
-
-
Save carlhannes/610d7db3cd46dbf28cc8f667b94b2e7c to your computer and use it in GitHub Desktop.
sj-booking-fix.js
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
// Paste the code below into your webbrowser console and press "enter" | |
// To open the console you can press "F12" or "Ctrl + Shift + J" for most browsers. | |
// Read more here: https://appuals.com/open-browser-console/ | |
// Instructions video on my twitter: https://twitter.com/_carlhannes/status/1590441813445599232 | |
// The code re-tries fetching data if it gets status 429, which is the error that the SJ page has | |
// It does this together with an exponential back-off delay which is common to use with microservices of this type | |
// Because of these re-tries and the delay, the overall load of the website and the servers will be lower, | |
// since it does not need to re-fetch requests that actually succeed. Read more on my twitter if you're interested: | |
// https://twitter.com/_carlhannes/status/1590605735314206721 | |
// I do not have a soundcloud but I heard the artist Yoyo Xno on Spotify is really good: | |
// https://open.spotify.com/artist/0AZYrtDySQn1YqHbNzYWib?si=1335f829215942b2 | |
window._fetch = window.fetch; | |
window.fetch = (...args) => { | |
return new Promise((resolve, reject) => { | |
const maxRetries = 10 | |
let retries = 0 | |
const retry = () => { | |
window._fetch(...args) | |
.then((data) => { | |
if (data.status === 429 && retries < maxRetries) { | |
retries++ | |
setTimeout(retry, 500 * Math.pow(2, retries)) | |
} else { | |
resolve(data); | |
} | |
}).catch(reject) | |
} | |
retry() | |
}) | |
}; |
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
// Add this code as the "URL" of a bookmark in your browser to quickly add the booking fix to your browser. | |
// Good if you want to help parents, grandparents etc, just tell them to click the bookmark before booking. | |
// Kudos to @_genesis_ai_ https://twitter.com/_genesis_ai_/status/1590778385760157696 | |
javascript:q=window.fetch;window.fetch=(...a)=>new Promise((r,_,R=0,T=U=>q(...a).then(d=>d.status==429?setTimeout(T,500*(2**++R)):r(d)))=%3ET()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hehe, yes. -- or consider something really revolutionary outside all IT tech: prevent the rush on the servers by getting the train system in order and provide more trains :-D