浏览代码

ref: reorder num keys to skip code

Sv443 1 年之前
父节点
当前提交
53ab595f9e
共有 1 个文件被更改,包括 26 次插入25 次删除
  1. 26 25
      src/features/input.ts

+ 26 - 25
src/features/input.ts

@@ -199,32 +199,33 @@ export function initNumKeysSkip() {
   log("Added number key press listener");
 }
 
-/** Emulates a click on the video progress bar at the position calculated from the passed time key (0-9) */
-function skipToTimeKey(key: number) {
-  const getX = (timeKey: number, maxWidth: number) => {
-    if(timeKey >= 10)
-      return maxWidth;
-    return Math.floor((maxWidth / 10) * timeKey);
-  };
+/** Returns the x position as a fraction of timeKey in maxWidth */
+function getX(timeKey: number, maxWidth: number) {
+  if(timeKey >= 10)
+    return maxWidth;
+  return Math.floor((maxWidth / 10) * timeKey);
+}
 
-  /** Calculate offsets of the bounding client rect of the passed element - see https://stackoverflow.com/a/442474/11187044 */
-  const getOffsetRect = (elem: HTMLElement) => {
-    let left = 0;
-    let top = 0;
-    const rect = elem.getBoundingClientRect();
-    while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
-      left += elem.offsetLeft - elem.scrollLeft;
-      top += elem.offsetTop - elem.scrollTop;
-      elem = elem.offsetParent as HTMLElement;
-    }
-    return {
-      top,
-      left,
-      width: rect.width,
-      height: rect.height,
-    };
+/** Calculates DOM-relative offsets of the bounding client rect of the passed element - see https://stackoverflow.com/a/442474/11187044 */
+function getOffsetRect(elem: HTMLElement) {
+  let left = 0;
+  let top = 0;
+  const rect = elem.getBoundingClientRect();
+  while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
+    left += elem.offsetLeft - elem.scrollLeft;
+    top += elem.offsetTop - elem.scrollTop;
+    elem = elem.offsetParent as HTMLElement;
+  }
+  return {
+    top,
+    left,
+    width: rect.width,
+    height: rect.height,
   };
+}
 
+/** Emulates a click on the video progress bar at the position calculated from the passed time key (0-9) */
+function skipToTimeKey(key: number) {
   // not technically a progress element but behaves pretty much the same
   const progressElem = document.querySelector<HTMLProgressElement>("tp-yt-paper-slider#progress-bar tp-yt-paper-progress#sliderBar");
   if(!progressElem)
@@ -239,10 +240,10 @@ function skipToTimeKey(key: number) {
 
   const evt = new MouseEvent("mousedown", {
     clientX: x,
-    clientY: y,
+    clientY: Math.round(y),
     // @ts-ignore
     layerX: x,
-    layerY: rect.height / 2,
+    layerY: Math.round(rect.height / 2),
     target: progressElem,
     bubbles: true,
     shiftKey: false,