Разработан веб-сервер и расширение для браузеров на базе Chromium
This commit is contained in:
43
extension/main.js
Normal file
43
extension/main.js
Normal file
@ -0,0 +1,43 @@
|
||||
const mutationObserverConfig = {
|
||||
attributes: true,
|
||||
childList: false,
|
||||
characterData: false,
|
||||
};
|
||||
|
||||
const connectWebSocket = () => {
|
||||
const ws = new WebSocket('ws://localhost:8000/ws/v1/plugin');
|
||||
const progressElement = document.querySelector('#progress-bar');
|
||||
const mutationObserver = new MutationObserver(() => {
|
||||
const promise = new Promise(resolve => {
|
||||
const playerBarElement = document.querySelector('ytmusic-player-bar');
|
||||
const metadataElement = playerBarElement.querySelector('.middle-controls > .content-info-wrapper');
|
||||
const titleElement = metadataElement.querySelector(`yt-formatted-string.title`);
|
||||
const subtitleElement = metadataElement.querySelector('span.subtitle > yt-formatted-string.byline');
|
||||
const imageElement = playerBarElement.querySelector('img');
|
||||
if (titleElement.textContent === '' || subtitleElement.length === 0) return;
|
||||
const data = {
|
||||
type: 'music',
|
||||
attributes: {
|
||||
title: titleElement.textContent,
|
||||
artists: subtitleElement.title.split('•')[0].trim(),
|
||||
progress: progressElement.ariaValueNow / progressElement.ariaValueMax,
|
||||
image: imageElement.src,
|
||||
},
|
||||
};
|
||||
ws.send(JSON.stringify(data));
|
||||
resolve();
|
||||
});
|
||||
promise.then();
|
||||
});
|
||||
|
||||
ws.onopen = () => {
|
||||
mutationObserver.observe(progressElement, mutationObserverConfig);
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
mutationObserver.disconnect();
|
||||
connectWebSocket();
|
||||
};
|
||||
};
|
||||
|
||||
connectWebSocket();
|
23
extension/manifest.json
Normal file
23
extension/manifest.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "YT Music Live",
|
||||
"description": "YT Music Live",
|
||||
"version": "1.0",
|
||||
"manifest_version": 3,
|
||||
"action": {
|
||||
"default_icon": "icon.png"
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"js": [
|
||||
"main.js"
|
||||
],
|
||||
"matches": [
|
||||
"*://music.youtube.com/*"
|
||||
]
|
||||
}
|
||||
],
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"scripting"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user