popup.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. document.addEventListener("alpine:init", () => {
  2. Alpine.data("playlistManager", () => ({
  3. playlists: {},
  4. init() {
  5. this.loadPlaylists();
  6. },
  7. async loadPlaylists() {
  8. try {
  9. const result = await browser.storage.local.get("playlists");
  10. console.log("LOAD RESULT", result.playlists);
  11. this.playlists = result.playlists || {};
  12. } catch (error) {
  13. console.error("Error loading playlists:", error);
  14. }
  15. },
  16. formatPlaylistName(name) {
  17. // Convert "listening-1" to "Listening - 1"
  18. return name
  19. .split("-")
  20. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  21. .join(" - ");
  22. },
  23. openVideo(url) {
  24. browser.tabs.create({ url });
  25. },
  26. async removeVideo(playlistName, index) {
  27. // Make a copy of the current playlist
  28. const playlist = [...this.playlists[playlistName]];
  29. // Remove the video at the specified index
  30. playlist.splice(index, 1);
  31. // Create an updated playlists object with the remaining playlists unchanged
  32. const updatedPlaylists = {
  33. ...this.playlists,
  34. [playlistName]: playlist,
  35. };
  36. // Update the playlists in storage
  37. try {
  38. await browser.storage.local.set({ playlists: updatedPlaylists });
  39. this.playlists = updatedPlaylists;
  40. } catch (error) {
  41. console.error("Error removing video:", error);
  42. }
  43. },
  44. }));
  45. });
  46. console.log("popup.js FINISHED doing something");