Просмотр исходного кода

feat: hide unfinished features in menu

Sven 1 год назад
Родитель
Сommit
fab275d1c3
5 измененных файлов с 49 добавлено и 53 удалено
  1. 25 30
      dist/BetterYTM.user.js
  2. 9 5
      src/features/index.ts
  3. 2 2
      src/features/input.ts
  4. 6 5
      src/features/menu/menu_old.ts
  5. 7 11
      src/utils.ts

+ 25 - 30
dist/BetterYTM.user.js

@@ -486,7 +486,7 @@ const scriptInfo = Object.freeze({
     name: GM.info.script.name,
     version: GM.info.script.version,
     namespace: GM.info.script.namespace,
-    lastCommit: "5f1d963", // assert as generic string instead of union
+    lastCommit: "df39e39", // assert as generic string instead of union
 });
 
 
@@ -534,7 +534,7 @@ __webpack_require__.r(__webpack_exports__);
  // TODO
 
 /** Contains all possible features with their default values and other config */
-const featInfo = Object.freeze({
+const featInfo = {
     //#SECTION input
     arrowKeySupport: {
         desc: "Arrow keys skip forwards and backwards by 10 seconds",
@@ -549,7 +549,7 @@ const featInfo = Object.freeze({
         default: true,
     },
     switchSitesHotkey: {
-        desc: "Which hotkey needs to be pressed to switch sites?",
+        desc: "TODO: Which hotkey needs to be pressed to switch sites?",
         type: "hotkey",
         category: "input",
         default: {
@@ -558,9 +558,10 @@ const featInfo = Object.freeze({
             ctrl: false,
             meta: false,
         },
+        visible: false,
     },
     disableBeforeUnloadPopup: {
-        desc: "Whether to completely disable the popup that sometimes appears before leaving the site",
+        desc: "Completely disable the popup that sometimes appears before leaving the site",
         type: "toggle",
         category: "input",
         default: false,
@@ -570,6 +571,7 @@ const featInfo = Object.freeze({
         type: "toggle",
         category: "input",
         default: true,
+        visible: false,
     },
     //#SECTION layout
     removeUpgradeTab: {
@@ -585,8 +587,8 @@ const featInfo = Object.freeze({
         min: 10,
         max: 1000,
         step: 5,
-        unit: "px",
         default: 160,
+        unit: "px",
     },
     volumeSliderStep: {
         desc: "Volume slider sensitivity - the smaller this number, the finer the volume control",
@@ -607,12 +609,14 @@ const featInfo = Object.freeze({
         type: "toggle",
         category: "layout",
         default: true,
+        visible: false,
     },
     dismissPopup: {
         desc: "TODO: Automatically dismisses the \"are you still there\" popup",
         type: "toggle",
         category: "layout",
         default: true,
+        visible: false,
     },
     //#SECTION lyrics
     geniusLyrics: {
@@ -621,7 +625,7 @@ const featInfo = Object.freeze({
         category: "lyrics",
         default: true,
     },
-});
+};
 
 
 /***/ }),
@@ -674,7 +678,7 @@ function onKeyDown(evt) {
             cancelable: true,
             isTrusted: true,
             repeat: false,
-            view: unsafeWindow,
+            view: unsafeWindow !== null && unsafeWindow !== void 0 ? unsafeWindow : window,
         };
         let invalidKey = false;
         let keyProps = {};
@@ -740,7 +744,7 @@ function switchSite(newDomain) {
         const url = `https://${subdomain}.youtube.com${pathname}${newSearch}${hash}`;
         console.info(`BetterYTM - switching to domain '${newDomain}' at ${url}`);
         disableBeforeUnload();
-        setTimeout(() => { location.href = url; }, 0);
+        location.assign(url);
     }
     catch (err) {
         console.error("Error while switching site:", err);
@@ -1322,7 +1326,8 @@ function addMenu() {
         const features = yield (0,_config__WEBPACK_IMPORTED_MODULE_0__.getFeatures)();
         for (const key in features) {
             const ftInfo = _index__WEBPACK_IMPORTED_MODULE_2__.featInfo[key];
-            if (!ftInfo)
+            // @ts-ignore
+            if (!ftInfo || ftInfo.visible === false)
                 continue;
             const { desc, type, default: ftDefault } = ftInfo;
             // @ts-ignore
@@ -1379,7 +1384,7 @@ function addMenu() {
                 if (type === "toggle" && typeof initialVal !== "undefined")
                     inputElem.checked = Boolean(initialVal);
                 // @ts-ignore
-                const unitTxt = (ftInfo === null || ftInfo === void 0 ? void 0 : ftInfo.unit) ? " " + ftInfo.unit : "";
+                const unitTxt = typeof ftInfo.unit === "string" ? " " + ftInfo.unit : "";
                 const fmtVal = (v) => String(v).trim();
                 const toggleLabelText = (toggled) => toggled ? "On" : "Off";
                 let labelElem;
@@ -1392,7 +1397,7 @@ function addMenu() {
                     labelElem.innerText = fmtVal(initialVal) + unitTxt;
                     inputElem.addEventListener("input", () => {
                         if (labelElem)
-                            labelElem.innerText = fmtVal(parseInt(inputElem.value));
+                            labelElem.innerText = fmtVal(parseInt(inputElem.value)) + unitTxt;
                     });
                 }
                 else if (type === "toggle") {
@@ -1421,9 +1426,9 @@ function addMenu() {
                     inputElem[type !== "toggle" ? "value" : "checked"] = ftDefault;
                     if (labelElem) {
                         if (type === "toggle")
-                            labelElem.innerText = toggleLabelText(inputElem.checked) + unitTxt;
+                            labelElem.innerText = toggleLabelText(inputElem.checked);
                         else
-                            labelElem.innerText = fmtVal(parseInt(inputElem.value)) + unitTxt;
+                            labelElem.innerText = fmtVal(parseInt(inputElem.value));
                     }
                     if (typeof initialVal !== "undefined")
                         confChanged(key, initialVal, ftDefault);
@@ -1674,29 +1679,19 @@ function ytForceShowVideoTime() {
     const player = document.querySelector("#movie_player");
     if (!player)
         return false;
-    player.dispatchEvent(new MouseEvent("mouseenter", {
-        view: unsafeWindow,
+    const defaultProps = {
+        view: unsafeWindow !== null && unsafeWindow !== void 0 ? unsafeWindow : window,
         bubbles: true,
         cancelable: false,
-    }));
+    };
+    player.dispatchEvent(new MouseEvent("mouseenter", defaultProps));
     const { x, y, width, height } = player.getBoundingClientRect();
     const screenY = Math.round(y + height / 2);
     const screenX = x + Math.min(50, Math.round(width / 3));
-    player.dispatchEvent(new MouseEvent("mousemove", {
-        view: unsafeWindow,
-        bubbles: true,
-        cancelable: false,
-        screenY,
-        screenX,
-        movementX: 5,
-        movementY: 0
-    }));
+    player.dispatchEvent(new MouseEvent("mousemove", Object.assign(Object.assign({}, defaultProps), { screenY,
+        screenX, movementX: 5, movementY: 0 })));
     setTimeout(() => {
-        player.dispatchEvent(new MouseEvent("mouseleave", {
-            view: unsafeWindow,
-            bubbles: true,
-            cancelable: false,
-        }));
+        player.dispatchEvent(new MouseEvent("mouseleave", defaultProps));
     }, 4000);
     return true;
 }

+ 9 - 5
src/features/index.ts

@@ -9,7 +9,7 @@ export * from "./menu/menu_old";
 export type FeatureCategory = "input" | "layout" | "lyrics";
 
 /** Contains all possible features with their default values and other config */
-export const featInfo = Object.freeze({
+export const featInfo = {
   //#SECTION input
   arrowKeySupport: {
     desc: "Arrow keys skip forwards and backwards by 10 seconds",
@@ -24,7 +24,7 @@ export const featInfo = Object.freeze({
     default: true,
   },
   switchSitesHotkey: {
-    desc: "Which hotkey needs to be pressed to switch sites?",
+    desc: "TODO: Which hotkey needs to be pressed to switch sites?",
     type: "hotkey",
     category: "input",
     default: {
@@ -33,9 +33,10 @@ export const featInfo = Object.freeze({
       ctrl: false,
       meta: false,
     },
+    visible: false,
   },
   disableBeforeUnloadPopup: {
-    desc: "Whether to completely disable the popup that sometimes appears before leaving the site",
+    desc: "Completely disable the popup that sometimes appears before leaving the site",
     type: "toggle",
     category: "input",
     default: false,
@@ -45,6 +46,7 @@ export const featInfo = Object.freeze({
     type: "toggle",
     category: "input",
     default: true,
+    visible: false,
   },
 
   //#SECTION layout
@@ -61,8 +63,8 @@ export const featInfo = Object.freeze({
     min: 10,
     max: 1000,
     step: 5,
-    unit: "px",
     default: 160,
+    unit: "px",
   },
   volumeSliderStep: {
     desc: "Volume slider sensitivity - the smaller this number, the finer the volume control",
@@ -83,12 +85,14 @@ export const featInfo = Object.freeze({
     type: "toggle",
     category: "layout",
     default: true,
+    visible: false,
   },
   dismissPopup: {
     desc: "TODO: Automatically dismisses the \"are you still there\" popup",
     type: "toggle",
     category: "layout",
     default: true,
+    visible: false,
   },
 
   //#SECTION lyrics
@@ -98,4 +102,4 @@ export const featInfo = Object.freeze({
     category: "lyrics",
     default: true,
   },
-});
+};

+ 2 - 2
src/features/input.ts

@@ -35,7 +35,7 @@ function onKeyDown(evt: KeyboardEvent) {
       cancelable: true,
       isTrusted: true,
       repeat: false,
-      view: unsafeWindow,
+      view: unsafeWindow ?? window,
     };
 
     let invalidKey = false;
@@ -118,7 +118,7 @@ function switchSite(newDomain: Domain) {
     console.info(`BetterYTM - switching to domain '${newDomain}' at ${url}`);
 
     disableBeforeUnload();
-    setTimeout(() => { location.href = url; }, 0);
+    location.assign(url);
   }
   catch(err) {
     console.error("Error while switching site:", err);

+ 6 - 5
src/features/menu/menu_old.ts

@@ -108,7 +108,8 @@ export async function addMenu() {
   for(const key in features) {
     const ftInfo = featInfo[key as keyof typeof features];
 
-    if(!ftInfo)
+    // @ts-ignore
+    if(!ftInfo || ftInfo.visible === false)
       continue;
 
     const { desc, type, default: ftDefault } = ftInfo;
@@ -179,7 +180,7 @@ export async function addMenu() {
         inputElem.checked = Boolean(initialVal);
 
       // @ts-ignore
-      const unitTxt = ftInfo?.unit ? " " + ftInfo.unit : "";
+      const unitTxt = typeof ftInfo.unit === "string" ? " " + ftInfo.unit : "";
 
       const fmtVal = (v: unknown) => String(v).trim();
       const toggleLabelText = (toggled: boolean) => toggled ? "On" : "Off";
@@ -195,7 +196,7 @@ export async function addMenu() {
 
         inputElem.addEventListener("input", () => {
           if(labelElem)
-            labelElem.innerText = fmtVal(parseInt(inputElem.value));
+            labelElem.innerText = fmtVal(parseInt(inputElem.value)) + unitTxt;
         });
       }
       else if(type === "toggle") {
@@ -228,9 +229,9 @@ export async function addMenu() {
 
         if(labelElem) {
           if(type === "toggle")
-            labelElem.innerText = toggleLabelText(inputElem.checked) + unitTxt;
+            labelElem.innerText = toggleLabelText(inputElem.checked);
           else
-            labelElem.innerText = fmtVal(parseInt(inputElem.value)) + unitTxt;
+            labelElem.innerText = fmtVal(parseInt(inputElem.value));
         }
 
         if(typeof initialVal !== "undefined")

+ 7 - 11
src/utils.ts

@@ -114,20 +114,20 @@ function ytForceShowVideoTime() {
   if(!player)
     return false;
 
-  player.dispatchEvent(new MouseEvent("mouseenter", {
-    view: unsafeWindow,
+  const defaultProps = {
+    view: unsafeWindow ?? window,
     bubbles: true,
     cancelable: false,
-  }));
+  };
+
+  player.dispatchEvent(new MouseEvent("mouseenter", defaultProps));
 
   const { x, y, width, height } = player.getBoundingClientRect();
   const screenY = Math.round(y + height / 2);
   const screenX = x + Math.min(50, Math.round(width / 3));
 
   player.dispatchEvent(new MouseEvent("mousemove", {
-    view: unsafeWindow,
-    bubbles: true,
-    cancelable: false,
+    ...defaultProps,
     screenY,
     screenX,
     movementX: 5,
@@ -135,11 +135,7 @@ function ytForceShowVideoTime() {
   }));
 
   setTimeout(() => {
-    player.dispatchEvent(new MouseEvent("mouseleave", {
-      view: unsafeWindow,
-      bubbles: true,
-      cancelable: false,
-    }));
+    player.dispatchEvent(new MouseEvent("mouseleave", defaultProps));
   }, 4000);
 
   return true;