getstation / electron-chrome-extension

Implementation of Chrome extension APIs for Electron
139 stars 26 forks source link

feat: support electron V5 #47

Closed magne4000 closed 5 years ago

magne4000 commented 5 years ago

Diff description

hugomano commented 5 years ago

Origin chrome-extension is now a secure origin in the renderer

Screenshot 2019-04-29 at 16 37 44 Screenshot 2019-04-29 at 16 41 28
hugomano commented 5 years ago

Blocked by https://github.com/electron/electron/issues/17989 (workaround for https://github.com/electron/electron/issues/18032).

Tried to hook with an event listener like this:

const hookWindowOpen = (webContents) => {
  webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
    event.preventDefault()

    Object.assign(options, {
      show: false,
      width: 600,
      height: 800,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        preload: path.join(__dirname, '../renderer/init.js')
      }
    })

    console.log(options);

    const win = new BrowserWindow(options);

    win.once('ready-to-show', () => {
      win.show()
      console.log(url)
      win.webContents.loadURL('https://github.com')
    })

    event.newGuest = win;
  })
}

app.on('web-contents-created', function (event, webContents) {
  hookWindowOpen(webContents)
})

the window.opener stay null after navigation and preloads doesn't load. Really related to the process model IMO.

hugomano commented 5 years ago

Test with BrowserWindow

Try with the main browser window instead of the webview

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      webviewTag: true,
      contextIsolation: false,
      nativeWindowOpen: true,
    }
  });

  mainWindow.loadURL('https://mail.google.com');
}
...

It's the same behavior than our webview:

Don't attach the newly created window for newGuest on new-open

mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
    ...
})

The options include the embedder through options.webContents which is the reference for window.opener.

In all case, if we create an orphan browser window, the opener is null and preloads are loaded.

Navigation

setup: the opener is webview with domain mail.google.com