electron开启nodeIntegration后导致传统页面的外部库加载失败,例如jquery

    46

    原因:BrowserWindow关闭了node隔离,再加载传统页面,导致外部库文件头部的环境判断出错。

    !function(a, b) {
        "object" == typeof module && "object" == typeof module.exports ? module.exports = a.document ? b(a, !0) : function(a) {
            if (!a.document)
                throw new Error("jQuery requires a window with a document");
            return b(a)
        }
        : b(a)
    }("undefined" != typeof window ? window : this, function(a, b) {
      //这里是执行代码
    }

    "object" typeof module && "object" typeof module.exports 会判断在node环境中,部分库会加载失败。

    解决方案:简单的将判断部分手动改为false,或者直接删除。

    推荐方案:electron区分内部页面与外部页面,加载外部http网页时,打开node隔离,毕竟随便一个页面就能执行本地代码了很危险。

      const container = GDContainerManager.shared.createContainer(url,url.startsWith('http')? {
          webPreferences:{
            webSecurity: true,
            preload:getPreloadPath(),
            nodeIntegration: false,
            contextIsolation: true
          }
        }:{
          webPreferences:{
            webSecurity: false,
            preload:undefined,
            nodeIntegration: true,
            contextIsolation: false
          }
        })