Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

RUN-5538 - Expose Find In Page #987

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js-adapter
Submodule js-adapter updated 1 files
+2 −1 Gruntfile.js
21 changes: 21 additions & 0 deletions src/browser/api/webcontents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
28 changes: 27 additions & 1 deletion src/browser/api_protocol/api_handlers/webcontents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Is there a reason we wouldn't want to use the identity here instead of the payload identity? Probably doesn't make a functional difference tho

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other functions are using the payload one, didn't see a need to change that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identity (First param) is the requesting identity, the identity in the payout is the target.

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;
Expand Down Expand Up @@ -137,4 +163,4 @@ export function getElectronWebContents({uuid, name}: Identity, errDesc?: string)
}

return webContents;
}
}