gpt4 book ai didi

javascript - 如何读取文件名并处理我的 html 标签?

转载 作者:行者123 更新时间:2023-12-02 23:52:58 27 4
gpt4 key购买 nike

我尝试用 Electron 和 jquery 制作音乐播放器。我想做的第一步是打开菜单,单击文件,然后单击要更改的音乐标题。我成功打开文件菜单,但无法继续下一步。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>chplayer</title>
<link rel="stylesheet" href=""/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>CH Music Player</h1>
<h1 id="musictitle">[Music Title]</h1>
<h3 id="artistname">[artist name]</h3>
<image src="http://placehold.it/150X90" id="albumimage"/>
<div>
<input type="range" id="musicProgress" min="0" max="100">
</div>

<table>
<tr>
<td>
<button id="previousButton"></button>
<button id="pauseButton"></button>
<button id="nextButton"></button>
</td>
<td>
<input type="range" id="volumeProgess" min="0" max="100">
</td>
</tr>
</table>
</body>
<script>

</script>
</html>

index.html

const { app, BrowserWindow, Menu, dialog } = require('electron')
const path = require('path')
const fs = require('fs')

let win

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

var menu = Menu.buildFromTemplate([
{
label: 'Folders',
accelerator: 'CommandOrControl+o',
click: function () {
openFolderDialog()
}
},
{
label: 'Info',
click: function() {
dialog.showMessageBox(win,{type: "none", title: "Information",
message: "github link"},(response,checkboxChecked) => {
console.log(response)
})
}
}
])
Menu.setApplicationMenu(menu)
win.loadFile('index.html')
win.webContents.openDevTools()
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)

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

app.on('activate', () => {
if (win === null) {
createWindow()
}
})

function openFolderDialog() {
dialog.showOpenDialog(win, {
properties: ['openFile']
} , filepath => {

if (filepath) {
fs.writeFile('path.txt', filepath, function (err, data) {
if (err) console.log(err);
});
scanFile(filepath)
}
})
}

function scanFile(filepath) {
if(!filepath || filepath[0] == 'undefined') return;
console.log(filepath[0])
fs.readFile(filepath[0],"utf8", (err,data) => {
if(err) console.log(err);
var arr = [];
if (data.substr(-4) === '.mp3' || data.substr(-4) === '.m4a'
|| data.substr(-5) === '.webm' || data.substr(-4) === '.wav'
|| data.substr(-4) === '.aac' || data.substr(-4) === '.ogg'
|| data.substr(-5) === '.opus') {
arr.push(data);
}
var objToSend = {};
objToSend.files = arr;
objToSend.path = filepath;

win.webContents.send('selected-files', objToSend)
})
}

main.js

我应该如何从文件打开菜单中读取文件名并更改 h1 musictitle 标签?

最佳答案

这里似乎发生了一些事情。您是否试图允许用户选择一个文件或一个文件目录?您的函数名为“openFolderDialog”,但您指定“openfile”。请参阅showOpenDialog各种选项的文档。在 OSX 上,您可以同时指定:[openfile, openDirectory],但在 Windows 上不起作用,openDirectory 优先。

如果您希望用户一次仅选择一个文件,则不需要“scanFile”函数 - 您可以使用过滤器(没有句点,大小写无关紧要):

filters: [
{name: 'Audio', extensions: ['mp3', 'ogg', 'm4a']},
]

查看这篇文章:Working with HTML5 Audio in Electron

要更改 H1 文本,因为您已经在使用 jquery,您可以这样做

  $('#musictitle').text(sometext);

关于javascript - 如何读取文件名并处理我的 html 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55561769/

27 4 0