zoukankan      html  css  js  c++  java
  • electron代码示例 在 html 中调用 electron 的函数 主进程的函数 nodejs

    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,
        });
      });
  • 相关阅读:
    每天一个linux命令(1):man命令
    安卓名词积累
    每天一个Linux命令:目录
    ubuntu下Python的安装和使用
    嵌入式基础知识(1):存储
    2月份学习笔记
    AndroidStudio 中的坑Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID
    AndroidStudio开发出现Warning:Gradle version 2.10 is required. Current version is 2.8. If u
    Git 问题
    判断年 月的值
  • 原文地址:https://www.cnblogs.com/bigben0123/p/13862627.html
Copyright © 2011-2022 走看看