Redirects to /?theme=dark which is an official dark theme for instagram
< Feedback on Instagram Dark Theme
Thanks for your reply! I used URLSearchParams as you suggested.
Good approach but now it duplicates the params because they are already included in your url
variable. Just test it with the URL I provided earlier.
And you can just use url.searchParams
btw.
Example code
const url = new URL(window.location.href);
const { searchParams } = url;
if (!searchParams.get("theme")) {
searchParams.append("theme", "dark");
window.location.replace(url);
}
Now it works, but I don't understand why you're doing the replacement so complicated. See the code I provided, it works like this too.
I must've done something wrong earlier cause window.location.replace(url)
didn't work for me at first, but now it works just fine. It's definitely cleaner, thanks for your advices!
This does not work when the URL already has query parameters like with
https://www.instagram.com/?hl=en
You should probably make use of URLSearchParams