popup checks for parent existence and origin

fix #35
This commit is contained in:
HF 2022-09-30 13:08:12 +02:00
parent bdf653bda2
commit 6e199321bc
3 changed files with 40 additions and 23 deletions

View File

@ -586,3 +586,22 @@ export function getDateKeyOfTs(ts) {
const year = date.getUTCFullYear(); const year = date.getUTCFullYear();
return `${year}${month}${day}`; return `${year}${month}${day}`;
} }
/*
* check if parent window exists and
* is accessible
*/
export function parentExists() {
try {
if (!window.opener
|| window.opener.closed
|| !window.opener.location
|| window.opener.location.origin !== window.location.origin
) {
return false;
}
return true;
} catch {
return false;
}
}

View File

@ -4,6 +4,7 @@
import { persistStore } from 'redux-persist'; import { persistStore } from 'redux-persist';
import { parentExists } from './core/utils';
import store from './store/storePopUp'; import store from './store/storePopUp';
import { import {
urlChange, urlChange,
@ -66,7 +67,7 @@ persistStore(store, {}, () => {
store.dispatch(addChatChannel(channel)); store.dispatch(addChatChannel(channel));
}); });
if (!window.opener || window.opener.closed) { if (!parentExists()) {
store.dispatch(fetchMe()); store.dispatch(fetchMe());
SocketClient.connect(); SocketClient.connect();
} }

View File

@ -4,12 +4,13 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import { parentExists } from '../../core/utils';
import { load, unload } from '../actions'; import { load, unload } from '../actions';
const { origin } = window.location; const { origin } = window.location;
window.addEventListener('beforeunload', () => { window.addEventListener('beforeunload', () => {
if (window.opener && !window.opener.closed) { if (parentExists()) {
window.opener.postMessage(unload(), origin); window.opener.postMessage(unload(), origin);
} }
}); });
@ -24,43 +25,39 @@ export default (store) => (next) => (action) => {
} }
if (action.data.type === 't/UNLOAD') { if (action.data.type === 't/UNLOAD') {
setTimeout(() => { setTimeout(() => {
if (!window.opener || window.opener.closed) { if (parentExists()) {
console.log('Parent window closed');
store.dispatch({ type: 't/PARENT_CLOSED' });
} else {
console.log('Parent window refreshed'); console.log('Parent window refreshed');
/* const parentReady = window.opener.document.readyState;
* hook to event and also send message to catch more if (parentReady !== 'complete'
* possibilities && parentReady !== 'loaded'
*/ && parentReady !== 'interactive'
try { ) {
// DOMContent no loaded yet
const sendLoad = () => { const sendLoad = () => {
window.opener.postMessage({ type: 't/LOAD' }, origin); window.opener.postMessage({ type: 't/LOAD' }, origin);
window.opener.removeEventListener('DOMContentLoaded', sendLoad); window.opener.removeEventListener('DOMContentLoaded', sendLoad);
}; };
window.opener.addEventListener('DOMContentLoaded', sendLoad, false); window.opener.addEventListener('DOMContentLoaded', sendLoad, false);
} catch {
console.log('Could not hook to parent window');
} }
window.opener.postMessage({ type: 't/LOAD' }, origin); window.opener.postMessage({ type: 't/LOAD' }, origin);
} else {
console.log('Parent window closed');
store.dispatch({ type: 't/PARENT_CLOSED' });
} }
}, 3000); }, 3000);
} }
return next(action.data); return next(action.data);
} }
if (window.opener if (window.opener && action.type) {
&& !window.opener.closed try {
&& action.type if (action.type === 'HYDRATED') {
) { window.opener.postMessage(load(), origin);
if (action.type === 'HYDRATED') { } else if (action.type.startsWith('s/')) {
window.opener.postMessage(load(), origin);
} else if (action.type.startsWith('s/')) {
try {
window.opener.postMessage(action, origin); window.opener.postMessage(action, origin);
} catch {
// nothing
} }
} catch {
// nothing
} }
} }