2020-10-14 00:11:06 +03:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2024-10-03 05:10:44 +03:00
|
|
|
import { internalContextBridge } from '@electron/internal/renderer/api/context-bridge';
|
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
2019-02-14 16:52:38 +03:00
|
|
|
|
2020-04-22 22:42:51 +03:00
|
|
|
const { contextIsolationEnabled } = internalContextBridge;
|
|
|
|
|
2022-01-06 20:28:03 +03:00
|
|
|
export const windowSetup = (isWebView: boolean, isHiddenPage: boolean) => {
|
2021-07-29 01:32:53 +03:00
|
|
|
if (!process.sandboxed && !isWebView) {
|
2019-02-14 16:52:38 +03:00
|
|
|
// Override default window.close.
|
|
|
|
window.close = function () {
|
2020-10-14 00:11:06 +03:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_WINDOW_CLOSE);
|
2019-02-14 16:52:38 +03:00
|
|
|
};
|
2020-04-27 22:46:04 +03:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['close'], window.close);
|
2019-02-14 16:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// But we do not support prompt().
|
2020-04-27 22:46:04 +03:00
|
|
|
window.prompt = function () {
|
|
|
|
throw new Error('prompt() is and will not be supported.');
|
|
|
|
};
|
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['prompt'], window.prompt);
|
2019-02-14 16:52:38 +03:00
|
|
|
|
2021-07-29 01:32:53 +03:00
|
|
|
if (isWebView) {
|
2019-02-14 16:52:38 +03:00
|
|
|
// Webview `document.visibilityState` tracks window visibility (and ignores
|
|
|
|
// the actual <webview> element visibility) for backwards compatibility.
|
|
|
|
// See discussion in #9178.
|
|
|
|
//
|
|
|
|
// Note that this results in duplicate visibilitychange events (since
|
|
|
|
// Chromium also fires them) and potentially incorrect visibility change.
|
|
|
|
// We should reconsider this decision for Electron 2.0.
|
|
|
|
let cachedVisibilityState = isHiddenPage ? 'hidden' : 'visible';
|
|
|
|
|
|
|
|
// Subscribe to visibilityState changes.
|
2023-06-14 21:06:46 +03:00
|
|
|
ipcRendererInternal.on(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, function (_event, visibilityState: DocumentVisibilityState) {
|
2019-02-14 16:52:38 +03:00
|
|
|
if (cachedVisibilityState !== visibilityState) {
|
|
|
|
cachedVisibilityState = visibilityState;
|
|
|
|
document.dispatchEvent(new Event('visibilitychange'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make document.hidden and document.visibilityState return the correct value.
|
2020-04-22 22:42:51 +03:00
|
|
|
const getDocumentHidden = () => cachedVisibilityState !== 'visible';
|
2019-07-09 02:43:49 +03:00
|
|
|
Object.defineProperty(document, 'hidden', {
|
2020-04-22 22:42:51 +03:00
|
|
|
get: getDocumentHidden
|
2019-02-14 16:52:38 +03:00
|
|
|
});
|
2020-04-22 22:42:51 +03:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['document', 'hidden'], getDocumentHidden);
|
2019-02-14 16:52:38 +03:00
|
|
|
|
2020-04-22 22:42:51 +03:00
|
|
|
const getDocumentVisibilityState = () => cachedVisibilityState;
|
2019-07-09 02:43:49 +03:00
|
|
|
Object.defineProperty(document, 'visibilityState', {
|
2020-04-22 22:42:51 +03:00
|
|
|
get: getDocumentVisibilityState
|
2019-02-14 16:52:38 +03:00
|
|
|
});
|
2020-04-22 22:42:51 +03:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['document', 'visibilityState'], getDocumentVisibilityState);
|
2019-02-14 16:52:38 +03:00
|
|
|
}
|
|
|
|
};
|