diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index a3753a6d8f05..af64d9017c4a 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -419,9 +419,11 @@ void WebContents::AddNewContents(content::WebContents* source, v8::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); auto api_web_contents = CreateFrom(isolate(), new_contents); - Emit("-add-new-contents", api_web_contents, disposition, user_gesture, + if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture, initial_rect.x(), initial_rect.y(), initial_rect.width(), - initial_rect.height()); + initial_rect.height())) { + api_web_contents->DestroyWebContents(); + } } content::WebContents* WebContents::OpenURLFromTab( diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index f19ff06b1110..ab5d6d2d0d0b 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1012,6 +1012,23 @@ describe('BrowserWindow module', function () { }) }) }) + + it('supports calling preventDefault on new-window events', (done) => { + w.destroy() + w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true + } + }) + const initialWebContents = webContents.getAllWebContents() + ipcRenderer.send('prevent-next-new-window', w.webContents.id) + w.webContents.once('new-window', () => { + assert.deepEqual(webContents.getAllWebContents(), initialWebContents) + done() + }) + w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html')) + }) }) }) diff --git a/spec/static/main.js b/spec/static/main.js index b9d7d9473c31..9612ed25cf05 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -245,3 +245,7 @@ ipcMain.on('create-window-with-options-cycle', (event) => { const window = new BrowserWindow({show: false, foo: foo}) event.returnValue = window.id }) + +ipcMain.on('prevent-next-new-window', (event, id) => { + webContents.fromId(id).once('new-window', event => event.preventDefault()) +})