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