|
@@ -1,12 +1,12 @@
|
|
|
import { expect } from 'chai'
|
|
|
-import { session, BrowserWindow, ipcMain, WebContents } from 'electron'
|
|
|
+import { app, session, BrowserWindow, ipcMain, WebContents } from 'electron'
|
|
|
import { closeAllWindows, closeWindow } from './window-helpers'
|
|
|
import * as http from 'http'
|
|
|
import { AddressInfo } from 'net'
|
|
|
import * as path from 'path'
|
|
|
import * as fs from 'fs'
|
|
|
import { ifdescribe } from './spec-helpers'
|
|
|
-import { emittedOnce } from './events-helpers'
|
|
|
+import { emittedOnce, emittedNTimes } from './events-helpers'
|
|
|
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
|
@@ -243,6 +243,150 @@ ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome ex
|
|
|
expect(bg).to.equal('')
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ describe('chrome extension content scripts', () => {
|
|
|
+ const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
+ const extensionPath = path.resolve(fixtures, 'extensions')
|
|
|
+
|
|
|
+ const addExtension = (name: string) => session.defaultSession.loadExtension(path.resolve(extensionPath, name))
|
|
|
+ const removeAllExtensions = () => {
|
|
|
+ Object.keys(session.defaultSession.getAllExtensions()).map(extName => {
|
|
|
+ session.defaultSession.removeExtension(extName)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ let responseIdCounter = 0
|
|
|
+ const executeJavaScriptInFrame = (webContents: WebContents, frameRoutingId: number, code: string) => {
|
|
|
+ return new Promise(resolve => {
|
|
|
+ const responseId = responseIdCounter++
|
|
|
+ ipcMain.once(`executeJavaScriptInFrame_${responseId}`, (event, result) => {
|
|
|
+ resolve(result)
|
|
|
+ })
|
|
|
+ webContents.send('executeJavaScriptInFrame', frameRoutingId, code, responseId)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ const generateTests = (sandboxEnabled: boolean, contextIsolationEnabled: boolean) => {
|
|
|
+ describe(`with sandbox ${sandboxEnabled ? 'enabled' : 'disabled'} and context isolation ${contextIsolationEnabled ? 'enabled' : 'disabled'}`, () => {
|
|
|
+ let w: BrowserWindow
|
|
|
+
|
|
|
+ describe('supports "run_at" option', () => {
|
|
|
+ beforeEach(async () => {
|
|
|
+ await closeWindow(w)
|
|
|
+ w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ width: 400,
|
|
|
+ height: 400,
|
|
|
+ webPreferences: {
|
|
|
+ contextIsolation: contextIsolationEnabled,
|
|
|
+ sandbox: sandboxEnabled
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ afterEach(() => {
|
|
|
+ removeAllExtensions()
|
|
|
+ return closeWindow(w).then(() => { w = null as unknown as BrowserWindow })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should run content script at document_start', async () => {
|
|
|
+ await addExtension('content-script-document-start')
|
|
|
+ w.webContents.once('dom-ready', async () => {
|
|
|
+ const result = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
|
|
|
+ expect(result).to.equal('red')
|
|
|
+ })
|
|
|
+ w.loadURL(url)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should run content script at document_idle', async () => {
|
|
|
+ await addExtension('content-script-document-idle')
|
|
|
+ w.loadURL(url)
|
|
|
+ const result = await w.webContents.executeJavaScript('document.body.style.backgroundColor')
|
|
|
+ expect(result).to.equal('red')
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should run content script at document_end', async () => {
|
|
|
+ await addExtension('content-script-document-end')
|
|
|
+ w.webContents.once('did-finish-load', async () => {
|
|
|
+ const result = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
|
|
|
+ expect(result).to.equal('red')
|
|
|
+ })
|
|
|
+ w.loadURL(url)
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ // TODO(nornagon): real extensions don't load on file: urls, so this
|
|
|
+ // test needs to be updated to serve its content over http.
|
|
|
+ describe.skip('supports "all_frames" option', () => {
|
|
|
+ const contentScript = path.resolve(fixtures, 'extensions/content-script')
|
|
|
+
|
|
|
+ // Computed style values
|
|
|
+ const COLOR_RED = `rgb(255, 0, 0)`
|
|
|
+ const COLOR_BLUE = `rgb(0, 0, 255)`
|
|
|
+ const COLOR_TRANSPARENT = `rgba(0, 0, 0, 0)`
|
|
|
+
|
|
|
+ before(() => {
|
|
|
+ BrowserWindow.addExtension(contentScript)
|
|
|
+ })
|
|
|
+
|
|
|
+ after(() => {
|
|
|
+ BrowserWindow.removeExtension('content-script-test')
|
|
|
+ })
|
|
|
+
|
|
|
+ beforeEach(() => {
|
|
|
+ w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ webPreferences: {
|
|
|
+ // enable content script injection in subframes
|
|
|
+ nodeIntegrationInSubFrames: true,
|
|
|
+ preload: path.join(contentScript, 'all_frames-preload.js')
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ afterEach(() =>
|
|
|
+ closeWindow(w).then(() => {
|
|
|
+ w = null as unknown as BrowserWindow
|
|
|
+ })
|
|
|
+ )
|
|
|
+
|
|
|
+ it('applies matching rules in subframes', async () => {
|
|
|
+ const detailsPromise = emittedNTimes(w.webContents, 'did-frame-finish-load', 2)
|
|
|
+ w.loadFile(path.join(contentScript, 'frame-with-frame.html'))
|
|
|
+ const frameEvents = await detailsPromise
|
|
|
+ await Promise.all(
|
|
|
+ frameEvents.map(async frameEvent => {
|
|
|
+ const [, isMainFrame, , frameRoutingId] = frameEvent
|
|
|
+ const result: any = await executeJavaScriptInFrame(
|
|
|
+ w.webContents,
|
|
|
+ frameRoutingId,
|
|
|
+ `(() => {
|
|
|
+ const a = document.getElementById('all_frames_enabled')
|
|
|
+ const b = document.getElementById('all_frames_disabled')
|
|
|
+ return {
|
|
|
+ enabledColor: getComputedStyle(a).backgroundColor,
|
|
|
+ disabledColor: getComputedStyle(b).backgroundColor
|
|
|
+ }
|
|
|
+ })()`
|
|
|
+ )
|
|
|
+ expect(result.enabledColor).to.equal(COLOR_RED)
|
|
|
+ if (isMainFrame) {
|
|
|
+ expect(result.disabledColor).to.equal(COLOR_BLUE)
|
|
|
+ } else {
|
|
|
+ expect(result.disabledColor).to.equal(COLOR_TRANSPARENT) // null color
|
|
|
+ }
|
|
|
+ })
|
|
|
+ )
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ generateTests(false, false)
|
|
|
+ generateTests(false, true)
|
|
|
+ generateTests(true, false)
|
|
|
+ generateTests(true, true)
|
|
|
+ })
|
|
|
})
|
|
|
|
|
|
ifdescribe(!process.electronBinding('features').isExtensionsEnabled())('chrome extensions', () => {
|
|
@@ -325,4 +469,199 @@ ifdescribe(!process.electronBinding('features').isExtensionsEnabled())('chrome e
|
|
|
|
|
|
expect(response).to.equal(3)
|
|
|
})
|
|
|
+
|
|
|
+ describe('extensions and dev tools extensions', () => {
|
|
|
+ let showPanelTimeoutId: NodeJS.Timeout | null = null
|
|
|
+
|
|
|
+ const showLastDevToolsPanel = (w: BrowserWindow) => {
|
|
|
+ w.webContents.once('devtools-opened', () => {
|
|
|
+ const show = () => {
|
|
|
+ if (w == null || w.isDestroyed()) return
|
|
|
+ const { devToolsWebContents } = w as unknown as { devToolsWebContents: WebContents | undefined }
|
|
|
+ if (devToolsWebContents == null || devToolsWebContents.isDestroyed()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const showLastPanel = () => {
|
|
|
+ // this is executed in the devtools context, where UI is a global
|
|
|
+ const { UI } = (window as any)
|
|
|
+ const lastPanelId = UI.inspectorView._tabbedPane._tabs.peekLast().id
|
|
|
+ UI.inspectorView.showPanel(lastPanelId)
|
|
|
+ }
|
|
|
+ devToolsWebContents.executeJavaScript(`(${showLastPanel})()`, false).then(() => {
|
|
|
+ showPanelTimeoutId = setTimeout(show, 100)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ showPanelTimeoutId = setTimeout(show, 100)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ afterEach(() => {
|
|
|
+ if (showPanelTimeoutId != null) {
|
|
|
+ clearTimeout(showPanelTimeoutId)
|
|
|
+ showPanelTimeoutId = null
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ describe('BrowserWindow.addDevToolsExtension', () => {
|
|
|
+ describe('for invalid extensions', () => {
|
|
|
+ it('throws errors for missing manifest.json files', () => {
|
|
|
+ const nonexistentExtensionPath = path.join(__dirname, 'does-not-exist')
|
|
|
+ expect(() => {
|
|
|
+ BrowserWindow.addDevToolsExtension(nonexistentExtensionPath)
|
|
|
+ }).to.throw(/ENOENT: no such file or directory/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('throws errors for invalid manifest.json files', () => {
|
|
|
+ const badManifestExtensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest')
|
|
|
+ expect(() => {
|
|
|
+ BrowserWindow.addDevToolsExtension(badManifestExtensionPath)
|
|
|
+ }).to.throw(/Unexpected token }/)
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe('for a valid extension', () => {
|
|
|
+ const extensionName = 'foo'
|
|
|
+
|
|
|
+ before(() => {
|
|
|
+ const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
|
|
+ BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
+ expect(BrowserWindow.getDevToolsExtensions()).to.have.property(extensionName)
|
|
|
+ })
|
|
|
+
|
|
|
+ after(() => {
|
|
|
+ BrowserWindow.removeDevToolsExtension('foo')
|
|
|
+ expect(BrowserWindow.getDevToolsExtensions()).to.not.have.property(extensionName)
|
|
|
+ })
|
|
|
+
|
|
|
+ describe('when the devtools is docked', () => {
|
|
|
+ let message: any
|
|
|
+ let w: BrowserWindow
|
|
|
+ before(async () => {
|
|
|
+ w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
|
|
|
+ const p = new Promise(resolve => ipcMain.once('answer', (event, message) => {
|
|
|
+ resolve(message)
|
|
|
+ }))
|
|
|
+ showLastDevToolsPanel(w)
|
|
|
+ w.loadURL('about:blank')
|
|
|
+ w.webContents.openDevTools({ mode: 'bottom' })
|
|
|
+ message = await p
|
|
|
+ })
|
|
|
+ after(closeAllWindows)
|
|
|
+
|
|
|
+ describe('created extension info', function () {
|
|
|
+ it('has proper "runtimeId"', async function () {
|
|
|
+ expect(message).to.have.ownProperty('runtimeId')
|
|
|
+ expect(message.runtimeId).to.equal(extensionName)
|
|
|
+ })
|
|
|
+ it('has "tabId" matching webContents id', function () {
|
|
|
+ expect(message).to.have.ownProperty('tabId')
|
|
|
+ expect(message.tabId).to.equal(w.webContents.id)
|
|
|
+ })
|
|
|
+ it('has "i18nString" with proper contents', function () {
|
|
|
+ expect(message).to.have.ownProperty('i18nString')
|
|
|
+ expect(message.i18nString).to.equal('foo - bar (baz)')
|
|
|
+ })
|
|
|
+ it('has "storageItems" with proper contents', function () {
|
|
|
+ expect(message).to.have.ownProperty('storageItems')
|
|
|
+ expect(message.storageItems).to.deep.equal({
|
|
|
+ local: {
|
|
|
+ set: { hello: 'world', world: 'hello' },
|
|
|
+ remove: { world: 'hello' },
|
|
|
+ clear: {}
|
|
|
+ },
|
|
|
+ sync: {
|
|
|
+ set: { foo: 'bar', bar: 'foo' },
|
|
|
+ remove: { foo: 'bar' },
|
|
|
+ clear: {}
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe('when the devtools is undocked', () => {
|
|
|
+ let message: any
|
|
|
+ let w: BrowserWindow
|
|
|
+ before(async () => {
|
|
|
+ w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
|
|
|
+ showLastDevToolsPanel(w)
|
|
|
+ w.loadURL('about:blank')
|
|
|
+ w.webContents.openDevTools({ mode: 'undocked' })
|
|
|
+ message = await new Promise(resolve => ipcMain.once('answer', (event, message) => {
|
|
|
+ resolve(message)
|
|
|
+ }))
|
|
|
+ })
|
|
|
+ after(closeAllWindows)
|
|
|
+
|
|
|
+ describe('created extension info', function () {
|
|
|
+ it('has proper "runtimeId"', function () {
|
|
|
+ expect(message).to.have.ownProperty('runtimeId')
|
|
|
+ expect(message.runtimeId).to.equal(extensionName)
|
|
|
+ })
|
|
|
+ it('has "tabId" matching webContents id', function () {
|
|
|
+ expect(message).to.have.ownProperty('tabId')
|
|
|
+ expect(message.tabId).to.equal(w.webContents.id)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('works when used with partitions', async () => {
|
|
|
+ const w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ webPreferences: {
|
|
|
+ nodeIntegration: true,
|
|
|
+ partition: 'temp'
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
|
|
+ BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
+ try {
|
|
|
+ showLastDevToolsPanel(w)
|
|
|
+
|
|
|
+ const p: Promise<any> = new Promise(resolve => ipcMain.once('answer', function (event, message) {
|
|
|
+ resolve(message)
|
|
|
+ }))
|
|
|
+
|
|
|
+ w.loadURL('about:blank')
|
|
|
+ w.webContents.openDevTools({ mode: 'bottom' })
|
|
|
+ const message = await p
|
|
|
+ expect(message.runtimeId).to.equal('foo')
|
|
|
+ } finally {
|
|
|
+ BrowserWindow.removeDevToolsExtension('foo')
|
|
|
+ await closeAllWindows()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ it('serializes the registered extensions on quit', () => {
|
|
|
+ const extensionName = 'foo'
|
|
|
+ const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', extensionName)
|
|
|
+ const serializedPath = path.join(app.getPath('userData'), 'DevTools Extensions')
|
|
|
+
|
|
|
+ BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
+ app.emit('will-quit')
|
|
|
+ expect(JSON.parse(fs.readFileSync(serializedPath, 'utf8'))).to.deep.equal([extensionPath])
|
|
|
+
|
|
|
+ BrowserWindow.removeDevToolsExtension(extensionName)
|
|
|
+ app.emit('will-quit')
|
|
|
+ expect(fs.existsSync(serializedPath)).to.be.false('file exists')
|
|
|
+ })
|
|
|
+
|
|
|
+ describe('BrowserWindow.addExtension', () => {
|
|
|
+ it('throws errors for missing manifest.json files', () => {
|
|
|
+ expect(() => {
|
|
|
+ BrowserWindow.addExtension(path.join(__dirname, 'does-not-exist'))
|
|
|
+ }).to.throw('ENOENT: no such file or directory')
|
|
|
+ })
|
|
|
+
|
|
|
+ it('throws errors for invalid manifest.json files', () => {
|
|
|
+ expect(() => {
|
|
|
+ BrowserWindow.addExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
|
|
|
+ }).to.throw('Unexpected token }')
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
})
|