Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我应该在我的电子应用程序中使用上下文隔离吗_Javascript_Electron - Fatal编程技术网

Javascript 我应该在我的电子应用程序中使用上下文隔离吗

Javascript 我应该在我的电子应用程序中使用上下文隔离吗,javascript,electron,Javascript,Electron,我和我的朋友差不多完了。它基本上是Spotify的一款正在播放/迷你播放器应用程序。当我检查渲染过程控制台时,我又收到了一个警告,我想清除它。这是关于worldSafeExecuteJavaScript的真实性以及它的不安全性。我进一步查看了它,结果发现我也需要打开contextIsolation。我做了更多的研究,发现我不能在渲染过程中使用require。我试图查找文档,但我感到困惑。我们想让我们的应用程序长期保持安全,尤其是因为我在上大学,而我的朋友在他的学校发展壮大时,可能会忙得不可开交;

我和我的朋友差不多完了。它基本上是Spotify的一款正在播放/迷你播放器应用程序。当我检查渲染过程控制台时,我又收到了一个警告,我想清除它。这是关于
worldSafeExecuteJavaScript
的真实性以及它的不安全性。我进一步查看了它,结果发现我也需要打开
contextIsolation
。我做了更多的研究,发现我不能在渲染过程中使用
require
。我试图查找文档,但我感到困惑。我们想让我们的应用程序长期保持安全,尤其是因为我在上大学,而我的朋友在他的学校发展壮大时,可能会忙得不可开交;另外,由于在Electron 12中,
contextIsolation
默认为true


在我们的javascript渲染过程中,我们只需要jQuery和IPC渲染器。我们如何合并
contextBridge
(我想这就是它的名字),将jQuery和ipcRenderer导入到我们的javascript中进行渲染过程?

我使用
contextBridge
让我的应用程序与
contextIsolation
一起工作。下面是我的带有预加载脚本的
main.js
webPreferences

    webPreferences: {
      worldSafeExecuteJavaScript: true,
      contextIsolation: true,
      preload: path.join(__dirname, "preload.js")
    },
这里有
preload.js
来导入
ipcrederer

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld(
    'ipcRenderer',
    {
/*
Important note: This will get it working, 
but I'm going to make multiple methods for 
each time I'm using this to heighten security.
https://www.electronjs.org/docs/tutorial/context-isolation#security-considerations
*/
      send: (channel, arg) => ipcRenderer.send(channel, arg),
      on: (event, data) => ipcRenderer.on(event, data)
    }
)
,我刚刚用
index.html
导入了它:

  <head>
    <meta charset="UTF-8">
    <title>Gemini</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self'">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="macos.css">
    <script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="node_modules/@fortawesome/fontawesome-free/css/all.css">
  </head>

双子座

不过,我不确定通过HTML导入jQuery是否更安全。

我使用
contextBridge
让我的应用程序使用
contextIsolation
。下面是我的带有预加载脚本的
main.js
webPreferences

    webPreferences: {
      worldSafeExecuteJavaScript: true,
      contextIsolation: true,
      preload: path.join(__dirname, "preload.js")
    },
这里有
preload.js
来导入
ipcrederer

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld(
    'ipcRenderer',
    {
/*
Important note: This will get it working, 
but I'm going to make multiple methods for 
each time I'm using this to heighten security.
https://www.electronjs.org/docs/tutorial/context-isolation#security-considerations
*/
      send: (channel, arg) => ipcRenderer.send(channel, arg),
      on: (event, data) => ipcRenderer.on(event, data)
    }
)
,我刚刚用
index.html
导入了它:

  <head>
    <meta charset="UTF-8">
    <title>Gemini</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self'">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="macos.css">
    <script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="node_modules/@fortawesome/fontawesome-free/css/all.css">
  </head>

双子座
不过,我不确定通过HTML导入jQuery是否更安全