forked from JGSMoreira/YT-Music-Jam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
52 lines (46 loc) · 1.64 KB
/
background.js
File metadata and controls
52 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let musicaAtual = null;
let fila = [];
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const activeTab = tabs[0];
// Verifica se a URL da guia ativa pertence ao YouTube Music
if (activeTab && activeTab.url.includes("music.youtube.com")) {
// Injeta um script na página para acessar informações da música
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
function: async (tab) => {
console.log("tab", tab);
await chrome.runtime.sendMessage({ musicInfo });
},
});
}
});
function verificarStatusMusica() {
chrome.tabs.query({ currentWindow: true }, async function (tabs) {
const ytMusicTab = tabs.find((tab) =>
tab.url.includes("music.youtube.com")
);
if (!ytMusicTab) return;
await chrome.tabs.sendMessage(ytMusicTab.id, {
action: "obterStatusMusica",
});
});
}
function verificarFila() {
chrome.tabs.query(
{ active: true, currentWindow: true },
async function (tabs) {
const activeTab = tabs[0];
if (activeTab && activeTab.url.includes("music.youtube.com")) {
await chrome.tabs.sendMessage(activeTab.id, { action: "getFila" });
}
}
);
}
const intervalObterFila = setInterval(verificarFila, 1000);
const intervalObterDadosMusica = setInterval(verificarStatusMusica, 1000);
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message?.action === "getMusica") sendResponse({ musicaAtual });
if (message?.action === "getFila") sendResponse({ fila });
if (message?.musicaAtual) musicaAtual = message.musicaAtual;
if (message?.fila) fila = message.fila;
});