我可以在Electron应用程序中阅读webview的cookie吗?

时间:2016-11-17 10:05:04

标签: cookies electron

使用WebView元素在Electron应用程序中显示其他页面时,是否可以读取和写入其Cookie?

1 个答案:

答案 0 :(得分:5)

是的,你可以:

通过会话访问cookie:

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')
  }
)

电子中访问cookie的其他方式:

  • 因为cookies是http协议的一部分,你可以通过电子重写cookie标题webrequest api
  • 您可以通过内容脚本(preload脚本)
  • 访问document.cookies

更多信息: