Compare commits
2 Commits
eb290f7ba5
...
a086668bd3
Author | SHA1 | Date |
---|---|---|
Violet Millie | a086668bd3 | |
Violet Millie | cdd138a754 |
|
@ -1,4 +1,24 @@
|
||||||
function onRuntimeMessage(message: any) {}
|
import type { Track, Artist } from "./modules/SpotifyAPI";
|
||||||
|
|
||||||
|
function onRuntimeMessage(message: Track) {
|
||||||
|
const artist = message.artist;
|
||||||
|
|
||||||
|
console.log(message.coverArt);
|
||||||
|
|
||||||
|
browser.notifications
|
||||||
|
.create({
|
||||||
|
title: message.title,
|
||||||
|
message: message.artist.name,
|
||||||
|
iconUrl: message.coverArt,
|
||||||
|
type: "basic",
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
console.log("OK");
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(onRuntimeMessage);
|
browser.runtime.onMessage.addListener(onRuntimeMessage);
|
||||||
|
|
||||||
|
|
|
@ -4,4 +4,7 @@ const API = new SpotifyAPI();
|
||||||
|
|
||||||
// use the API
|
// use the API
|
||||||
|
|
||||||
API.getCurrentTrack();
|
const data = API.getCurrentTrack();
|
||||||
|
|
||||||
|
browser.runtime.sendMessage(data);
|
||||||
|
|
||||||
|
|
|
@ -6,20 +6,14 @@ export type Artist = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Track = {
|
export type Track = {
|
||||||
track: {
|
|
||||||
title: string;
|
title: string;
|
||||||
URL: string;
|
URL: string;
|
||||||
coverArt: string;
|
coverArt: string;
|
||||||
|
|
||||||
artist: Artist | Artist[];
|
artist: Artist;
|
||||||
};
|
|
||||||
|
|
||||||
playing: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Playstate = {
|
export type PlayState = {
|
||||||
track: Track;
|
|
||||||
|
|
||||||
isPlaying: boolean;
|
isPlaying: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -39,24 +33,44 @@ export default class SpotifyAPI {
|
||||||
return `${window.location.protocol}//${window.location.host}${relativeURL}`;
|
return `${window.location.protocol}//${window.location.host}${relativeURL}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentTrack() {
|
getCurrentTrack(): Track {
|
||||||
const titleElement = this.scraper.getBarElement("title")!;
|
const titleElement = this.scraper.getBarElement("title")!;
|
||||||
const trackURL = `${titleElement.getAttribute("href")}`;
|
const trackURL = `${titleElement.getAttribute("href")}`;
|
||||||
|
|
||||||
const artistElement = this.scraper.getBarElement("artist")!;
|
const artistElement = this.scraper.getBarElement("artist")!;
|
||||||
const artistName = artistElement.textContent;
|
const artistName = artistElement.textContent!;
|
||||||
const artistURL = artistElement.getAttribute("href");
|
const artistURL = artistElement.getAttribute("href")!;
|
||||||
|
|
||||||
// console.log(trackElement.textContent);
|
const coverArtImage = this.scraper
|
||||||
|
.getBarElement("coverArtImage")!
|
||||||
|
.getAttribute("src")!;
|
||||||
|
|
||||||
console.log(
|
return {
|
||||||
`Now playing: ${
|
title: titleElement.textContent!,
|
||||||
titleElement.textContent
|
URL: this.generateFullURL(trackURL),
|
||||||
} by ${artistName} (${this.generateFullURL(trackURL)})`
|
|
||||||
);
|
coverArt: coverArtImage,
|
||||||
|
|
||||||
|
artist: {
|
||||||
|
name: artistName,
|
||||||
|
URL: this.generateFullURL(artistURL),
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlaystate() {}
|
getPlaystate() {
|
||||||
|
const playbackControlButton = this.scraper.getBarElement(
|
||||||
|
"playbackControlButton"
|
||||||
|
)!;
|
||||||
|
|
||||||
|
/* The button's aria-label attribute contains the action that it will perform once clicked
|
||||||
|
not its current playstate */
|
||||||
|
return playbackControlButton.getAttribute("aria-label") === "Play"
|
||||||
|
? false
|
||||||
|
: true;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPlayback() {}
|
||||||
|
|
||||||
// TODO: Implement events: onPlay, onPause (?), trackChanged, playstateChanged
|
// TODO: Implement events: onPlay, onPause (?), trackChanged, playstateChanged
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue