popup.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // - consider separating context menu items (rather than having a sub-menu)
  10. Alpine.data("playlistManager", () => ({
  11. playlists: {},
  12. init() {
  13. this.loadPlaylists();
  14. },
  15. async loadPlaylists() {
  16. try {
  17. const result = await browser.storage.local.get("playlists");
  18. console.log("LOAD RESULT", result.playlists);
  19. this.playlists = result.playlists || {};
  20. } catch (error) {
  21. console.error("Error loading playlists:", error);
  22. }
  23. },
  24. formatPlaylistName(name) {
  25. // Convert "listening-1" to "Listening - 1"
  26. return name
  27. .split("-")
  28. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  29. .join(" - ");
  30. },
  31. openVideo(url) {
  32. browser.tabs.create({ url });
  33. },
  34. async removeVideo(playlistName, index) {
  35. const playlists = JSON.parse(JSON.stringify(this.playlists));
  36. // Make a copy of the current playlist
  37. const playlist = [...playlists[playlistName]];
  38. // Remove the video at the specified index
  39. playlist.splice(index, 1);
  40. // Create an updated playlists object with the remaining playlists unchanged
  41. const updatedPlaylists = {
  42. ...playlists,
  43. [playlistName]: playlist,
  44. };
  45. // Update the playlists in storage
  46. try {
  47. await browser.storage.local.set({ playlists: updatedPlaylists });
  48. this.playlists = updatedPlaylists;
  49. } catch (error) {
  50. console.error("Error removing video:", error);
  51. }
  52. },
  53. videotitle: {
  54. ["@click"]() {
  55. console.log("TITLE CLICK", this.$el);
  56. },
  57. },
  58. videoPlayLink: {
  59. ["@click.prevent"]() {
  60. browser.tabs.update({ url: this.$el.href });
  61. },
  62. },
  63. removeVideoButton: {
  64. ["@click"]() {
  65. this.removeVideo(
  66. this.$el.dataset.playlistName,
  67. this.$el.dataset.playlistIndex,
  68. );
  69. },
  70. },
  71. }));
  72. });