|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | 6 | import { WebContentsView, webContents } from 'electron'; |
| 7 | +import { FileAccess } from '../../../base/common/network.js'; |
7 | 8 | import { Disposable } from '../../../base/common/lifecycle.js'; |
8 | 9 | import { Emitter, Event } from '../../../base/common/event.js'; |
9 | 10 | import { VSBuffer } from '../../../base/common/buffer.js'; |
10 | | -import { IBrowserViewBounds, IBrowserViewDevToolsStateEvent, IBrowserViewFocusEvent, IBrowserViewKeyDownEvent, IBrowserViewState, IBrowserViewNavigationEvent, IBrowserViewLoadingEvent, IBrowserViewLoadError, IBrowserViewTitleChangeEvent, IBrowserViewFaviconChangeEvent, IBrowserViewNewPageRequest, BrowserViewStorageScope, IBrowserViewCaptureScreenshotOptions, IBrowserViewFindInPageOptions, IBrowserViewFindInPageResult, IBrowserViewVisibilityEvent, BrowserNewPageLocation } from '../common/browserView.js'; |
| 11 | +import { IBrowserViewBounds, IBrowserViewDevToolsStateEvent, IBrowserViewFocusEvent, IBrowserViewKeyDownEvent, IBrowserViewState, IBrowserViewNavigationEvent, IBrowserViewLoadingEvent, IBrowserViewLoadError, IBrowserViewTitleChangeEvent, IBrowserViewFaviconChangeEvent, IBrowserViewNewPageRequest, BrowserViewStorageScope, IBrowserViewCaptureScreenshotOptions, IBrowserViewFindInPageOptions, IBrowserViewFindInPageResult, IBrowserViewVisibilityEvent, BrowserNewPageLocation, browserViewIsolatedWorldId } from '../common/browserView.js'; |
11 | 12 | import { EVENT_KEY_CODE_MAP, KeyCode, KeyMod, SCAN_CODE_STR_TO_EVENT_KEY_CODE } from '../../../base/common/keyCodes.js'; |
12 | 13 | import { IWindowsMainService } from '../../windows/electron-main/windows.js'; |
13 | 14 | import { IBaseWindow, ICodeWindow } from '../../window/electron-main/window.js'; |
@@ -96,6 +97,7 @@ export class BrowserView extends Disposable { |
96 | 97 | sandbox: true, |
97 | 98 | webviewTag: false, |
98 | 99 | session: viewSession, |
| 100 | + preload: FileAccess.asFileUri('vs/platform/browserView/electron-browser/preload-browserView.js').fsPath, |
99 | 101 |
|
100 | 102 | // TODO@kycutler: Remove this once https://github.com/electron/electron/issues/42578 is fixed |
101 | 103 | type: 'browserView' |
@@ -535,6 +537,23 @@ export class BrowserView extends Disposable { |
535 | 537 | this._view.webContents.stopFindInPage(keepSelection ? 'keepSelection' : 'clearSelection'); |
536 | 538 | } |
537 | 539 |
|
| 540 | + /** |
| 541 | + * Get the currently selected text in the browser view. |
| 542 | + * Returns immediately with empty string if the page is still loading. |
| 543 | + */ |
| 544 | + async getSelectedText(): Promise<string> { |
| 545 | + // we don't want to wait for the page to finish loading, which executeJavaScript normally does. |
| 546 | + if (this._view.webContents.isLoading()) { |
| 547 | + return ''; |
| 548 | + } |
| 549 | + try { |
| 550 | + // Uses our preloaded contextBridge-exposed API. |
| 551 | + return await this._view.webContents.executeJavaScriptInIsolatedWorld(browserViewIsolatedWorldId, [{ code: 'window.browserViewAPI?.getSelectedText?.() ?? ""' }]); |
| 552 | + } catch { |
| 553 | + return ''; |
| 554 | + } |
| 555 | + } |
| 556 | + |
538 | 557 | /** |
539 | 558 | * Clear all storage data for this browser view's session |
540 | 559 | */ |
|
0 commit comments