Path: blob/main/src/vs/platform/browserView/electron-browser/preload-browserView.ts
5241 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45/* eslint-disable no-restricted-globals */67/**8* Preload script for pages loaded in Integrated Browser9*10* It runs in an isolated context that Electron calls an "isolated world".11* Specifically the isolated world with worldId 999, which shows in DevTools as "Electron Isolated Context".12* Despite being isolated, it still runs on the same page as the JS from the actual loaded website13* which runs on the so-called "main world" (worldId 0. In DevTools as "top").14*15* Learn more: see Electron docs for Security, contextBridge, and Context Isolation.16*/17(function () {1819const { contextBridge } = require('electron');2021// #######################################################################22// ### ###23// ### !!! DO NOT USE GET/SET PROPERTIES ANYWHERE HERE !!! ###24// ### !!! UNLESS THE ACCESS IS WITHOUT SIDE EFFECTS !!! ###25// ### (https://github.com/electron/electron/issues/25516) ###26// ### ###27// #######################################################################28const globals = {29/**30* Get the currently selected text in the page.31*/32getSelectedText(): string {33try {34// Even if the page has overridden window.getSelection, our call here will still reach the original35// implementation. That's because Electron proxies functions, such as getSelectedText here, that are36// exposed to a different context via exposeInIsolatedWorld or exposeInMainWorld.37return window.getSelection()?.toString() ?? '';38} catch {39return '';40}41}42};4344try {45// Use `contextBridge` APIs to expose globals to the same isolated world where this preload script runs (worldId 999).46// The globals object will be recursively frozen (and for functions also proxied) by Electron to prevent47// modification within the given context.48contextBridge.exposeInIsolatedWorld(999, 'browserViewAPI', globals);49} catch (error) {50console.error(error);51}52}());535455