Finish prototype for Spotify API, test implementation of notifications
This commit is contained in:
parent
eb3e73f690
commit
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -36,27 +30,47 @@ export default class SpotifyAPI {
|
||||||
* @example `/album/` -> `https://open.spotify.com/album/`
|
* @example `/album/` -> `https://open.spotify.com/album/`
|
||||||
*/
|
*/
|
||||||
private generateFullURL(relativeURL: string) {
|
private generateFullURL(relativeURL: string) {
|
||||||
return `${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