|
|
@@ -159,6 +159,45 @@ function navigateTab(tabId, url) {
|
|
|
return browser.tabs.update(tabId, { url: url });
|
|
|
}
|
|
|
|
|
|
+async function updateTracking(message) {
|
|
|
+ // Get current playlists from storage
|
|
|
+ const { playlists } = await browser.storage.local.get("playlists");
|
|
|
+
|
|
|
+ // Find the video in playlists
|
|
|
+ const result = findVideoInPlaylists(playlists, message.url);
|
|
|
+
|
|
|
+ // If the video is not present in any playlist, do nothing
|
|
|
+ if (!result) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const [playlistName, videoIndex, _] = result;
|
|
|
+
|
|
|
+ // Only update for pause and ended events
|
|
|
+ if (message.type === "pause" || message.type === "ended") {
|
|
|
+ // Create a copy of the playlist for immutability
|
|
|
+ const updatedPlaylists = { ...playlists };
|
|
|
+ const targetPlaylist = [...updatedPlaylists[playlistName]];
|
|
|
+
|
|
|
+ // Get the current video object
|
|
|
+ const video = { ...targetPlaylist[videoIndex] };
|
|
|
+
|
|
|
+ // Update the video's status based on message type
|
|
|
+ if (message.type === "pause") {
|
|
|
+ video.status = message.timestamp; // Set status to current timestamp
|
|
|
+ } else if (message.type === "ended") {
|
|
|
+ video.status = "done"; // Set status to "done"
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update the video in the playlist
|
|
|
+ targetPlaylist[videoIndex] = video;
|
|
|
+ updatedPlaylists[playlistName] = targetPlaylist;
|
|
|
+
|
|
|
+ // Save the updated playlists back to storage
|
|
|
+ await browser.storage.local.set({ playlists: updatedPlaylists });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
async function updateHistory(message) {
|
|
|
const { history: currentHistory } =
|
|
|
await browser.storage.local.get("history");
|
|
|
@@ -267,9 +306,11 @@ browser.runtime.onMessage.addListener((message, sender, _sendResponse) => {
|
|
|
case "play":
|
|
|
case "playing":
|
|
|
case "pause":
|
|
|
+ updateTracking(message);
|
|
|
updateHistory(message);
|
|
|
break;
|
|
|
case "ended":
|
|
|
+ updateTracking(message);
|
|
|
updateHistory(message);
|
|
|
advance(sender.tab.id, message.url);
|
|
|
break;
|