Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read webview's cookies in an Electron app?

When using a WebView element for displaying other pages inside an Electron app, is it possible to read and write its cookies?

like image 317
pupeno Avatar asked Nov 17 '16 10:11

pupeno


1 Answers

Yes you can:

Access cookies via session:

const { session } = require('electron').remote

// Here we access the session via the partition name
// You could also get it from the webContents object
// (webContents.session)
const cookies = session.fromPartition(<yourWebviewPartionName>).cookies

// Get a specific Cookie
cookies.get(
  {
    url: <targetURL>,
    name: <cookieName>
  }, 
  (error, result) => console.log('Found the following cookies', result)
)

// Get all cookies
cookies.get(
  {}, 
  (error, result) => console.log('Found the following cookies', result)
)

// Remove a cookie
cookies.remove(
  <targetURL>,
  <cookieName>,
  error => {
    if (error) throw error
    console.log('cookie deleted')
  }
)

Other ways of accessig cookies in electron:

  • since cookies are part of the http protocol you could rewrite the cookie headers via electrons webrequest api
  • you could access document.cookies via contentscripts (preload scripts)

More Info:

  • webview
  • webContents
  • session
like image 160
HaNdTriX Avatar answered Nov 05 '22 01:11

HaNdTriX