1,
创建窗口时必须打开nodeIntegration:
win = new BrowserWindow({ 800, height: 600, sandbox: true, webPreferences: { nodeIntegration: true }, })
2,示例
html是在renderer进程。如果要调用主进程需要用remote
<input type="button" value="cache" onclick="getCache()"> <script> const { remote, ipcRenderer, shell } = require('electron'); const win = remote.getCurrentWindow(); var getCache=()=>{ var size = win.webContents.session.getCacheSize(); }
const mainProcess = remote.require('./main.js'); </script>
electron In action 示例很好:
https://github.com/electron-in-action
改变发送头:
function createWindow() { const filter = { urls: ["http://192.168.50.206:8080/*", "http://192.168.50.199:8087/*", 'https://*.github.com/*', '*://electron.github.io/'] } //session.defaultSession.webRequest.onBeforeSendHeaders /**/ session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => { details.requestHeaders['User-Agent'] = 'MyAgent' callback({ requestHeaders: details.requestHeaders }) }) 。。。。。。。
改变响应头
session.defaultSession.webRequest.onHeadersReceived((details, callback) => { // logger.debug(`onHeadersReceived: url:${details.url}, details:${JSON.stringify(details)}`); console.log("========= electron onHeadersReceived ====================") //console.log(stringtoHex(details)) // for (const key in details.responseHeaders) { // const lowerKey = key.toLowerCase(); // switch (lowerKey) { // case 'x-frame-options': // case 'content-security-policy': // // logger.debug(`Received header ${key}: ${details.responseHeaders[key]}`); // delete details.responseHeaders[key]; // break; // } // } console.log(`Response headers: ${JSON.stringify(details.responseHeaders)}`); callback({ cancel: false, responseHeaders: details.responseHeaders, statusLine: details.statusLine, }); });