Explorar o código

fix: reloadTab behavior

Sv443 hai 5 meses
pai
achega
173c976be6
Modificáronse 7 ficheiros con 27 adicións e 29 borrados
  1. 2 2
      src/config.ts
  2. 2 5
      src/dialogs/importCfg.ts
  3. 2 2
      src/features/behavior.ts
  4. 2 2
      src/features/input.ts
  5. 2 3
      src/index.ts
  6. 4 5
      src/menu/menu_old.ts
  7. 13 10
      src/utils/misc.ts

+ 2 - 2
src/config.ts

@@ -1,5 +1,5 @@
 import { DataStore, compress, type DataMigrationsDict, decompress, type LooseUnion, clamp } from "@sv443-network/userutils";
-import { disableBeforeUnload, featInfo } from "./features/index.js";
+import { enableDiscardBeforeUnload, featInfo } from "./features/index.js";
 import { compressionSupported, error, getVideoTime, info, log, reloadTab, t, type TrLocale } from "./utils/index.js";
 import { emitSiteEvent } from "./siteEvents.js";
 import { compressionFormat } from "./constants.js";
@@ -253,7 +253,7 @@ export function setDefaultFeatures() {
 export async function promptResetConfig() {
   if(await showPrompt({ type: "confirm", message: t("reset_config_confirm") })) {
     closeCfgMenu();
-    disableBeforeUnload();
+    enableDiscardBeforeUnload();
     await setDefaultFeatures();
     if(location.pathname.startsWith("/watch")) {
       const videoTime = await getVideoTime(0);

+ 2 - 5
src/dialogs/importCfg.ts

@@ -3,7 +3,6 @@ import { BytmDialog } from "../components/index.js";
 import { scriptInfo } from "../constants.js";
 import { emitSiteEvent } from "../siteEvents.js";
 import { formatVersion, getFeatures, migrations, setFeatures } from "../config.js";
-import { disableBeforeUnload } from "../features/index.js";
 import { FeatureConfig } from "src/types.js";
 import { showPrompt } from "./prompt.js";
 
@@ -107,10 +106,8 @@ async function renderFooter() {
 
       await setFeatures({ ...getFeatures(), ...parsed.data });
 
-      if(await showPrompt({ type: "confirm", message: t("import_success_confirm_reload") })) {
-        disableBeforeUnload();
-        return reloadTab();
-      }
+      if(await showPrompt({ type: "confirm", message: t("import_success_confirm_reload") }))
+        return await reloadTab();
 
       emitSiteEvent("rebuildCfgMenu", parsed.data);
 

+ 2 - 2
src/features/behavior.ts

@@ -10,13 +10,13 @@ import { LogLevel } from "../types.js";
 let discardBeforeUnload = false;
 
 /** Disables the popup before leaving the site */
-export function disableBeforeUnload() {
+export function enableDiscardBeforeUnload() {
   discardBeforeUnload = true;
   info("Disabled popup before leaving the site");
 }
 
 /** (Re-)enables the popup before leaving the site */
-export function enableBeforeUnload() {
+export function disableDiscardBeforeUnload() {
   discardBeforeUnload = false;
   info("Enabled popup before leaving the site");
 }

+ 2 - 2
src/features/input.ts

@@ -1,7 +1,7 @@
 import { DataStore, clamp, compress, decompress } from "@sv443-network/userutils";
 import { error, getVideoTime, info, log, warn, getDomain, compressionSupported, t, clearNode, resourceAsString, getCurrentChannelId, getCurrentMediaType, sanitizeChannelId, addStyleFromResource, isValidChannelId, getVideoElement, setInnerHtml } from "../utils/index.js";
 import type { AutoLikeData, Domain } from "../types.js";
-import { disableBeforeUnload } from "./behavior.js";
+import { enableDiscardBeforeUnload } from "./behavior.js";
 import { emitSiteEvent, siteEvents } from "../siteEvents.js";
 import { featInfo } from "./index.js";
 import { getFeature } from "../config.js";
@@ -89,7 +89,7 @@ async function switchSite(newDomain: Domain) {
     if(!subdomain)
       throw new Error(`Unrecognized domain '${newDomain}'`);
 
-    disableBeforeUnload();
+    enableDiscardBeforeUnload();
 
     const { pathname, search, hash } = new URL(location.href);
 

+ 2 - 3
src/index.ts

@@ -19,7 +19,7 @@ import {
   // song lists
   initQueueButtons, initAboveQueueBtns,
   // behavior
-  initBeforeUnloadHook, disableBeforeUnload,
+  initBeforeUnloadHook, enableDiscardBeforeUnload,
   initAutoCloseToasts, initRememberSongTime,
   // input
   initArrowKeySkip, initSiteSwitch,
@@ -125,7 +125,7 @@ async function init() {
     }
 
     if(features.disableBeforeUnloadPopup && domain === "ytm")
-      disableBeforeUnload();
+      enableDiscardBeforeUnload();
 
     if(features.rememberSongTime)
       initRememberSongTime();
@@ -381,7 +381,6 @@ function registerDevCommands() {
   GM.registerMenuCommand("Reset config", async () => {
     if(confirm("Reset the configuration to its default values?\nThis will automatically reload the page.")) {
       await clearConfig();
-      disableBeforeUnload();
       await reloadTab();
     }
   }, "r");

+ 4 - 5
src/menu/menu_old.ts

@@ -1,7 +1,7 @@
 import { compress, debounce, isScrollable, type Stringifiable } from "@sv443-network/userutils";
 import { type defaultData, formatVersion, getFeature, getFeatures, migrations, setFeatures } from "../config.js";
 import { buildNumber, compressionFormat, host, mode, scriptInfo } from "../constants.js";
-import { featInfo, disableBeforeUnload } from "../features/index.js";
+import { featInfo } from "../features/index.js";
 import { error, getResourceUrl, info, log, resourceAsString, getLocale, hasKey, initTranslations, setLocale, t, arrayWithSeparators, tp, type TrKey, onInteraction, getDomain, copyToClipboard, warn, compressionSupported, tryToDecompressAndParse, setInnerHtml, type TrLocale, tl, reloadTab } from "../utils/index.js";
 import { emitSiteEvent, siteEvents } from "../siteEvents.js";
 import { getChangelogDialog, getFeatHelpDialog, showPrompt } from "../dialogs/index.js";
@@ -184,7 +184,6 @@ async function mountCfgMenu() {
   reloadTxtEl.ariaLabel = reloadTxtEl.title = t("reload_tooltip");
   reloadTxtEl.addEventListener("click", () => {
     closeCfgMenu();
-    disableBeforeUnload();
     reloadTab();
   });
 
@@ -243,7 +242,7 @@ async function mountCfgMenu() {
         await setFeatures({ ...getFeatures(), ...parsed.data });
 
         if(await showPrompt({ type: "confirm", message: t("import_success_confirm_reload") })) {
-          disableBeforeUnload();
+          log("Reloading tab after importing configuration");
           return reloadTab();
         }
 
@@ -332,8 +331,8 @@ async function mountCfgMenu() {
           denyBtnTooltip: (type) => `${t(type === "alert" ? "click_to_close_tooltip" : "click_to_cancel_tooltip")} / ${tl(initLocale!, type === "alert" ? "click_to_close_tooltip" : "click_to_cancel_tooltip")}`,
         })) {
           closeCfgMenu();
-          disableBeforeUnload();
-          reloadTab();
+          log("Reloading tab after changing language");
+          await reloadTab();
         }
       }
       else if(getLocale() !== featConf.locale)

+ 13 - 10
src/utils/misc.ts

@@ -3,6 +3,7 @@ import { marked } from "marked";
 import { branch, compressionFormat, repo, sessionStorageAvailable } from "../constants.js";
 import { type Domain, type NumberLengthFormat, type ResourceKey, type StringGen } from "../types.js";
 import { error, type TrLocale, warn, sendRequest, getLocale, log, getVideoElement, getVideoTime } from "./index.js";
+import { enableDiscardBeforeUnload } from "../features/behavior.js";
 import { getFeature } from "../config.js";
 import langMapping from "../../assets/locales.json" with { type: "json" };
 import resourcesJson from "../../assets/resources.json" with { type: "json" };
@@ -224,21 +225,23 @@ 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 parameter `time_continue` and `bytm-reload-tab-volume` in GM storage */
 export async function reloadTab() {
   try {
-    let time = 0, volume = 0;
+    enableDiscardBeforeUnload();
 
     if(getVideoElement()) {
-      time = (await getVideoTime() ?? 0) - 0.25;
-      volume = Math.round(getVideoElement()!.volume * 100);
-    }
+      const time = (await getVideoTime() ?? 0) - 0.25;
+      const volume = Math.round(getVideoElement()!.volume * 100);
+
+      const url = new URL(location.href);
 
-    const url = new URL(location.href);
+      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));
 
-    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));
+      return location.replace(url);
+    }
 
-    location.href = url.href;
+    location.reload();
   }
   catch(err) {
     error("Couldn't save video time and volume before reloading tab:", err);