popup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. async exportPlaylists() {
  54. try {
  55. // Get current playlists
  56. const playlistResult = await browser.storage.local.get("playlists");
  57. const playlists = playlistResult.playlists || {};
  58. // Get playback history
  59. const historyResult = await browser.storage.local.get("history");
  60. const playbackHistory = historyResult.history || {};
  61. // Create export data object
  62. const exportData = {
  63. playlists,
  64. playbackHistory,
  65. exportDate: new Date().toISOString(),
  66. };
  67. // Convert to JSON
  68. const jsonString = JSON.stringify(exportData, null, 2);
  69. // Create download
  70. const blob = new Blob([jsonString], { type: "application/json" });
  71. const url = URL.createObjectURL(blob);
  72. // Trigger download
  73. const a = document.createElement("a");
  74. a.href = url;
  75. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  76. document.body.appendChild(a);
  77. a.click();
  78. // Clean up
  79. setTimeout(() => {
  80. document.body.removeChild(a);
  81. URL.revokeObjectURL(url);
  82. }, 100);
  83. } catch (error) {
  84. console.error("Error exporting playlists:", error);
  85. }
  86. },
  87. videotitle: {
  88. ["@click"]() {
  89. console.log("TITLE CLICK", this.$el);
  90. },
  91. },
  92. videoPlayLink: {
  93. ["@click.prevent"]() {
  94. browser.tabs.update({ url: this.$el.href });
  95. },
  96. },
  97. removeVideoButton: {
  98. ["@click"]() {
  99. this.removeVideo(
  100. this.$el.dataset.playlistName,
  101. this.$el.dataset.playlistIndex,
  102. );
  103. },
  104. },
  105. exportButton: {
  106. ["@click"]() {
  107. this.exportPlaylists();
  108. },
  109. },
  110. }));
  111. });