Last active
February 14, 2022 09:56
-
-
Save discatte/f47ccd76ea2a2bca25b1e0003222a5b2 to your computer and use it in GitHub Desktop.
wordle local storage nytimes redirect magic https://twitter.com/GalacticFurball/status/1492014062976188420
This file contains 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
const ls = window.localStorage; | |
// Ensure we're always sending something | |
let stringifiedData = '{}'; | |
// Default to production | |
let url = 'https://www.nytimes.com/games/wordle' | |
// Don't attempt unless our user has local storage enabled | |
if (ls) { | |
const errors = []; | |
const localData = { | |
time: new Date().getTime(), | |
statistics: null, | |
darkTheme: null, | |
colorBlindTheme: null, | |
}; | |
// Allow QA folks to switch the redirect url | |
const params = new Proxy(new URLSearchParams(window.location.search), { | |
get: (searchParams, prop) => searchParams.get(prop), | |
}); | |
if (params.url) url = params.url | |
// Attempt to pull and parse the stats and themes | |
try { | |
localData.statistics = JSON.parse(ls.getItem('statistics')); | |
localData.darkTheme = JSON.parse(ls.getItem('darkTheme')); | |
localData.colorBlindTheme = JSON.parse(ls.getItem('colorBlindTheme')); | |
} catch (e) { | |
// Anything that's not valid JSON will not be sent | |
errors.push(e); | |
} | |
try { | |
stringifiedData = JSON.stringify(localData); | |
} catch (e) { | |
errors.push(e); | |
} | |
} | |
// Everyone will redirect, regardless of local storage | |
window.location.assign(`${url}?data=${stringifiedData}`) |
This file contains 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
const ls = window.localStorage; | |
// Ensure we're always sending something | |
let stringifiedData = '{}'; | |
// Default to production | |
let url = 'https://www.nytimes.com/games/wordle' | |
// Don't attempt unless our user has local storage enabled | |
if (ls) { | |
const errors = []; | |
const localData = { | |
time: new Date().getTime(), | |
statistics: null, | |
darkTheme: null, | |
colorBlindTheme: null, | |
force: true | |
}; | |
// Attempt to pull and parse the stats and themes | |
try { | |
localData.statistics = JSON.parse(ls.getItem('statistics')); | |
localData.darkTheme = JSON.parse(ls.getItem('darkTheme')); | |
localData.colorBlindTheme = JSON.parse(ls.getItem('colorBlindTheme')); | |
} catch (e) { | |
// Anything that's not valid JSON will not be sent | |
errors.push(e); | |
} | |
try { | |
stringifiedData = JSON.stringify(localData); | |
} catch (e) { | |
errors.push(e); | |
} | |
} | |
// Everyone will redirect, regardless of local storage | |
window.location.assign(`${url}?data=${stringifiedData}`) |
there are two versions, as shortly after posting about this someone noticed the QA section was removed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I remember hearing the creator talk about this on his appearance on the syntax.fm podcast. It's funny — when he brought it up, everybody immediately said it would have problems, and it turns out that it did! The better way to do this is probably to
postMessage
to a cross-domain iframe (with proper CORS).