Selaa lähdekoodia

ref!: remove getVideoSelector and getVideoElement

Sv443 8 kuukautta sitten
vanhempi
commit
ae67de7f7d
5 muutettua tiedostoa jossa 9 lisäystä ja 63 poistoa
  1. 0 2
      changelog.md
  2. 0 50
      contributing.md
  3. 7 3
      src/features/behavior.ts
  4. 1 3
      src/interface.ts
  5. 1 5
      src/types.ts

+ 0 - 2
changelog.md

@@ -70,8 +70,6 @@
     - `fetchVideoVotes()` to fetch the approximate like and dislike count of a video from [Return Youtube Dislike](https://returnyoutubedislike.com/)
     - `getDomain()` returns the current domain ("yt" or "ytm")
     - `waitVideoElementReady()` returns a promise that resolves when the video element is ready
-    - `getVideoSelector()` returns the video element selector for the current domain
-    - `getVideoElement()` returns the video element for the current domain
   - SelectorObserver / `addSelectorListener()` changes:
     - Added `ytMasthead` instance for the title bar on YT
     - Renamed all YT-specific instances to have the `yt` prefix

+ 0 - 50
contributing.md

@@ -378,8 +378,6 @@ Functions marked with 🔒 need to be passed a per-session and per-plugin authen
   - [getThumbnailUrl()](#getthumbnailurl) - Returns the URL to the thumbnail of the currently playing video
   - [getBestThumbnailUrl()](#getbestthumbnailurl) - Returns the URL to the best quality thumbnail of the currently playing video
   - [waitVideoElementReady()](#waitvideoelementready) - Waits for the video element to be queryable in the DOM - has to be called after `bytm:observersReady`
-  - [getVideoSelector()](#getvideoselector) - Returns the CSS selector for the video element of the current domain
-  - [getVideoElement()](#getvideoelement) - Returns the video element of the current domain
 - Components:
   - [createHotkeyInput()](#createhotkeyinput) - Creates a hotkey input element
   - [createToggleInput()](#createtoggleinput) - Creates a toggle input element
@@ -840,54 +838,6 @@ Functions marked with 🔒 need to be passed a per-session and per-plugin authen
 
 <br>
 
-> #### getVideoSelector()
-> Usage:  
-> ```ts
-> unsafeWindow.BYTM.getVideoSelector(): string
-> ```
->   
-> Description:  
-> Returns the CSS selector for the video element of the current [domain.](#getdomain)  
->   
-> <details><summary><b>Example <i>(click to expand)</i></b></summary>
-> 
-> ```ts
-> const domain = unsafeWindow.BYTM.getDomain();
-> const videoSelector = unsafeWindow.BYTM.getVideoSelector();
-> 
-> console.log(domain, videoSelector); // "ytm", "ytmusic-player video"
-> ```
-> </details>
-
-<br>
-
-> #### getVideoElement()
-> Usage:  
-> ```ts
-> unsafeWindow.BYTM.getVideoElement(): HTMLVideoElement | null
-> ```
->   
-> Description:  
-> Returns the video element of the current domain (YT or YTM).  
->   
-> <details><summary><b>Example <i>(click to expand)</i></b></summary>
-> 
-> ```ts
-> async function pauseVideo() {
->   const videoElement = await unsafeWindow.BYTM.waitVideoElementReady();
->   videoElement?.pause();
-> }
-> 
-> window.addEventListener("bytm:observersReady", () => {
->   document.querySelector("#my-button").addEventListener("click", () => {
->     pauseVideo();
->   });
-> });
-> ```
-> </details>
-
-<br>
-
 > #### setLocale()
 > Usage:  
 > ```ts

+ 7 - 3
src/features/behavior.ts

@@ -1,5 +1,5 @@
 import { clamp, interceptWindowEvent, pauseFor } from "@sv443-network/userutils";
-import { domLoaded, error, getDomain, getVideoTime, getWatchId, info, log, getVideoSelector, waitVideoElementReady, clearNode, currentMediaType } from "../utils/index.js";
+import { domLoaded, error, getDomain, getVideoTime, getWatchId, info, log, waitVideoElementReady, clearNode, currentMediaType, dbg, getVideoElement } from "../utils/index.js";
 import { getFeature } from "../config.js";
 import { addSelectorListener } from "../observers.js";
 import { initialParams } from "../constants.js";
@@ -134,6 +134,7 @@ async function restVidRestoreTime() {
       else {
         let vidElem: HTMLVideoElement;
         const doRestoreTime = async () => {
+          dbg("Restoring time to", entry.songTime);
           if(!vidElem)
             vidElem = await waitVideoElementReady();
           const vidRestoreTime = entry.songTime - (getFeature("rememberSongTimeReduction") ?? 0);
@@ -163,11 +164,13 @@ async function restVidStartUpdateLoop() {
       return;
     lastSongTime = songTime;
 
+    dbg("># looped, different songTime:", songTime);
+
     const watchID = getWatchId();
     if(!watchID)
       return;
 
-    const paused = document.querySelector<HTMLVideoElement>(getVideoSelector())?.paused ?? false;
+    const paused = getVideoElement()?.paused ?? false;
 
     // don't immediately update to reduce race conditions and only update if the video is playing
     // also it just sounds better if the song starts at the beginning if only a couple seconds have passed
@@ -180,7 +183,7 @@ async function restVidStartUpdateLoop() {
       await restVidSetEntry(entry);
     }
     // if the song is rewound to the beginning, update the entry accordingly
-    else {
+    else if(!paused) {
       const entry = remVidsCache.find(entry => entry.watchID === watchID);
       if(entry && songTime <= entry.songTime)
         await restVidSetEntry({ ...entry, songTime, updateTimestamp: Date.now() });
@@ -210,6 +213,7 @@ async function restVidSetEntry(data: RemVidObj) {
 
 /** Deletes an entry */
 async function restVidDeleteEntry(watchID: string) {
+  dbg("Deleting entry with watchID", watchID);
   remVidsCache = [...remVidsCache.filter(entry => entry.watchID !== watchID)];
   await GM.setValue("bytm-rem-songs", JSON.stringify(remVidsCache));
 }

+ 1 - 3
src/interface.ts

@@ -1,7 +1,7 @@
 import * as UserUtils from "@sv443-network/userutils";
 import * as compareVersions from "compare-versions";
 import { mode, branch, host, buildNumber, compressionFormat, scriptInfo } from "./constants.js";
-import { getDomain, waitVideoElementReady, getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes, getVideoElement, getVideoSelector } from "./utils/index.js";
+import { getDomain, waitVideoElementReady, getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes } from "./utils/index.js";
 import { addSelectorListener } from "./observers.js";
 import { getFeatures, setFeatures } from "./config.js";
 import { autoLikeStore, featInfo, fetchLyricsUrlTop, getLyricsCacheEntry, sanitizeArtists, sanitizeSong } from "./features/index.js";
@@ -126,8 +126,6 @@ const globalFuncs: InterfaceFunctions = {
   getThumbnailUrl,
   getBestThumbnailUrl,
   waitVideoElementReady,
-  getVideoSelector,
-  getVideoElement,
 
   // translations:
   /**/ setLocale: setLocaleInterface,

+ 1 - 5
src/types.ts

@@ -4,7 +4,7 @@ import type { scriptInfo } from "./constants.js";
 import type { addSelectorListener } from "./observers.js";
 import type resources from "../assets/resources.json";
 import type locales from "../assets/locales.json";
-import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp, fetchVideoVotes, onInteraction, getThumbnailUrl, getBestThumbnailUrl, getLocale, hasKey, hasKeyFor, getVideoSelector, getVideoElement, getDomain, waitVideoElementReady } from "./utils/index.js";
+import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp, fetchVideoVotes, onInteraction, getThumbnailUrl, getBestThumbnailUrl, getLocale, hasKey, hasKeyFor, getDomain, waitVideoElementReady } from "./utils/index.js";
 import type { SiteEventsMap } from "./siteEvents.js";
 import type { InterfaceEventsMap, getAutoLikeDataInterface, getFeaturesInterface, getPluginInfo, registerPlugin, saveAutoLikeDataInterface, saveFeaturesInterface, setLocaleInterface } from "./interface.js";
 import type { BytmDialog, ExImDialog, createCircularBtn, createHotkeyInput, createRipple, createToggleInput, showIconToast, showToast } from "./components/index.js";
@@ -284,10 +284,6 @@ export type InterfaceFunctions = {
   getBestThumbnailUrl: typeof getBestThumbnailUrl;
   /** Resolves the returned promise when the video element is queryable in the DOM */
   waitVideoElementReady: typeof waitVideoElementReady;
-  /** Returns the video element's CSS selector for the current domain */
-  getVideoSelector: typeof getVideoSelector;
-  /** Returns the video element for the current domain or null if none could be found */
-  getVideoElement: typeof getVideoElement;
 
   // translations:
   /** 🔒 Sets the locale for all new translations */