Add ability to set global preload scripts

This commit is contained in:
Samuel Attard
2017-09-03 02:15:46 +10:00
committed by Cheng Zhao
parent d598aa1a67
commit 0ddd078aaf
10 changed files with 116 additions and 13 deletions

View File

@@ -1021,6 +1021,41 @@ describe('BrowserWindow module', () => {
})
})
describe('global preload scripts', function () {
it('can add and remove multiple global preload script', function () {
var preload = path.join(fixtures, 'module', 'set-global.js')
var preload2 = path.join(fixtures, 'module', 'set-global-2.js')
assert.deepEqual(BrowserWindow.getGlobalPreloads(), [])
BrowserWindow.addGlobalPreload(preload)
assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload])
BrowserWindow.addGlobalPreload(preload2)
assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload, preload2])
BrowserWindow.removeGlobalPreload(preload)
assert.deepEqual(BrowserWindow.getGlobalPreloads(), [preload2])
BrowserWindow.removeGlobalPreload(preload2)
assert.deepEqual(BrowserWindow.getGlobalPreloads(), [])
})
it('loads the script before other scripts in window including normal preloads', function (done) {
var preload = path.join(fixtures, 'module', 'set-global.js')
var preload2 = path.join(fixtures, 'module', 'set-global-2.js')
ipcMain.once('answer', function (event, test) {
BrowserWindow.removeGlobalPreload(preload2)
assert.equal(test, 'preload2')
done()
})
w.destroy()
BrowserWindow.addGlobalPreload(preload2)
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload
}
})
w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
})
})
describe('"node-integration" option', () => {
it('disables node integration when specified to false', (done) => {
const preload = path.join(fixtures, 'module', 'send-later.js')

1
spec/fixtures/module/set-global-2.js vendored Normal file
View File

@@ -0,0 +1 @@
if (!window.test) window.test = 'preload2'

View File

@@ -1 +1 @@
window.test = 'preload'
if (!window.test) window.test = 'preload'