popup.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. document.addEventListener("alpine:init", () => {
  2. //TODO
  3. // - reorder videos in playlist
  4. // - remove videos from playlist
  5. // - preserve playlist order (sort)
  6. // - "play/resume playlist" button (pick up where you left off)
  7. // - track timestamp to truly resume where you left off
  8. // - periodically remove watched videos
  9. Alpine.data("playlistManager", () => ({
  10. playlists: {},
  11. init() {
  12. this.loadPlaylists();
  13. },
  14. async loadPlaylists() {
  15. try {
  16. const result = await browser.storage.local.get("playlists");
  17. console.log("LOAD RESULT", result.playlists);
  18. this.playlists = result.playlists || {};
  19. } catch (error) {
  20. console.error("Error loading playlists:", error);
  21. }
  22. },
  23. formatPlaylistName(name) {
  24. // Convert "listening-1" to "Listening - 1"
  25. return name
  26. .split("-")
  27. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  28. .join(" - ");
  29. },
  30. openVideo(url) {
  31. browser.tabs.create({ url });
  32. },
  33. async removeVideo(playlistName, index) {
  34. // Make a copy of the current playlist
  35. const playlist = [...this.playlists[playlistName]];
  36. // Remove the video at the specified index
  37. playlist.splice(index, 1);
  38. // Create an updated playlists object with the remaining playlists unchanged
  39. const updatedPlaylists = {
  40. ...this.playlists,
  41. [playlistName]: playlist,
  42. };
  43. // Update the playlists in storage
  44. try {
  45. await browser.storage.local.set({ playlists: updatedPlaylists });
  46. this.playlists = updatedPlaylists;
  47. } catch (error) {
  48. console.error("Error removing video:", error);
  49. }
  50. },
  51. videotitle: {
  52. ["@click"]() {
  53. console.log('TITLE CLICK', this.$el);
  54. },
  55. },
  56. videoPlayLink: {
  57. ["@click.prevent"](evt) {
  58. if (evt.target && evt.target.href) {
  59. browser.tabs.update({ url: evt.target.href });
  60. }
  61. },
  62. },
  63. removeVideoButton: {
  64. ["@click"](evt) {
  65. if (evt.target && evt.target.attributes.playlistName && evt.target.attributes.playlistIndex) {
  66. this.removeVideo(evt.target.attributes.playlistName.value, evt.target.attributes.playlistIndex.value);
  67. }
  68. },
  69. },
  70. }));
  71. });