-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
77 lines (66 loc) · 2.38 KB
/
content.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Get the current song title and artist
function getCurrentSong() {
const title = document.querySelector(
'[data-testid="context-item-info-title"]'
)?.textContent;
const artist = document.querySelector(
'[data-testid="context-item-info-artist"]'
)?.textContent;
return { title, artist };
}
// Store the current song
let currentSong;
// Create a MutationObserver to watch for changes in the song information
const observer = new MutationObserver(() => {
// Get the new song
const newSong = getCurrentSong();
// Compare with the current song
if (newSong.title !== currentSong.title || newSong.artist !== currentSong.artist) {
// Send a message to the background script with the new song information
chrome.runtime.sendMessage({
type: "songChange",
title: newSong.title,
artist: newSong.artist,
});
// Update the current song
currentSong = newSong;
}
});
const destroyContentJS = () => {
function destructor() {
// Destruction is needed only once
document.removeEventListener(destructionEvent, destructor);
// Tear down content script: Unbind events, clear timers, restore DOM, etc.
// console.log("destroyed content.js and disconnected observer");
observer.disconnect();
}
const destructionEvent = "destructmyextension_" + chrome.runtime.id;
// Unload previous content script if needed
document.dispatchEvent(new CustomEvent(destructionEvent));
document.addEventListener(destructionEvent, destructor);
};
// Keep checking if title and artist have loaded on the page every 500ms before content script starts, so they will be defined
const intervalID = setInterval(() => {
currentSong = getCurrentSong();
if (currentSong.title && currentSong.artist) {
destroyContentJS();
// Start content.js
main();
}
}, 500);
function main() {
clearInterval(intervalID);
// Configure the MutationObserver to watch for changes in the target element
const nowPlaying = document.querySelector('[data-testid="now-playing-widget"]');
// Start observing the target elements and run the mutation callback on change
observer.observe(nowPlaying, {
childList: true,
subtree: true,
});
// Add an event listener to handle disconnection of observer before tab closes
window.addEventListener("beforeunload", () => {
// console.log("disconnected observer");
destroyContentJS();
observer.disconnect();
});
}