gpt4 book ai didi

javascript - 将setTimeout()函数从JSDOM更改为node.js默认值?

转载 作者:行者123 更新时间:2023-12-03 12:38:13 25 4
gpt4 key购买 nike

我在ytdl.getInfo()函数中使用带有ytdl-core和electronic包的node.js时出现错误。该错误仅在 Electron 渲染器文件中调用.getInfo()函数时发生,在主文件中它可以正常工作,因此请确保它与脚本链接到的html文件有关。
错误消息是这样并且多次发生:

Uncaught TypeError: setTimeout(...).unref is not a function
at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
at Object.exports.<computed> [as getBasicInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
at exports.getInfo (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:352)
at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461
at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
at getHTMLWatchPageBody (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:113)
at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:127
at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
at getIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:126)
at setIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:266)
at getWatchJSONPage (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:277)
at async retryFunc (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:207)
at async pipeline (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:150)
我做了一些研究,似乎有很多东西。这与我的程序使用的是JSm setTimeout()函数而不是node.js版本有关。在GitHub上争论的人们对类似的问题不太了解。所以我问我如何使用node.js setTimeout()函数。我创建了一个2. Node 项目,并重现了该问题。
这是一些我不太了解正在发生什么的GitHub链接:
https://github.com/electron/electron/issues/21162
https://github.com/sindresorhus/get-port/pull/40
https://github.com/facebook/jest/issues/9033
https://github.com/grpc/grpc-node/issues/1077
https://github.com/facebook/jest/issues/1909
https://github.com/jsdom/jsdom/blob/a6acac4e9dec4f859fff22676fb4e9eaa9139787/lib/jsdom/browser/Window.js#L176
这是我在2.项目中的代码:
main.js,我只是从 Electron 方面复制了
const { app, BrowserWindow } = require('electron');

function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

win.loadFile('index.html')
win.openDevTools();
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})

我的index.html文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
<p>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</p>
<script src="down.js"></script>
</body>
</html>
我的下载文件:
const ytdl = require("ytdl-core");

ytdl.getInfo("https://www.youtube.com/watch?v=ZA6LEWQf6us").then(
(info) => {
console.log(info);
}
);
我的package.json文件:
{
"name": "nodetestenv",
"version": "1.0.0",
"description": "test node things",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^11.1.0"
},
"dependencies": {
"ytdl-core": "^4.1.5"
}
}
谢谢。

最佳答案

我找到了有用的资料。启动 Electron 时,有一个面板“问题”,似乎是新的,我在其中找到了this网站的链接,解释说我的问题来自“内容安全策略”。看到此错误之后,我的node.js应用程序还告诉我,ytdl-core有一个新更新。我想昨天不是在那儿,更新为我解决了这个问题。
这是我的项目中的照片,其中包含错误和消息。这是我的实际项目,因为我在测试环境中找到了解决方案。
Picture of Error

关于javascript - 将setTimeout()函数从JSDOM更改为node.js默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65305651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com