Javascript Electron https,没有找到node-fetch模块



这是我的gate.html代码:

<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self <' https://example.com.tr/validkeys.txt">
<meta http-equiv='Content-Security-Policy' content="default-src 'self'; script-src 'self'">
<title>License Gate</title>
<link rel='stylesheet' href='./index.css'>
</head>
<body>
<form id='license-gate'>
<label for='key'>
Please enter your license key
</label>
<input id='key' type='text' placeholder='Your license key'>
<button type='submit'>
Submit
</button>
</form>
</body>
</html>

这是我的gate.js代码(我在打开我的gate.html时预加载了这个,参见我的main.js)

const { ipcRenderer } = require('electron')
const https = require('https')

window.addEventListener('DOMContentLoaded', async () => {
console.log('DOM content loaded')
const gate = document.getElementById('license-gate')
gate.addEventListener('submit', async event => {
console.log('Submit button clicked')
event.preventDefault()

const key = document.getElementById('key').value;
console.log('Entered key:', key)
https.get('https://example.com.tr/validkeys.txt', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const keys = data.trim().split('n')
console.log('Valid keys:', keys)
if (keys.includes(key)) {
console.log('Key is valid')
ipcRenderer.send('key-validation-result', true)
} else {
console.log('Key is invalid')
ipcRenderer.send('key-validation-result', false)
}
});
}).on('error', (err) => {
console.log('Error: ', err.message);
});
})
})

我的main。js

const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
const isDev = process.env.NODE_ENV === 'development'
const { ipcRenderer } = require('electron');

let mainWindow;
async function gateCreateWindowWithLicense(createWindow) { 
const gateWindow = new BrowserWindow({
resizable: false,
frame: false,
width: 1920,
height: 1080,
webPreferences: {
preload: path.join(__dirname, 'gate.js'), //here, I'm preloading gate.js
devTools: true,
},
})


gateWindow.loadFile('gate.html')

if (isDev) {
gateWindow.webContents.openDevTools({ mode: 'detach' })
}
//ipcmain
ipcMain.on('key-validation-result', async (event, isValid) => {
if (isValid) {
// Close the license gate window
gateWindow.close()

// Launch our main window
createWindow()
} else {
// Show an error message
const dialogOptions = {
type: 'error',
title: 'Invalid license key',
message: 'The license key you entered is not valid. Please try again.',
buttons: ['OK'],
defaultId: 0,
cancelId: 0,
}

await dialog.showMessageBox(dialogOptions, gateWindow)
}
})
} 
function createWindow() {
const { screen } = require('electron');
let factor = screen.getPrimaryDisplay().scaleFactor;
// get the size of the screen
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({
width: 800/factor,
height: 600/factor,
useContentSize: true,
webPreferences: {
nodeIntegration: true,
zoomFactor: 1.0 / factor,
blinkFeatures: 'OverlayScrollbars'
},
autoHideMenuBar: true,
fullscreenable: true,
});
// Update the title of the Electron app window when the website updates its title
mainWindow.webContents.on('page-title-updated', (event, title) => {
event.preventDefault();
mainWindow.setTitle('Axiswiwo');

});
mainWindow.loadURL("http://www.thewebsitethatiwanttoopen.com/");
mainWindow.on('closed', function () {
mainWindow = null;

});
mainWindow.on('enter-full-screen', () => {
mainWindow.setResizable(true); // set window to resizable in fullscreen mode
mainWindow.maximize(); // maximize the window to fill the screen
});
mainWindow.on('leave-full-screen', () => {
mainWindow.setResizable(true); // set window to resizable
mainWindow.unmaximize(); // restore the window to its previous size
mainWindow.setSize(800, 600); // resize the window to 800x600
mainWindow.setAspectRatio(800 / 600);
});
// when the window is small, maximize it to fill the screen
if (mainWindow.isMaximizable()) {
mainWindow.maximize();
}
} // end of createwindow
app.whenReady().then(() => {
gateCreateWindowWithLicense(createWindow);
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});

所以,当我打开程序,在开发工具控制台它说:开发工具控制台错误

因此,它也不能预加载gate.js。

我尝试更新npm,安装"https"它已经在node.js中可用。而且,它找不到"node-fetch"要么。出现相同的错误。我不能使用XmlHttpRequest,因为它违反了内容政策,我现在不能访问网站面板。窗口。Fetch也不能获取数据。我真的不知道如何获取这些数据。

好吧,是我的错。Electron不允许将大多数节点模块导入预加载脚本。详见:https://www.electronjs.org/docs/latest/tutorial/tutorial-preload

一个BrowserWindow的预加载脚本运行在一个既可以访问HTML DOM又可以访问Node.js和Electron api的有限子集的上下文中。

就是它说的。

最新更新