Skip to content

Commit 0a01ba8

Browse files
committed
media q
1 parent 581ab53 commit 0a01ba8

1 file changed

Lines changed: 77 additions & 25 deletions

File tree

speed_rewind.js

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -453,28 +453,24 @@ if (window.top === window.self) {
453453
}
454454
}
455455
let shiftPressed = false;
456+
let lastShiftPressTime = 0;
457+
const DOUBLE_PRESS_THRESHOLD = 500;
456458
function handleShift(event) {
457-
shiftPressed = event.detail.action === "pressed";
458-
459-
if (lockedSpeed && shiftPressed && video.playbackRate !== initialSpeed) {
460-
console.log(
461-
"keyboardShift if: video.playbackRate !== initialSpeed: ",
462-
video.playbackRate !== initialSpeed,
463-
video.playbackRate,
464-
initialSpeed
465-
);
466-
lockedSpeed = false;
467-
468-
currentSpeed = initialSpeed;
469-
video.playbackRate = initialSpeed;
470-
showSpeedDisplay();
471-
} else {
472-
console.log(
473-
"keyboardShift else: video.playbackRate !== initialSpeed: ",
474-
video.playbackRate !== initialSpeed,
475-
video.playbackRate,
476-
initialSpeed
477-
);
459+
const isPressed = event.detail.action === "pressed";
460+
shiftPressed = isPressed;
461+
462+
if (isPressed) {
463+
const now = Date.now();
464+
const isDoublePress = now - lastShiftPressTime < DOUBLE_PRESS_THRESHOLD;
465+
lastShiftPressTime = now;
466+
467+
if (lockedSpeed && isDoublePress && video.playbackRate !== initialSpeed) {
468+
console.log("Double Shift detected - resetting speed");
469+
lockedSpeed = false;
470+
currentSpeed = initialSpeed;
471+
video.playbackRate = initialSpeed;
472+
showSpeedDisplay();
473+
}
478474
}
479475
}
480476

@@ -561,10 +557,66 @@ if (window.top === window.self) {
561557
video.currentTime = Math.max(0, video.currentTime - 3);
562558
}
563559
});
564-
document.addEventListener("mediaPlay", (event) => {
565-
// https://github.com/stopsopa/os-browser-bridge/blob/main/SPECIAL.md#detect-media-keys
566-
document.querySelector('tp-yt-paper-dialog [id="close-button"]')?.click();
567-
});
560+
561+
562+
(function () {
563+
// everything in this block goes together wiht https://github.com/stopsopa/os-browser-bridge/commit/0b99fd4cae061846325749571ca52acc2ad4164b
564+
document.addEventListener("mediaPlay", (event) => {
565+
log("mediaPlay:", event.detail.action, "document.hidden", document.hidden);
566+
567+
if (document.hidden) {
568+
return;
569+
}
570+
571+
if (event.detail.action === "pressed") {
572+
document.querySelector('tp-yt-paper-dialog [id="close-button"]')?.click();
573+
return;
574+
// let's not do that here, allow native media keyboard buttons to control it
575+
// this way when I take off my headphones we give a chance for native event to stop music
576+
if (video.paused) {
577+
video.play();
578+
} else {
579+
video.pause();
580+
}
581+
}
582+
});
583+
584+
(function () {
585+
let isCommandPressed = false;
586+
document.addEventListener("keyboardCommand", (e) => {
587+
isCommandPressed = e.detail.action === "pressed";
588+
});
589+
document.addEventListener(
590+
"mediaNext",
591+
(e) => {
592+
if (document.hidden) {
593+
return;
594+
}
595+
if (isCommandPressed && e.detail.action === "pressed") {
596+
log("→ Playlist Next (Local override)");
597+
document.querySelector(".ytp-next-button")?.click();
598+
e.stopImmediatePropagation();
599+
}
600+
},
601+
true
602+
);
603+
document.addEventListener(
604+
"mediaPrevious",
605+
(e) => {
606+
if (document.hidden) {
607+
return;
608+
}
609+
if (isCommandPressed && e.detail.action === "pressed") {
610+
log("→ Playlist Previous (Local override)");
611+
document.querySelector(".ytp-prev-button")?.click();
612+
e.stopImmediatePropagation();
613+
}
614+
},
615+
true
616+
);
617+
618+
})();
619+
}());
568620
} catch (e) {
569621
log("general try catch error:", e);
570622
}

0 commit comments

Comments
 (0)