首页 > 解决方案 > 在不使用 nodeIntegration true 的情况下在电子中启用屏幕共享

问题描述

我正在电子中创建本机应用程序。为此,我创建了一个 BrowserWindow 并使用 nodeIntegration false 将 url 加载到其中。如果在浏览器中打开,在我的网站上一切正常。但是,如果我在电子中运行相同,则屏幕共享不起作用。我尝试使用一些在线参考来实现它,例如使用“预加载”来加载 js 文件。但没有任何效果

我为此使用最新的电子。

main.js

const {app, BrowserWindow} = require('electron');
const path = require('path');
let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      allowRunningInsecureContent: true,
      preload: path.join(__dirname, 'loadScripts.js')
    }
  });
  mainWindow.loadURL("https://abc.xyz.com/dkajd");
  mainWindow.webContents.openDevTools();
}
app.on('ready', createWindow);

app.on('activate', function () {
  if (mainWindow === null) createWindow()
});

加载脚本.js

const { desktopCapturer } = require('electron');
let mediaSources = null;

desktopCapturer.getSources({ types: ['window', 'screen'] },(err, sources) => {
  mediaSources = sources;
});

标签: electron

解决方案


推荐阅读