Skip to content

Commit 81361e1

Browse files
committed
making preload plugin scripts work again
1 parent d1bcee7 commit 81361e1

File tree

15 files changed

+134
-81
lines changed

15 files changed

+134
-81
lines changed

packages/selenium-ide/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"@seleniumhq/code-export-ruby-rspec": "^4.0.0-alpha.1",
107107
"side-code-export": "^4.0.0-alpha.11",
108108
"@seleniumhq/get-driver": "^4.0.0-alpha.1",
109-
"@seleniumhq/side-api": "^4.0.0-alpha.33",
109+
"@seleniumhq/side-api": "^4.0.0-alpha.34",
110110
"@seleniumhq/side-model": "^4.0.0-alpha.4",
111111
"@seleniumhq/side-runtime": "^4.0.0-alpha.31",
112112
"dnd-core": "^16.0.1",

packages/selenium-ide/src/browser/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const api: Api = processApi((path: string) => {
1111
return Handler()(path)
1212
})
1313

14+
export {BrowserApiMutators} from './mutator'
1415
export { Api } from '@seleniumhq/side-api'
1516

1617
export default api
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { processApi } from '@seleniumhq/side-api'
22
import { ApiHoist as Api } from '@seleniumhq/side-api'
33

4+
type FunctionKeys<T extends Record<string, {mutator?: Function}>> = {
5+
[K in keyof T as T[K]['mutator'] extends Function ? K : never]: T[K]['mutator'];
6+
};
7+
48
/**
59
* This Converts the chrome API type to something usable
610
* from the front end
711
*/
8-
export type BrowserApiMutatorMapper = {
9-
[Namespace in keyof Api]: {
10-
[Handler in keyof Api[Namespace]]: Api[Namespace][Handler]['mutator']
11-
}
12+
export type BrowserApiMutators = {
13+
[Namespace in keyof Api]: FunctionKeys<Api[Namespace]>
1214
}
1315

14-
export default processApi<BrowserApiMutatorMapper>(
16+
export default processApi<BrowserApiMutators>(
1517
(_, handler) => handler.mutator
1618
)
Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
1-
import { contextBridge } from 'electron'
2-
import api from 'browser/api'
3-
import apiMutators from 'browser/api/mutator'
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
import { PluginPreloadOutputShape } from '@seleniumhq/side-api'
18+
import api, { Api, BrowserApiMutators } from 'browser/api'
19+
import mutators from 'browser/api/mutator'
20+
import { contextBridge, ipcRenderer } from 'electron'
421

5-
/**
6-
* Binds our API on initialization
7-
*/
8-
process.once('loaded', async () => {
9-
/**
10-
* Expose it in the main context
11-
*/
12-
contextBridge.exposeInMainWorld('sideAPI', { ...api, mutators: apiMutators })
13-
})
22+
export default (
23+
apiSubset: Partial<Api> & { mutators: Partial<BrowserApiMutators> } = {
24+
...api,
25+
mutators,
26+
}
27+
) =>
28+
new Promise<(PluginPreloadOutputShape | null)[]>((resolve) => {
29+
const loadPluginFromPath = ([pluginPath, preloadPath]: [
30+
string,
31+
string
32+
]): PluginPreloadOutputShape | null => {
33+
try {
34+
const pluginPreload = __non_webpack_require__(preloadPath)
35+
const pluginHandler =
36+
typeof pluginPreload === 'function'
37+
? pluginPreload
38+
: pluginPreload.default
39+
return pluginHandler((...args: any[]) =>
40+
ipcRenderer.send(`message-${pluginPath}`, ...args)
41+
)
42+
} catch (e) {
43+
console.error(e)
44+
return null
45+
}
46+
}
47+
48+
window.sideAPI = apiSubset as Api & { mutators: BrowserApiMutators }
49+
50+
/**
51+
* Binds our API on initialization
52+
*/
53+
process.once('loaded', async () => {
54+
/**
55+
* Expose it in the main context
56+
*/
57+
contextBridge.exposeInMainWorld('sideAPI', window.sideAPI)
58+
const pluginPaths = await api.plugins.list()
59+
const preloadPaths = await api.plugins.listPreloadPaths()
60+
const richPlugins: [string, string][] = pluginPaths.map((p, i) => [
61+
p,
62+
preloadPaths[i],
63+
])
64+
const plugins = richPlugins.map(loadPluginFromPath)
65+
resolve(plugins)
66+
})
67+
})
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import '../../helpers/preload'
1+
import preload from '../../helpers/preload'
2+
3+
preload()
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import '../../helpers/preload'
1+
import preload from '../../helpers/preload'
2+
3+
preload()

packages/selenium-ide/src/browser/windows/PlaybackWindow/preload.ts

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,34 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17+
import { PluginPreloadOutputShape } from '@seleniumhq/side-api'
1718
import api from 'browser/api'
1819
import apiMutators from 'browser/api/mutator'
19-
import { contextBridge, ipcRenderer, webFrame } from 'electron'
20-
import { identity } from 'lodash/fp'
20+
import preload from 'browser/helpers/preload'
21+
import { webFrame } from 'electron'
2122
import Recorder from './preload/recorder'
2223

23-
const pluginFromPath = ([pluginPath, preloadPath]: [string, string]) => {
24-
try {
25-
const pluginPreload = __non_webpack_require__(preloadPath)
26-
const pluginHandler =
27-
typeof pluginPreload === 'function'
28-
? pluginPreload
29-
: pluginPreload.default
30-
return pluginHandler((...args: any[]) =>
31-
ipcRenderer.send(`message-${pluginPath}`, ...args)
32-
)
33-
} catch (e) {
34-
console.error(e)
35-
return null
36-
}
37-
}
3824

39-
/**
40-
* Expose it in the main context
41-
*/
42-
window.addEventListener('DOMContentLoaded', async () => {
43-
webFrame.executeJavaScript(`
44-
Object.defineProperty(navigator, 'webdriver', {
45-
get () {
46-
return true
47-
}
48-
})
49-
`)
50-
window.sideAPI = {
25+
(async () => {
26+
const plugins = await preload({
5127
recorder: api.recorder,
52-
// @ts-expect-error
5328
mutators: { recorder: apiMutators.recorder },
54-
}
55-
const pluginPaths = await api.plugins.list()
56-
const preloadPaths = await api.plugins.listPreloadPaths()
57-
const richPlugins: [string, string][] = pluginPaths.map((p, i) => [p, preloadPaths[i]])
58-
const plugins = richPlugins.map(pluginFromPath).filter(identity)
29+
})
30+
window.addEventListener('DOMContentLoaded', async () => {
31+
webFrame.executeJavaScript(`
32+
Object.defineProperty(navigator, 'webdriver', {
33+
get () {
34+
return true
35+
}
36+
})
37+
`)
38+
setTimeout(async () => {
39+
console.debug('Initializing the recorder')
40+
new Recorder(window, plugins.filter(Boolean) as PluginPreloadOutputShape[])
41+
}, 500)
42+
})
43+
})();
5944

60-
contextBridge.exposeInMainWorld('sideAPI', window.sideAPI)
61-
setTimeout(async () => {
62-
console.debug('Initializing the recorder')
63-
new Recorder(window, plugins)
64-
}, 500)
65-
})
45+
/**
46+
* Expose it in the main context
47+
*/
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import '../../helpers/preload'
1+
import preload from '../../helpers/preload'
2+
3+
preload()
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import '../../helpers/preload'
1+
import preload from '../../helpers/preload'
2+
3+
preload()

packages/selenium-ide/src/main/session/controllers/Windows/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const windowLoaderFactoryMap: WindowLoaderFactoryMap = Object.fromEntries(
6161
devTools: !isAutomated,
6262
...(windowConfig?.webPreferences ?? {}),
6363
preload: hasPreload ? preloadPath : undefined,
64+
sandbox: hasPreload ? false : undefined,
6465
},
6566
...options,
6667
})
@@ -198,11 +199,19 @@ export default class WindowsController extends BaseController {
198199
webPreferences: {
199200
// This should be the default preload, which just adds the sideAPI to the window
200201
preload: join(__dirname, `project-editor-preload-bundle.js`),
202+
sandbox: false,
201203
...(opts?.webPreferences ?? {}),
202204
},
205+
show: false,
203206
})
204207
this.windows[name] = window
205208
await window.loadURL(`file://${filepath}`)
209+
await this.useWindowState(
210+
window,
211+
'windowSize' + name,
212+
'windowPosition' + name
213+
)
214+
window.show()
206215
return true
207216
}
208217

@@ -290,7 +299,11 @@ export default class WindowsController extends BaseController {
290299
if (isMac) {
291300
window.setWindowButtonVisibility(false)
292301
}
293-
await this.useWindowState(window, 'windowSizePlayback', 'windowPositionPlayback')
302+
await this.useWindowState(
303+
window,
304+
'windowSizePlayback',
305+
'windowPositionPlayback'
306+
)
294307
window.show()
295308
}
296309

@@ -305,14 +318,14 @@ export default class WindowsController extends BaseController {
305318
) {
306319
const size = storage.get(sizeKey) as number[]
307320
const position = storage.get(positionKey) as number[]
308-
if (size.length) window.setSize(size[0], size[1], true)
321+
if (size?.length) window.setSize(size[0], size[1], true)
309322

310323
const screenElectron = electron.screen.getPrimaryDisplay()
311324

312325
const sWidth = screenElectron.bounds.width
313326
const sHeight = screenElectron.bounds.height
314327

315-
if (position.length) {
328+
if (position?.length) {
316329
const adjustedX = position[0] < 0 ? 50 : position[0]
317330
const adjustedY = position[1] < 0 ? 50 : position[1]
318331
window.setPosition(adjustedX, adjustedY)
@@ -347,7 +360,7 @@ export default class WindowsController extends BaseController {
347360
await this.initializePlaybackWindow()
348361
await this.close('splash')
349362

350-
await this.open(projectEditorWindowName, {show: false})
363+
await this.open(projectEditorWindowName, { show: false })
351364
const projectWindow = await this.get(projectEditorWindowName)
352365
this.useWindowState(projectWindow, 'windowSize', 'windowPosition')
353366

0 commit comments

Comments
 (0)