Chrome Extension获取在YouTube音乐上播放的音乐的细节

发布于 2025-01-27 04:24:36 字数 97 浏览 4 评论 0原文

我想创建一个Chrome Extension,我需要获取YouTube音乐中正在播放的音乐的详细信息。有什么办法可以做到这一点(也许使用JS获取音乐的名称或其他方式)。感谢任何帮助

I want to create a Chrome Extension and I need to get the details of the music that is being played on YouTube Music. Is there any way to do that (maybe using JS to get the name of the music or whatever). Any kind of help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2025-02-03 04:24:36

您可以在YouTube音乐网站上运行内容脚本&应用并解析HTML以获取所需的信息。这是带有懒惰代码的快速脚本。显然,您应该更好地进行检测和错误处理。

清单:

{
    "manifest_version": 3,
    "name": "Youtube Music Info",
    "version": "1.0",
    "description": "Gets information from YT Music.",
    "content_scripts": [
        {
            "matches": [ "https://music.youtube.com/*" ],
            "js": [ "content.js" ],
            "all_frames": false,
            "run_at": "document_start"
        }
    ]
}

内容脚本:

let ytmusicPlayerBar

let waitForPlayerBarInterval = setInterval(() => {

    if (document.querySelector("ytmusic-player-bar")) {
        ytmusicPlayerBar = document.querySelector("ytmusic-player-bar")
        clearInterval(waitForPlayerBarInterval)
        getInfo()
    }

}, 1000)

function getInfo() {

    let getInfoInterval = setInterval(() => {

        if (ytmusicPlayerBar.querySelector(".title").innerText) {

            let info = {
                song: ytmusicPlayerBar.querySelector(".title").innerText,
                artist: ytmusicPlayerBar.querySelectorAll("a")[0].innerText,
                albumName: ytmusicPlayerBar.querySelectorAll("a")[1].innerText,
                albumCover: ytmusicPlayerBar.querySelector(".image").src,
                time: ytmusicPlayerBar.querySelector(".time-info").innerText
            }
        
            console.log(info)

        } else {
            console.log("Nothing is playing.")
        }

    }, 1000)

}

You can run content scripts on the Youtube Music website & app and parse the HTML to get the info you need. Here's a quick script with lazy code. You should obviously do the detection and error-handling better.

Manifest:

{
    "manifest_version": 3,
    "name": "Youtube Music Info",
    "version": "1.0",
    "description": "Gets information from YT Music.",
    "content_scripts": [
        {
            "matches": [ "https://music.youtube.com/*" ],
            "js": [ "content.js" ],
            "all_frames": false,
            "run_at": "document_start"
        }
    ]
}

Content Script:

let ytmusicPlayerBar

let waitForPlayerBarInterval = setInterval(() => {

    if (document.querySelector("ytmusic-player-bar")) {
        ytmusicPlayerBar = document.querySelector("ytmusic-player-bar")
        clearInterval(waitForPlayerBarInterval)
        getInfo()
    }

}, 1000)

function getInfo() {

    let getInfoInterval = setInterval(() => {

        if (ytmusicPlayerBar.querySelector(".title").innerText) {

            let info = {
                song: ytmusicPlayerBar.querySelector(".title").innerText,
                artist: ytmusicPlayerBar.querySelectorAll("a")[0].innerText,
                albumName: ytmusicPlayerBar.querySelectorAll("a")[1].innerText,
                albumCover: ytmusicPlayerBar.querySelector(".image").src,
                time: ytmusicPlayerBar.querySelector(".time-info").innerText
            }
        
            console.log(info)

        } else {
            console.log("Nothing is playing.")
        }

    }, 1000)

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文