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

pup*_*eno 3 cookies electron

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

HaN*_*riX 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')
  }
)
Run Code Online (Sandbox Code Playgroud)

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

  • 因为cookie是http协议的一部分,你可以通过电子webrequest api 重写cookie头
  • 你可以document.cookies通过contentscripts 访问(预加载脚本)

更多信息: