Skip to content

Instantly share code, notes, and snippets.

@derpycoder
Last active September 30, 2022 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derpycoder/5395c504f0eface691077d18e356e5cd to your computer and use it in GitHub Desktop.
Save derpycoder/5395c504f0eface691077d18e356e5cd to your computer and use it in GitHub Desktop.
Opens a Pop Up & Closes it when it matches a route URL!
const POP_UP_URL = "/"
const TARGET_URL = window.location.origin
const TARGET_URL_REGEX = new RegExp(TARGET_URL)
const POP_UP_WINDOW = open(POP_UP_URL, "Pop Up Window", `scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=512,height=512`);
const onPopUpClose = () => {
console.log("Pop Up Closed")
}
// Watch if Pop Up closed, repeatedly, as onclose callback doesn't work for pop ups!
let popUpWatcher = setInterval(() => {
if (POP_UP_WINDOW.closed) {
clearInterval(popUpWatcher);
onPopUpClose();
}
}, 500);
let redirectWatcher = null;
POP_UP_WINDOW.onunload = () => {
clearInterval(redirectWatcher)
// Watch Redirects happening in the Pop Up
redirectWatcher = setInterval(() => {
if(POP_UP_WINDOW.closed) {
clearInterval(redirectWatcher)
return;
}
// Close the Pop Up when its URL matches the Target URL!
if (TARGET_URL_REGEX.test(POP_UP_WINDOW.document.location.href)) {
clearInterval(redirectWatcher);
POP_UP_WINDOW.close();
}
}, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment