From 6fc8eaf536c2b91a050a3dcae7f8eca748f7c769 Mon Sep 17 00:00:00 2001 From: Sv443 Date: Sat, 23 Nov 2024 21:22:47 +0100 Subject: [PATCH] fix: using GM storage instead of URL params cause YTM strips them out --- changelog.md | 1 + src/features/volume.ts | 22 +++++++++++++--------- src/interface.ts | 4 +++- src/types.ts | 2 +- src/utils/misc.ts | 33 ++++++++++++++++++++------------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/changelog.md b/changelog.md index 67c08b297..b266f7f97 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,7 @@ - **Plugin Changes:** - Added authenticated function `reloadTab()` to keep video time and volume and disable BYTM features like initial tab volume + - Exposed the constants `initialParams` (initial URLParams object) and `sessionStorageAvailable` (bool) on the BYTM API diff --git a/src/features/volume.ts b/src/features/volume.ts index 44755f8f0..4e07fb816 100644 --- a/src/features/volume.ts +++ b/src/features/volume.ts @@ -3,8 +3,8 @@ import { getFeature } from "../config.js"; import { addStyleFromResource, error, log, resourceAsString, setGlobalCssVar, setInnerHtml, t, waitVideoElementReady, warn } from "../utils/index.js"; import { siteEvents } from "../siteEvents.js"; import { featInfo } from "./index.js"; -import "./volume.css"; import { addSelectorListener } from "../observers.js"; +import "./volume.css"; //#region init vol features @@ -36,8 +36,7 @@ export async function initVolumeFeatures() { // the following are only run once: - if(getFeature("setInitialTabVolume") || new URL(location.href).searchParams.has("bytm_volume")) - setInitialTabVolume(sliderElem); + setInitialTabVolume(sliderElem); if(typeof getFeature("volumeSliderSize") === "number") setVolSliderSize(); @@ -248,18 +247,23 @@ export async function volumeSharedBetweenTabsDisabled() { /** Sets the volume slider to a set volume level when the session starts */ async function setInitialTabVolume(sliderElem: HTMLInputElement) { + const reloadTabVol = Number(await GM.getValue("bytm-reload-tab-volume", 0)); + GM.deleteValue("bytm-reload-tab-volume").catch(() => void 0); + + if(!getFeature("setInitialTabVolume") || reloadTabVol === 0 || isNaN(reloadTabVol)) + return; + await waitVideoElementReady(); - const urlVol = new URL(location.href).searchParams.get("bytm_volume"); - const initialVol = urlVol ? Number(urlVol) / 100 : getFeature("initialTabVolumeLevel"); + + const initialVol = reloadTabVol > 0 ? Math.round(reloadTabVol) : getFeature("initialTabVolumeLevel"); + if(getFeature("volumeSharedBetweenTabs")) { lastCheckedSharedVolume = ignoreVal = initialVol; if(getFeature("volumeSharedBetweenTabs")) - GM.setValue("bytm-shared-volume", String(initialVol)); + GM.setValue("bytm-shared-volume", String(initialVol)).catch((err) => error("Couldn't save shared volume level due to an error:", err)); } sliderElem.value = String(initialVol); sliderElem.dispatchEvent(new Event("change", { bubbles: true })); - urlVol && history.replaceState({}, document.title, location.href.replace(/([&?]?bytm_volume=\d+($|&))/g, "")); - - log(`Set initial tab volume to ${initialVol}%${urlVol ? " (from URL)" : "(from configuration)"}`); + log(`Set initial tab volume to ${initialVol}%${reloadTabVol > 0 ? " (from GM storage)" : " (from configuration)"}`); } diff --git a/src/interface.ts b/src/interface.ts index a331c3123..fe6f35d2a 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,6 +1,6 @@ import * as UserUtils from "@sv443-network/userutils"; import * as compareVersions from "compare-versions"; -import { mode, branch, host, buildNumber, compressionFormat, scriptInfo } from "./constants.js"; +import { mode, branch, host, buildNumber, compressionFormat, scriptInfo, initialParams, sessionStorageAvailable } from "./constants.js"; import { getDomain, waitVideoElementReady, getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes, setInnerHtml, getCurrentMediaType, tl, tlp, PluginError, formatNumber, reloadTab } from "./utils/index.js"; import { addSelectorListener } from "./observers.js"; import { getFeatures, setFeatures } from "./config.js"; @@ -167,7 +167,9 @@ export function initInterface() { branch, host, buildNumber, + initialParams, compressionFormat, + sessionStorageAvailable, ...scriptInfo, // functions ...globalFuncs, diff --git a/src/types.ts b/src/types.ts index c4cd10d7c..27482d443 100644 --- a/src/types.ts +++ b/src/types.ts @@ -120,7 +120,7 @@ export type BytmObject = // information from the userscript header & typeof scriptInfo // certain variables from `src/constants.ts` - & Pick + & Pick // global functions exposed through the interface in `src/interface.ts` & InterfaceFunctions // others diff --git a/src/utils/misc.ts b/src/utils/misc.ts index acd706d87..1788f1760 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -1,4 +1,4 @@ -import { clamp, compress, decompress, fetchAdvanced, openInNewTab, pauseFor, randomId, randRange, type Prettify } from "@sv443-network/userutils"; +import { compress, decompress, fetchAdvanced, openInNewTab, pauseFor, randomId, randRange, type Prettify } from "@sv443-network/userutils"; import { marked } from "marked"; import { branch, compressionFormat, repo, sessionStorageAvailable } from "../constants.js"; import { type Domain, type NumberLengthFormat, type ResourceKey, type StringGen } from "../types.js"; @@ -221,22 +221,29 @@ export function formatNumber(num: number, notation?: NumberLengthFormat): string ); } -/** Reloads the tab. If a video is currently playing, its time and volume will be preserved through the URL parameters `time_continue` and `bytm_volume` */ +/** Reloads the tab. If a video is currently playing, its time and volume will be preserved through the URL parameter `time_continue` and `bytm-reload-tab-volume` in GM storage */ export async function reloadTab() { - let time = 0, volume = 0; + try { + let time = 0, volume = 0; - if(getVideoElement()) { - time = (await getVideoTime() ?? 0) - 0.25; - volume = clamp(Math.round(getVideoElement()!.volume * 100), 0, 100); - } + if(getVideoElement()) { + time = (await getVideoTime() ?? 0) - 0.25; + volume = Math.round(getVideoElement()!.volume * 100); + } + + const url = new URL(location.href); - const url = new URL(location.href); - if(time > 0) - url.searchParams.set("time_continue", String(time)); - if(volume > 0) - url.searchParams.set("bytm_volume", String(volume)); + if(isNaN(time) && time > 0) + url.searchParams.set("time_continue", String(time)); + if(isNaN(volume) && volume > 0) + await GM.setValue("bytm-reload-tab-volume", String(volume)); - location.href = url.href; + location.href = url.href; + } + catch(err) { + error("Couldn't save video time and volume before reloading tab:", err); + location.reload(); + } } //#region resources