diff --git a/js-adapter b/js-adapter index 354bbb098..01acf07b8 160000 --- a/js-adapter +++ b/js-adapter @@ -1 +1 @@ -Subproject commit 354bbb098fa48d9851e694a68a780b1103afda00 +Subproject commit 01acf07b8034493122b347d6d7ac4c15abe49313 diff --git a/src/browser/api/webcontents.ts b/src/browser/api/webcontents.ts index b6e290449..4be612c60 100644 --- a/src/browser/api/webcontents.ts +++ b/src/browser/api/webcontents.ts @@ -7,6 +7,12 @@ import { InjectableContext, EntityType } from '../../shapes'; import { prepareConsoleMessageForRVM } from '../rvm/utils'; export function hookWebContentsEvents(webContents: Electron.WebContents, { uuid, name }: Identity, topic: string, routeFunc: WindowRoute) { + webContents.on('found-in-page', (e, result) => { + const type = 'found-in-page'; + const payload = { uuid, name, topic, type, result }; + ofEvents.emit(routeFunc(type, uuid, name), payload); + }); + webContents.on('did-get-response-details', (e, status, newUrl, @@ -205,3 +211,18 @@ export function setIframeHandlers (webContents: Electron.WebContents, contextObj ofEvents.emit(route.window('frame-disconnected', uuid, name), payload); }; } + +export function findInPage(webContents: Electron.WebContents, searchTerm: string, options?: Electron.FindInPageOptions) { + return new Promise((resolve) => { + const getResults = (event: Electron.Event, result: any) => { + resolve(result); + }; + + webContents.once('found-in-page', getResults); + webContents.findInPage(searchTerm, options); + }); +} + +export function stopFindInPage(webContents: Electron.WebContents, action: 'clearSelection' | 'keepSelection' | 'activateSelection') { + webContents.stopFindInPage(action); +} diff --git a/src/browser/api_protocol/api_handlers/webcontents.ts b/src/browser/api_protocol/api_handlers/webcontents.ts index cadb25057..0a8a70884 100644 --- a/src/browser/api_protocol/api_handlers/webcontents.ts +++ b/src/browser/api_protocol/api_handlers/webcontents.ts @@ -10,11 +10,13 @@ const successAck: APIPayloadAck = { success: true }; export const webContentsApiMap = { 'execute-javascript-in-window': { apiFunc: executeJavascript, apiPath: '.executeJavaScript' }, + 'find-in-page': findInPage, 'get-zoom-level': getZoomLevel, 'navigate-window': navigateWindow, 'navigate-window-back': navigateWindowBack, 'navigate-window-forward': navigateWindowForward, 'stop-window-navigation': stopWindowNavigation, + 'stop-find-in-page': stopFindInPage, 'reload-window': reloadWindow, 'set-zoom-level': setZoomLevel, 'set-window-preload-state': setWindowPreloadState @@ -49,6 +51,30 @@ function executeJavascript(identity: Identity, message: APIMessage, ack: Acker, return nack(new Error('Rejected, target window is not owned by requesting identity')); } + +function findInPage(identity: Identity, message: APIMessage, ack: Acker): void { + const { payload } = message; + const { searchTerm, options } = payload; + const dataAck = Object.assign({}, successAck); + const windowIdentity = getTargetWindowIdentity(payload); + const webContents = getElectronWebContents(windowIdentity); + + WebContents.findInPage(webContents, searchTerm, options).then((data) => { + dataAck.data = data; + ack(dataAck); + }); +} + +function stopFindInPage(identity: Identity, message: APIMessage, ack: Acker): void { + const { payload } = message; + const { action } = payload; + const windowIdentity = getTargetWindowIdentity(payload); + const webContents = getElectronWebContents(windowIdentity); + + WebContents.stopFindInPage(webContents, action); + ack(successAck); +} + function navigateWindow(identity: Identity, message: APIMessage, ack: Acker, nack: (error: Error) => void): void { const { payload } = message; const { url } = payload; @@ -137,4 +163,4 @@ export function getElectronWebContents({uuid, name}: Identity, errDesc?: string) } return webContents; -} \ No newline at end of file +}