add nudge to settings

This commit is contained in:
Josh Patra
2025-05-19 12:54:35 -04:00
parent 278baa1a05
commit 2d8a4fc25f
5 changed files with 352 additions and 399 deletions

403
inject.js
View File

@@ -2,16 +2,15 @@ var regStrip = /^[\r\t\f\v ]+|[\r\t\f\v ]+$/gm;
var tc = { var tc = {
settings: { settings: {
lastSpeed: 1.0, // default 1x lastSpeed: 1.0,
enabled: true, // default enabled enabled: true,
speeds: {}, // empty object to hold speed for each source speeds: {},
displayKeyCode: 86,
displayKeyCode: 86, // default: V rememberSpeed: false,
rememberSpeed: false, // default: false forceLastSavedSpeed: false,
forceLastSavedSpeed: false, //default: false audioBoolean: false,
audioBoolean: false, // default: false startHidden: false,
startHidden: false, // default: false controllerOpacity: 0.3,
controllerOpacity: 0.3, // default: 0.3
keyBindings: [], keyBindings: [],
blacklist: `\ blacklist: `\
www.instagram.com www.instagram.com
@@ -27,30 +26,21 @@ var tc = {
subtitleNudgeInterval: 25, subtitleNudgeInterval: 25,
subtitleNudgeAmount: 0.001 subtitleNudgeAmount: 0.001
}, },
// Holds a reference to all of the AUDIO/VIDEO DOM elements we've attached to
mediaElements: [], mediaElements: [],
isNudging: false // Flag for nudge operation (ADDED) isNudging: false // ADDED: Flag for nudge operation
}; };
/* Log levels (depends on caller specifying the correct level) */ /* Log levels */
function log(message, level) { function log(message, level) {
verbosity = tc.settings.logLevel; verbosity = tc.settings.logLevel;
if (typeof level === "undefined") { if (typeof level === "undefined") level = tc.settings.defaultLogLevel;
level = tc.settings.defaultLogLevel;
}
if (verbosity >= level) { if (verbosity >= level) {
// MODIFIED: Added [VSC] prefix for clarity
let prefix = "[VSC] "; let prefix = "[VSC] ";
if (level === 2) { if (level === 2) console.log(prefix + "ERROR: " + message);
console.log(prefix + "ERROR: " + message); else if (level === 3) console.log(prefix + "WARNING: " + message);
} else if (level === 3) { else if (level === 4) console.log(prefix + "INFO: " + message);
console.log(prefix + "WARNING: " + message); else if (level === 5) console.log(prefix + "DEBUG: " + message);
} else if (level === 4) { else if (level === 6) {
console.log(prefix + "INFO: " + message);
} else if (level === 5) {
console.log(prefix + "DEBUG: " + message);
} else if (level === 6) {
console.log(prefix + "DEBUG (VERBOSE): " + message); console.log(prefix + "DEBUG (VERBOSE): " + message);
console.trace(); console.trace();
} }
@@ -58,7 +48,7 @@ function log(message, level) {
} }
chrome.storage.sync.get(tc.settings, function (storage) { chrome.storage.sync.get(tc.settings, function (storage) {
// MODIFIED: Robust keyBinding initialization from storage or defaults. // MODIFIED: Robust keyBinding initialization
tc.settings.keyBindings = tc.settings.keyBindings =
Array.isArray(storage.keyBindings) && Array.isArray(storage.keyBindings) &&
storage.keyBindings.length > 0 && storage.keyBindings.length > 0 &&
@@ -99,7 +89,7 @@ chrome.storage.sync.get(tc.settings, function (storage) {
value: 1.0, value: 1.0,
force: false, force: false,
predefined: true predefined: true
}, // Default value for reset action is 1.0 },
{ {
action: "fast", action: "fast",
key: Number(storage.fastKeyCode) || 71, key: Number(storage.fastKeyCode) || 71,
@@ -117,7 +107,7 @@ chrome.storage.sync.get(tc.settings, function (storage) {
log("Initializing/Updating keybindings in storage.", 4); log("Initializing/Updating keybindings in storage.", 4);
chrome.storage.sync.set({ chrome.storage.sync.set({
keyBindings: tc.settings.keyBindings, keyBindings: tc.settings.keyBindings,
version: "0.6.3.8" version: "0.6.3.10"
}); // Update version }); // Update version
} }
@@ -132,16 +122,16 @@ chrome.storage.sync.get(tc.settings, function (storage) {
tc.settings.controllerOpacity = Number(storage.controllerOpacity) || 0.3; tc.settings.controllerOpacity = Number(storage.controllerOpacity) || 0.3;
tc.settings.blacklist = String(storage.blacklist || tc.settings.blacklist); tc.settings.blacklist = String(storage.blacklist || tc.settings.blacklist);
if (typeof storage.logLevel !== "undefined") { if (typeof storage.logLevel !== "undefined")
tc.settings.logLevel = Number(storage.logLevel); tc.settings.logLevel = Number(storage.logLevel);
}
// ADDED: Load nudge settings from storage // ADDED: Load nudge settings
tc.settings.enableSubtitleNudge = tc.settings.enableSubtitleNudge =
typeof storage.enableSubtitleNudge !== "undefined" typeof storage.enableSubtitleNudge !== "undefined"
? Boolean(storage.enableSubtitleNudge) ? Boolean(storage.enableSubtitleNudge)
: tc.settings.enableSubtitleNudge; : tc.settings.enableSubtitleNudge;
tc.settings.subtitleNudgeInterval = tc.settings.subtitleNudgeInterval =
Number(storage.subtitleNudgeInterval) || 25; // Using 25ms as requested Number(storage.subtitleNudgeInterval) || 25;
tc.settings.subtitleNudgeAmount = tc.settings.subtitleNudgeAmount =
Number(storage.subtitleNudgeAmount) || tc.settings.subtitleNudgeAmount; Number(storage.subtitleNudgeAmount) || tc.settings.subtitleNudgeAmount;
@@ -156,18 +146,18 @@ chrome.storage.sync.get(tc.settings, function (storage) {
predefined: true predefined: true
}); });
} }
initializeWhenReady(document); initializeWhenReady(document);
}); });
function getKeyBindings(action, what = "value") { function getKeyBindings(action, what = "value") {
if (!tc.settings.keyBindings) return false; // ADDED: Guard against undefined keyBindings // Original getKeyBindings
if (!tc.settings.keyBindings) return false;
try { try {
const binding = tc.settings.keyBindings.find( const binding = tc.settings.keyBindings.find(
(item) => item.action === action (item) => item.action === action
); );
if (binding) return binding[what]; if (binding) return binding[what];
// Fallback defaults for safety // Fallbacks from original
if (what === "value") { if (what === "value") {
if (action === "slower" || action === "faster") return 0.1; if (action === "slower" || action === "faster") return 0.1;
if (action === "rewind" || action === "advance") return 10; if (action === "rewind" || action === "advance") return 10;
@@ -181,64 +171,42 @@ function getKeyBindings(action, what = "value") {
} }
} }
// Original setKeyBindings, used by original resetSpeed logic. // Original setKeyBindings from your provided code (used by original resetSpeed)
function setKeyBindings(action, value) { function setKeyBindings(action, value) {
if (!tc.settings.keyBindings) return; // ADDED: Guard if (!tc.settings.keyBindings) return;
const binding = tc.settings.keyBindings.find( const binding = tc.settings.keyBindings.find(
(item) => item.action === action (item) => item.action === action
); );
if (binding) { if (binding) {
binding["value"] = value; binding["value"] = value;
log(
`In-memory value for keyBinding '${action}' set to ${value} by original setKeyBindings func`,
6
);
} }
} }
function defineVideoController() { function defineVideoController() {
tc.videoController = function (target, parent) { tc.videoController = function (target, parent) {
if (target.vsc) { if (target.vsc) return target.vsc;
log(`VSC controller already exists for ${target.src || "video"}.`, 6);
return target.vsc;
}
log(`Creating VSC controller for ${target.src || "video"}.`, 4); log(`Creating VSC controller for ${target.src || "video"}.`, 4);
tc.mediaElements.push(target); tc.mediaElements.push(target);
target.vsc = this; target.vsc = this;
this.video = target; this.video = target;
this.parent = parent || target.parentElement; this.parent = parent || target.parentElement;
this.nudgeIntervalId = null; // ADDED: For the subtitle nudge feature this.nudgeIntervalId = null; // ADDED for nudge
// Original logic for determining initial speed let storedSpeed; // Original logic
let storedSpeed;
if (!tc.settings.rememberSpeed) { if (!tc.settings.rememberSpeed) {
storedSpeed = tc.settings.speeds[target.currentSrc]; storedSpeed = tc.settings.speeds[target.currentSrc];
if (!storedSpeed) { if (!storedSpeed) storedSpeed = 1.0;
log( setKeyBindings("reset", getKeyBindings("fast"));
"Overwriting stored speed to 1.0 due to rememberSpeed being disabled or no speed for src",
5
);
storedSpeed = 1.0;
}
setKeyBindings("reset", getKeyBindings("fast")); // Original logic for 'R' key toggle state
} else { } else {
log("Recalling stored speed due to rememberSpeed being enabled", 5);
storedSpeed = storedSpeed =
tc.settings.speeds[target.currentSrc] || tc.settings.lastSpeed; tc.settings.speeds[target.currentSrc] || tc.settings.lastSpeed;
} }
if (tc.settings.forceLastSavedSpeed) { if (tc.settings.forceLastSavedSpeed) storedSpeed = tc.settings.lastSpeed;
storedSpeed = tc.settings.lastSpeed;
}
this.div = this.initializeControls(); this.div = this.initializeControls();
if (Math.abs(target.playbackRate - storedSpeed) > 0.001) { if (Math.abs(target.playbackRate - storedSpeed) > 0.001) {
log(
`Video current rate ${target.playbackRate.toFixed(2)} differs from VSC target ${storedSpeed.toFixed(2)}. Setting speed (initial).`,
4
);
setSpeed(target, storedSpeed, true); // MODIFIED: Pass true for isInitialSet setSpeed(target, storedSpeed, true); // MODIFIED: Pass true for isInitialSet
} else { } else {
if (this.speedIndicator) if (this.speedIndicator)
@@ -251,12 +219,11 @@ function defineVideoController() {
} }
} }
// Original mediaEventAction
var mediaEventAction = function (event) { var mediaEventAction = function (event) {
// Original mediaEventAction
const video = event.target; const video = event.target;
if (!video.vsc) return; if (!video.vsc) return;
let speedToSet = tc.settings.speeds[video.currentSrc];
let speedToSet = tc.settings.speeds[video.currentSrc]; // Use 'let'
if (!tc.settings.rememberSpeed) { if (!tc.settings.rememberSpeed) {
if (!speedToSet) speedToSet = 1.0; if (!speedToSet) speedToSet = 1.0;
setKeyBindings("reset", getKeyBindings("fast")); setKeyBindings("reset", getKeyBindings("fast"));
@@ -266,14 +233,9 @@ function defineVideoController() {
if (tc.settings.forceLastSavedSpeed) speedToSet = tc.settings.lastSpeed; if (tc.settings.forceLastSavedSpeed) speedToSet = tc.settings.lastSpeed;
if (Math.abs(video.playbackRate - speedToSet) > 0.001) { if (Math.abs(video.playbackRate - speedToSet) > 0.001) {
log(
`Media event '${event.type}': rate ${video.playbackRate.toFixed(2)} vs target ${speedToSet.toFixed(2)}. Setting.`,
4
);
setSpeed(video, speedToSet, false); // MODIFIED: isInitialSet is false setSpeed(video, speedToSet, false); // MODIFIED: isInitialSet is false
} }
// ADDED: Manage nudge
// ADDED: Manage nudge based on event type
if (event.type === "play") video.vsc.startSubtitleNudge(); if (event.type === "play") video.vsc.startSubtitleNudge();
else if (event.type === "pause" || event.type === "ended") else if (event.type === "pause" || event.type === "ended")
video.vsc.stopSubtitleNudge(); video.vsc.stopSubtitleNudge();
@@ -286,32 +248,31 @@ function defineVideoController() {
target.addEventListener( target.addEventListener(
"pause", "pause",
(this.handlePause = mediaEventAction.bind(this)) (this.handlePause = mediaEventAction.bind(this))
); // ADDED for nudge ); // ADDED
target.addEventListener( target.addEventListener(
"ended", "ended",
(this.handleEnded = mediaEventAction.bind(this)) (this.handleEnded = mediaEventAction.bind(this))
); // ADDED for nudge ); // ADDED
target.addEventListener( target.addEventListener(
"seeked", "seeked",
(this.handleSeek = mediaEventAction.bind(this)) (this.handleSeek = mediaEventAction.bind(this))
); );
var srcObserver = new MutationObserver((mutations) => { var srcObserver = new MutationObserver((mutations) => {
// Original srcObserver
mutations.forEach((mutation) => { mutations.forEach((mutation) => {
if ( if (
mutation.type === "attributes" && mutation.type === "attributes" &&
(mutation.attributeName === "src" || (mutation.attributeName === "src" ||
mutation.attributeName === "currentSrc") mutation.attributeName === "currentSrc")
) { ) {
if (!this.div) return; // MODIFIED: Check if div exists if (!this.div) return;
log(`Src changed to: ${mutation.target.currentSrc || "empty"}`, 4); this.stopSubtitleNudge(); // ADDED
this.stopSubtitleNudge(); // ADDED: Stop nudge for old src if (!mutation.target.src && !mutation.target.currentSrc)
if (!mutation.target.src && !mutation.target.currentSrc) {
this.div.classList.add("vsc-nosource"); this.div.classList.add("vsc-nosource");
} else { else {
this.div.classList.remove("vsc-nosource"); this.div.classList.remove("vsc-nosource");
let newSrcSpeed = tc.settings.speeds[mutation.target.currentSrc]; // MODIFIED: Follow original logic let newSrcSpeed = tc.settings.speeds[mutation.target.currentSrc];
if (!tc.settings.rememberSpeed) { if (!tc.settings.rememberSpeed) {
if (!newSrcSpeed) newSrcSpeed = 1.0; if (!newSrcSpeed) newSrcSpeed = 1.0;
} else { } else {
@@ -319,7 +280,6 @@ function defineVideoController() {
} }
if (tc.settings.forceLastSavedSpeed) if (tc.settings.forceLastSavedSpeed)
newSrcSpeed = tc.settings.lastSpeed; newSrcSpeed = tc.settings.lastSpeed;
setSpeed(mutation.target, newSrcSpeed, true); // MODIFIED: isInitialSet = true setSpeed(mutation.target, newSrcSpeed, true); // MODIFIED: isInitialSet = true
if (!mutation.target.paused && mutation.target.playbackRate !== 1.0) if (!mutation.target.paused && mutation.target.playbackRate !== 1.0)
this.startSubtitleNudge(); // ADDED this.startSubtitleNudge(); // ADDED
@@ -329,14 +289,13 @@ function defineVideoController() {
}); });
srcObserver.observe(target, { attributeFilter: ["src", "currentSrc"] }); srcObserver.observe(target, { attributeFilter: ["src", "currentSrc"] });
// ADDED: Initial nudge check if (!target.paused && target.playbackRate !== 1.0)
if (!target.paused && target.playbackRate !== 1.0) { this.startSubtitleNudge(); // ADDED
this.startSubtitleNudge();
}
}; };
// --- Nudge Methods (ADDED) --- // --- Nudge Methods (ADDED) ---
tc.videoController.prototype.startSubtitleNudge = function () { tc.videoController.prototype.startSubtitleNudge = function () {
if (!location.hostname.includes("youtube.com")) return; // ADDED: Nudge only on YouTube
if ( if (
!tc.settings.enableSubtitleNudge || !tc.settings.enableSubtitleNudge ||
this.nudgeIntervalId !== null || this.nudgeIntervalId !== null ||
@@ -347,10 +306,7 @@ function defineVideoController() {
this.stopSubtitleNudge(); this.stopSubtitleNudge();
return; return;
} }
log( log(`Nudge: Starting interval: ${tc.settings.subtitleNudgeInterval}ms.`, 5);
`Nudge: Starting for ${this.video.currentSrc || "video"} (Rate: ${this.video.playbackRate.toFixed(2)}) interval: ${tc.settings.subtitleNudgeInterval}ms.`,
5
);
this.nudgeIntervalId = setInterval(() => { this.nudgeIntervalId = setInterval(() => {
if ( if (
!this.video || !this.video ||
@@ -363,10 +319,8 @@ function defineVideoController() {
} }
const currentRate = this.video.playbackRate; const currentRate = this.video.playbackRate;
const nudgeAmount = tc.settings.subtitleNudgeAmount; const nudgeAmount = tc.settings.subtitleNudgeAmount;
tc.isNudging = true; tc.isNudging = true;
this.video.playbackRate = currentRate + nudgeAmount; this.video.playbackRate = currentRate + nudgeAmount;
requestAnimationFrame(() => { requestAnimationFrame(() => {
if ( if (
this.video && this.video &&
@@ -379,49 +333,43 @@ function defineVideoController() {
}); });
}, tc.settings.subtitleNudgeInterval); }, tc.settings.subtitleNudgeInterval);
}; };
tc.videoController.prototype.stopSubtitleNudge = function () { tc.videoController.prototype.stopSubtitleNudge = function () {
if (this.nudgeIntervalId !== null) { if (this.nudgeIntervalId !== null) {
log( log(`Nudge: Stopping.`, 5);
`Nudge: Stopping for ${this.video ? this.video.currentSrc || "video" : "detached video"}`,
5
);
clearInterval(this.nudgeIntervalId); clearInterval(this.nudgeIntervalId);
this.nudgeIntervalId = null; this.nudgeIntervalId = null;
} }
}; };
// --- End Nudge Methods ---
tc.videoController.prototype.remove = function () { tc.videoController.prototype.remove = function () {
// Original remove
this.stopSubtitleNudge(); // ADDED this.stopSubtitleNudge(); // ADDED
// Original remove logic:
if (this.div && this.div.parentNode) this.div.remove(); if (this.div && this.div.parentNode) this.div.remove();
if (this.video) { if (this.video) {
this.video.removeEventListener("play", this.handlePlay); this.video.removeEventListener("play", this.handlePlay);
this.video.removeEventListener("pause", this.handlePause); // ADDED this.video.removeEventListener("pause", this.handlePause); // ADDED
this.video.removeEventListener("ended", this.handleEnded); // ADDED this.video.removeEventListener("ended", this.handleEnded); // ADDED
this.video.removeEventListener("seeked", this.handleSeek); // MODIFIED: was "seek" in original provided code this.video.removeEventListener("seeked", this.handleSeek); // Original had "seek"
delete this.video.vsc; delete this.video.vsc;
} }
let idx = tc.mediaElements.indexOf(this.video); let idx = tc.mediaElements.indexOf(this.video);
if (idx !== -1) tc.mediaElements.splice(idx, 1); if (idx !== -1) tc.mediaElements.splice(idx, 1);
}; };
// Original initializeControls
tc.videoController.prototype.initializeControls = function () { tc.videoController.prototype.initializeControls = function () {
// Original initializeControls
log("initializeControls Begin", 5); log("initializeControls Begin", 5);
const doc = this.video.ownerDocument; const doc = this.video.ownerDocument;
const speedForUI = this.video.playbackRate.toFixed(2); const speedForUI = this.video.playbackRate.toFixed(2);
var top = Math.max(this.video.offsetTop, 0) + "px", var top = Math.max(this.video.offsetTop, 0) + "px",
left = Math.max(this.video.offsetLeft, 0) + "px"; left = Math.max(this.video.offsetLeft, 0) + "px";
log("Speed variable for UI set to: " + speedForUI, 5);
var wrapper = doc.createElement("div"); var wrapper = doc.createElement("div");
wrapper.classList.add("vsc-controller"); wrapper.classList.add("vsc-controller");
if (!this.video.src && !this.video.currentSrc) if (!this.video.src && !this.video.currentSrc)
wrapper.classList.add("vsc-nosource"); wrapper.classList.add("vsc-nosource");
if (tc.settings.startHidden) wrapper.classList.add("vsc-hidden"); if (tc.settings.startHidden) wrapper.classList.add("vsc-hidden");
var shadow = wrapper.attachShadow({ mode: "open" }); var shadow = wrapper.attachShadow({ mode: "open" });
var shadowTemplate = ` shadow.innerHTML = `
<style> @import "${chrome.runtime.getURL("shadow.css")}"; </style> <style> @import "${chrome.runtime.getURL("shadow.css")}"; </style>
<div id="controller" style="top:${top}; left:${left}; opacity:${tc.settings.controllerOpacity}"> <div id="controller" style="top:${top}; left:${left}; opacity:${tc.settings.controllerOpacity}">
<span data-action="drag" class="draggable">${speedForUI}</span> <span data-action="drag" class="draggable">${speedForUI}</span>
@@ -433,7 +381,6 @@ function defineVideoController() {
<button data-action="display" class="hideButton">×</button> <button data-action="display" class="hideButton">×</button>
</span> </span>
</div>`; </div>`;
shadow.innerHTML = shadowTemplate;
this.speedIndicator = shadow.querySelector(".draggable"); // MODIFIED: Original was "span" this.speedIndicator = shadow.querySelector(".draggable"); // MODIFIED: Original was "span"
// MODIFIED: Pass this.video as 4th arg to runAction // MODIFIED: Pass this.video as 4th arg to runAction
@@ -450,8 +397,8 @@ function defineVideoController() {
}, },
true true
); );
shadow.querySelectorAll("button").forEach((button) => { shadow.querySelectorAll("button").forEach((btn) =>
button.addEventListener( btn.addEventListener(
"click", "click",
(e) => { (e) => {
runAction( runAction(
@@ -463,8 +410,8 @@ function defineVideoController() {
e.stopPropagation(); e.stopPropagation();
}, },
true true
); )
}); );
shadow shadow
.querySelector("#controller") .querySelector("#controller")
.addEventListener("click", (e) => e.stopPropagation(), false); .addEventListener("click", (e) => e.stopPropagation(), false);
@@ -473,8 +420,8 @@ function defineVideoController() {
.addEventListener("mousedown", (e) => e.stopPropagation(), false); .addEventListener("mousedown", (e) => e.stopPropagation(), false);
var fragment = doc.createDocumentFragment(); var fragment = doc.createDocumentFragment();
fragment.appendChild(wrapper); fragment.appendChild(wrapper);
const parentEl = this.parent || this.video.parentElement; const pEl = this.parent || this.video.parentElement;
if (!parentEl || !parentEl.parentNode) { if (!pEl || !pEl.parentNode) {
doc.body.appendChild(fragment); doc.body.appendChild(fragment);
return wrapper; return wrapper;
} }
@@ -483,38 +430,35 @@ function defineVideoController() {
case location.hostname == "www.amazon.com": case location.hostname == "www.amazon.com":
case location.hostname == "www.reddit.com": case location.hostname == "www.reddit.com":
case /hbogo\./.test(location.hostname): case /hbogo\./.test(location.hostname):
parentEl.parentElement.insertBefore(fragment, parentEl); pEl.parentElement.insertBefore(fragment, pEl);
break; break;
case location.hostname == "www.facebook.com": case location.hostname == "www.facebook.com":
let p = let p =
parentEl.parentElement.parentElement.parentElement.parentElement pEl.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement.parentElement; .parentElement.parentElement.parentElement;
if (p && p.firstChild) p.insertBefore(fragment, p.firstChild); if (p && p.firstChild) p.insertBefore(fragment, p.firstChild);
else if (parentEl.firstChild) else if (pEl.firstChild) pEl.insertBefore(fragment, pEl.firstChild);
parentEl.insertBefore(fragment, parentEl.firstChild); else pEl.appendChild(fragment);
else parentEl.appendChild(fragment);
break; break;
case location.hostname == "tv.apple.com": case location.hostname == "tv.apple.com":
const appleRoot = parentEl.getRootNode(); const r = pEl.getRootNode();
const scrim = const s = r && r.querySelector ? r.querySelector(".scrim") : null;
appleRoot && appleRoot.querySelector if (s) s.prepend(fragment);
? appleRoot.querySelector(".scrim") else pEl.insertBefore(fragment, pEl.firstChild);
: null;
if (scrim) scrim.prepend(fragment);
else parentEl.insertBefore(fragment, parentEl.firstChild);
break; break;
default: default:
parentEl.insertBefore(fragment, parentEl.firstChild); pEl.insertBefore(fragment, pEl.firstChild);
} }
return wrapper; return wrapper;
}; };
} }
function escapeStringRegExp(str) { function escapeStringRegExp(str) {
const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; const m = /[|\\{}()[\]^$+*?.]/g;
return str.replace(matchOperatorsRe, "\\$&"); return str.replace(m, "\\$&");
} } // Original
function isBlacklisted() { function isBlacklisted() {
/* ... same original logic (with robust regex) ... */
let blacklisted = false; let blacklisted = false;
const blacklistLines = tc.settings.blacklist const blacklistLines = tc.settings.blacklist
? tc.settings.blacklist.split("\n") ? tc.settings.blacklist.split("\n")
@@ -539,18 +483,18 @@ function isBlacklisted() {
return blacklisted; return blacklisted;
} }
var coolDown = false; var coolDown = false; // Original coolDown
function refreshCoolDown() { function refreshCoolDown() {
// Original refreshCoolDown
log("Begin refreshCoolDown", 5); log("Begin refreshCoolDown", 5);
if (coolDown) clearTimeout(coolDown); if (coolDown) clearTimeout(coolDown);
coolDown = setTimeout(function () { coolDown = setTimeout(function () {
coolDown = false; coolDown = false;
}, 1000); }, 1000);
// log("End refreshCoolDown", 6); // Original log level was 5
} }
function setupListener() { function setupListener() {
if (document.vscRateListenerAttached) return; // MODIFIED: Ensure flag check if (document.vscRateListenerAttached) return; // Original flag was vscRateChangeListenerAttached
// MODIFIED: fromUserInput parameter added // MODIFIED: fromUserInput parameter added
function updateSpeedFromEvent(video, fromUserInput = false) { function updateSpeedFromEvent(video, fromUserInput = false) {
@@ -565,12 +509,12 @@ function setupListener() {
tc.settings.speeds[video.currentSrc || "unknown_src"] = speed; tc.settings.speeds[video.currentSrc || "unknown_src"] = speed;
tc.settings.lastSpeed = speed; tc.settings.lastSpeed = speed;
chrome.storage.sync.set({ lastSpeed: speed }, () => { chrome.storage.sync.set({ lastSpeed: speed }, () => {
if (chrome.runtime.lastError) /* error handling if needed */
log(`Error saving lastSpeed: ${chrome.runtime.lastError.message}`, 2);
}); });
// MODIFIED: Only "blink" (show controller) if change was from user input // MODIFIED: Only "blink" (show controller) if change was from user input
if (fromUserInput) { if (fromUserInput) {
// The original runAction("blink", null, null) implies value is taken from keybinding or default
runAction("blink", getKeyBindings("blink", "value") || 1000, null, video); runAction("blink", getKeyBindings("blink", "value") || 1000, null, video);
} }
@@ -586,7 +530,6 @@ function setupListener() {
function (event) { function (event) {
// ADDED: Check tc.isNudging at the very start // ADDED: Check tc.isNudging at the very start
if (tc.isNudging) { if (tc.isNudging) {
// log("Ratechange event during nudge, VSC UI/state update skipped. Allowing propagation for YT.", 6);
return; return;
} }
@@ -602,7 +545,7 @@ function setupListener() {
return; return;
const eventOrigin = event.detail && event.detail.origin; const eventOrigin = event.detail && event.detail.origin;
let isFromUserInputForBlink = false; // MODIFIED: Flag to control blink let isFromUserInputForBlink = false;
if (tc.settings.forceLastSavedSpeed) { if (tc.settings.forceLastSavedSpeed) {
if (eventOrigin === "videoSpeed") { if (eventOrigin === "videoSpeed") {
@@ -615,100 +558,60 @@ function setupListener() {
video.playbackRate = detailSpeedNum; video.playbackRate = detailSpeedNum;
} }
} }
isFromUserInputForBlink = event.detail.fromUserInput !== false; // Respect passed flag // Use fromUserInput from event.detail if present (set by setSpeed)
isFromUserInputForBlink = event.detail.fromUserInput !== false;
updateSpeedFromEvent(video, isFromUserInputForBlink); updateSpeedFromEvent(video, isFromUserInputForBlink);
event.stopImmediatePropagation(); event.stopImmediatePropagation();
} else { } else {
if (Math.abs(video.playbackRate - tc.settings.lastSpeed) > 0.001) { if (Math.abs(video.playbackRate - tc.settings.lastSpeed) > 0.001) {
log(
`Ratechange (Force ON): Discrepancy. Video rate: ${video.playbackRate.toFixed(2)}, VSC wants: ${tc.settings.lastSpeed.toFixed(2)}. Forcing.`,
3
);
video.playbackRate = tc.settings.lastSpeed; video.playbackRate = tc.settings.lastSpeed;
event.stopImmediatePropagation(); event.stopImmediatePropagation();
// The next ratechange will be from VSC forcing, consider that not direct user input for blink
// updateSpeedFromEvent will be called by that next event.
} else { } else {
updateSpeedFromEvent(video, false); // Not user input, just confirming forced speed updateSpeedFromEvent(video, false);
} }
} }
} else { } else {
// forceLastSavedSpeed is OFF // forceLastSavedSpeed is OFF
// Determine if it was a VSC-initiated user action (like S/D keys) // If setSpeed was called (which sets video.vscIsDirectSetByVSC)
// The `setSpeed` function, when called by `runAction`, doesn't add a special origin detail // then it's a user-driven change (or initial set).
// when forceLastSavedSpeed is off. So, a native ratechange event fires. isFromUserInputForBlink = video.vscIsDirectSetByVSC === true;
// We assume if forceLastSavedSpeed is off, any rate change processed here if (video.hasOwnProperty("vscIsDirectSetByVSC"))
// that isn't a nudge IS significant enough to update UI state. delete video.vscIsDirectSetByVSC; // Consume flag
// The "blink" should happen if tc.settings.lastSpeed *changed* due to this event,
// implying it wasn't just a confirmation of existing speed. updateSpeedFromEvent(video, isFromUserInputForBlink);
const oldLastSpeed = tc.settings.lastSpeed; // DO NOT stop propagation when forceLastSavedSpeed is OFF
updateSpeedFromEvent(video, false); // Initially assume not user-driven for blink
if (
Math.abs(oldLastSpeed - tc.settings.lastSpeed) > 0.001 &&
oldLastSpeed !== 1.0 &&
tc.settings.lastSpeed !== 1.0
) {
// If lastSpeed actually changed due to this event, it was likely a user action via VSC
// or a significant external change. Trigger blink.
// Exception: don't blink if going to/from 1.0x as that's often a reset.
// This logic is imperfect for determining "user input" when not forcing.
// A cleaner way would be if setSpeed could flag the *next* native event.
// For now, this is a heuristic.
if (!tc.isNudging) {
// Double check not a nudge, though already filtered
runAction(
"blink",
getKeyBindings("blink", "value") || 1000,
null,
video
);
}
}
} }
}, },
true true
); );
document.vscRateChangeListenerAttached = true; // MODIFIED: Ensure flag is set document.vscRateListenerAttached = true; // Original flag name
} }
// MODIFIED: More robust initialization flow with unique flags // Original initializeWhenReady and initializeNow structure, with unique flags for re-entrancy checks
var vscInitializedDocuments = new Set(); var vscInitializedDocuments = new Set();
function initializeWhenReady(doc) { function initializeWhenReady(doc) {
if (doc.vscInitWhenReadyCalledFullUniqueFlag && doc.readyState !== "loading") if (doc.vscInitWhenReadyUniqueFlag1 && doc.readyState !== "loading") return;
return; doc.vscInitWhenReadyUniqueFlag1 = true;
doc.vscInitWhenReadyCalledFullUniqueFlag = true; if (isBlacklisted()) return;
if (isBlacklisted()) {
return;
}
log( log(
`initializeWhenReady for: ${doc.location ? doc.location.href : "iframe"}. ReadyState: ${doc.readyState}`, `initializeWhenReady for: ${doc.location ? doc.location.href : "iframe"}. RS: ${doc.readyState}`,
5 5
); );
if (doc === window.document && !window.vscPageLoadListenerUniqueFlag1) {
if (doc === window.document && !window.vscPageLoadListenerFullUniqueFlag) {
window.addEventListener("load", () => initializeNow(window.document), { window.addEventListener("load", () => initializeNow(window.document), {
once: true once: true
}); });
window.vscPageLoadListenerFullUniqueFlag = true; window.vscPageLoadListenerUniqueFlag1 = true;
} }
if (doc.readyState === "complete") { if (doc.readyState === "complete") initializeNow(doc);
initializeNow(doc); else if (!doc.vscReadyStateListenerUniqueFlag1) {
} else { doc.addEventListener("readystatechange", function onRSChangeUnique1() {
if (!doc.vscReadyStateListenerFullUniqueFlag) { if (doc.readyState === "complete") {
doc.addEventListener( doc.removeEventListener("readystatechange", onRSChangeUnique1);
"readystatechange", initializeNow(doc);
function onRSChange_VSC_Final_Unique_CB() { }
if (doc.readyState === "complete") { });
doc.removeEventListener( doc.vscReadyStateListenerUniqueFlag1 = true;
"readystatechange",
onRSChange_VSC_Final_Unique_CB
);
initializeNow(doc);
}
}
);
doc.vscReadyStateListenerFullUniqueFlag = true;
}
} }
} }
function inIframe() { function inIframe() {
@@ -719,6 +622,7 @@ function inIframe() {
} }
} }
function getShadow(parent) { function getShadow(parent) {
/* ... original logic ... */
let result = []; let result = [];
function getChild(p) { function getChild(p) {
if (p.firstElementChild) { if (p.firstElementChild) {
@@ -742,7 +646,7 @@ function initializeNow(doc) {
4 4
); );
if (!tc.settings.enabled) { if (!tc.settings.enabled) {
log("VSC is disabled.", 4); log("VSC disabled.", 4);
return; return;
} }
if (!doc.body.classList.contains("vsc-initialized")) if (!doc.body.classList.contains("vsc-initialized"))
@@ -766,8 +670,8 @@ function initializeNow(doc) {
docsForKeydown.add(window.top.document); docsForKeydown.add(window.top.document);
} catch (e) {} } catch (e) {}
docsForKeydown.forEach((lDoc) => { docsForKeydown.forEach((lDoc) => {
if (!lDoc.vscKeydownListenerUniqueFlagB) { if (!lDoc.vscKeydownListenerUniqueFlagC) {
// Different flag // Unique flag name
lDoc.addEventListener( lDoc.addEventListener(
"keydown", "keydown",
function (event) { function (event) {
@@ -807,11 +711,11 @@ function initializeNow(doc) {
}, },
true true
); );
lDoc.vscKeydownListenerUniqueFlagB = true; lDoc.vscKeydownListenerUniqueFlagC = true;
} }
}); });
if (!doc.vscMutationObserverUniqueFlagB) { if (!doc.vscMutationObserverUniqueFlagC) {
// Different flag // Unique flag name
const obs = new MutationObserver((muts) => { const obs = new MutationObserver((muts) => {
if (typeof requestIdleCallback === "function") if (typeof requestIdleCallback === "function")
requestIdleCallback(() => processMutations(muts), { timeout: 1000 }); requestIdleCallback(() => processMutations(muts), { timeout: 1000 });
@@ -865,7 +769,7 @@ function initializeNow(doc) {
attributes: true, attributes: true,
attributeFilter: ["aria-hidden"] attributeFilter: ["aria-hidden"]
}); });
doc.vscMutationObserverUniqueFlagB = true; doc.vscMutationObserverUniqueFlagC = true;
} }
const q = tc.settings.audioBoolean ? "video,audio" : "video"; const q = tc.settings.audioBoolean ? "video,audio" : "video";
doc.querySelectorAll(q).forEach((vid) => { doc.querySelectorAll(q).forEach((vid) => {
@@ -879,7 +783,7 @@ function initializeNow(doc) {
vscInitializedDocuments.add(doc); vscInitializedDocuments.add(doc);
} }
// MODIFIED setSpeed to accept `isInitialCall` and use it for `fromUserInput` // MODIFIED setSpeed to accept `isInitialCall` and pass it for `fromUserInput` in custom event
function setSpeed(video, speed, isInitialCall = false) { function setSpeed(video, speed, isInitialCall = false) {
const numericSpeed = Number(speed); const numericSpeed = Number(speed);
if (isNaN(numericSpeed) || numericSpeed <= 0 || numericSpeed > 16) return; if (isNaN(numericSpeed) || numericSpeed <= 0 || numericSpeed > 16) return;
@@ -904,23 +808,23 @@ function setSpeed(video, speed, isInitialCall = false) {
); );
} else { } else {
if (Math.abs(video.playbackRate - numericSpeed) > 0.001) { if (Math.abs(video.playbackRate - numericSpeed) > 0.001) {
// Before changing rate, set a flag that this is VSC initiated for non-forced mode // ADDED: Set a temporary flag on the video element itself before changing playbackRate
video.vscIsSettingRate = !isInitialCall; // True if user action, false if initial // This helps the native ratechange event handler determine if VSC initiated this change.
if (!isInitialCall) {
video.vscIsDirectlySettingRate = true;
}
video.playbackRate = numericSpeed; video.playbackRate = numericSpeed;
// Flag will be cleared by ratechange listener after processing
} }
} }
if (!isInitialCall) refreshCoolDown(); if (!isInitialCall) refreshCoolDown(); // Original call
if (video.vsc) { if (video.vsc) {
if (numericSpeed === 1.0 || video.paused) video.vsc.stopSubtitleNudge(); if (numericSpeed === 1.0 || video.paused) video.vsc.stopSubtitleNudge();
else video.vsc.startSubtitleNudge(); else video.vsc.startSubtitleNudge();
} }
} }
// MODIFIED runAction to pass 4th arg `specificVideo` to some internal calls if needed. // MODIFIED runAction for specificVideo targeting and passing `isInitialCall=false` to setSpeed
// And pass `isInitialCall=false` to setSpeed calls.
function runAction(action, value, e, specificVideo = null) { function runAction(action, value, e, specificVideo = null) {
// ... (robust mediaTagsToProcess logic from previous correct version) ...
var mediaTagsToProcess = []; var mediaTagsToProcess = [];
if (specificVideo) mediaTagsToProcess = [specificVideo]; if (specificVideo) mediaTagsToProcess = [specificVideo];
else if (e && e.target) { else if (e && e.target) {
@@ -955,8 +859,9 @@ function runAction(action, value, e, specificVideo = null) {
e && e.target && e.target.getRootNode && e.target.getRootNode().host e && e.target && e.target.getRootNode && e.target.getRootNode().host
? e.target.getRootNode().host ? e.target.getRootNode().host
: null; : null;
var originalActionForResetContext = actionBeingProcessedForReset_ctx; // Use a local context var
actionBeingProcessedForReset_ctx = action; // Store current action for original resetSpeed context (local to this runAction call)
const currentActionContext = action;
mediaTagsToProcess.forEach(function (v) { mediaTagsToProcess.forEach(function (v) {
if (!v || !v.vsc || !v.vsc.div || !v.vsc.speedIndicator) return; if (!v || !v.vsc || !v.vsc.div || !v.vsc.speedIndicator) return;
@@ -969,6 +874,7 @@ function runAction(action, value, e, specificVideo = null) {
return; return;
if (action === "blink" && specificVideo && v !== specificVideo) return; if (action === "blink" && specificVideo && v !== specificVideo) return;
// MODIFIED: showController only for explicit user-driven actions, not "blink" itself
const userDrivenActionsThatShowController = [ const userDrivenActionsThatShowController = [
"rewind", "rewind",
"advance", "advance",
@@ -1008,12 +914,12 @@ function runAction(action, value, e, specificVideo = null) {
case "slower": case "slower":
setSpeed(v, Math.max(v.playbackRate - numValue, 0.07), false); setSpeed(v, Math.max(v.playbackRate - numValue, 0.07), false);
break; break;
// MODIFIED: Calls new resetSpeed directly // MODIFIED: Passing currentActionContext to original resetSpeed
case "reset": case "reset":
resetSpeedSimple(v, 1.0); resetSpeed(v, 1.0, currentActionContext);
break; break;
case "fast": case "fast":
resetSpeedSimple(v, numValue); resetSpeed(v, numValue, currentActionContext);
break; break;
case "display": case "display":
controllerDiv.classList.add("vsc-manual"); controllerDiv.classList.add("vsc-manual");
@@ -1060,45 +966,44 @@ function runAction(action, value, e, specificVideo = null) {
log(`Unknown action: ${action}`, 3); log(`Unknown action: ${action}`, 3);
} }
}); });
actionBeingProcessedForReset_ctx = originalActionForResetContext;
} }
var actionBeingProcessedForReset_ctx = null; // Context for original resetSpeed // Removed global actionBeingProcessedForReset_global
function pause(v) { function pause(v) {
if (v.paused) v.play().catch((e) => log(`Play err:${e.message}`, 2)); if (v.paused) v.play().catch((e) => log(`Play err:${e.message}`, 2));
else v.pause(); else v.pause();
} }
// MODIFIED: New simpler resetSpeed function // MODIFIED: Original resetSpeed function now takes currentActionContext
function resetSpeedSimple(v, targetActionSpeed) { function resetSpeed(v, target, currentActionContext = null) {
log( log(
`resetSpeedSimple: Video current: ${v.playbackRate.toFixed(2)}, Target for this action: ${targetActionSpeed.toFixed(2)}`, `resetSpeed (original): Video current: ${v.playbackRate.toFixed(2)}, Target: ${target.toFixed(2)}, Context: ${currentActionContext}`,
4 4
); );
const fastPreferredSpeed = getKeyBindings("fast", "value") || 1.8;
if (targetActionSpeed === 1.0) { if (Math.abs(v.playbackRate - target) < 0.01) {
// Action was "reset" (R key) // Using Math.abs for float comparison
if (Math.abs(v.playbackRate - 1.0) < 0.01) { // If current speed IS the target of THIS action
setSpeed(v, fastPreferredSpeed, false); if (v.playbackRate === (getKeyBindings("reset", "value") || 1.0)) {
if (target !== 1.0) {
setSpeed(v, 1.0, false);
} else {
// Target is 1.0 and current is 1.0 (or what reset key last stored) -> go to fast
setSpeed(v, getKeyBindings("fast", "value"), false);
}
} else { } else {
setSpeed(v, 1.0, false); // Current is target, but not what 'reset' binding value holds (e.g. G pressed, current is fast speed)
setSpeed(v, getKeyBindings("reset", "value") || 1.0, false);
} }
} else { } else {
// Action was "fast" (G key), targetActionSpeed is the preferred speed // Current speed is NOT the target of this action. Set to the target.
if (Math.abs(v.playbackRate - targetActionSpeed) < 0.01) { if (currentActionContext === "reset") {
setSpeed(v, 1.0, false); // Only do this for 'reset' action context
} else { setKeyBindings("reset", v.playbackRate); // Original call to store current rate
setSpeed(v, targetActionSpeed, false);
} }
setSpeed(v, target, false);
} }
} }
// Remove or comment out the old `resetSpeed` function that uses setKeyBindings and actionBeingProcessedForReset_global
/*
function resetSpeed(v, target) { // THIS IS THE OLD ONE TO BE REPLACED by resetSpeedSimple
// ... original complex logic ...
}
*/
function muted(v) { function muted(v) {
v.muted = !v.muted; v.muted = !v.muted;
@@ -1114,7 +1019,7 @@ function jumpToMark(v) {
else log("No mark.", 4); else log("No mark.", 4);
} }
function handleDrag(video, e) { function handleDrag(video, e) {
/* ... same original ... */ /* ... same original logic ... */
if (!video || !video.vsc || !video.vsc.div || !video.vsc.div.shadowRoot) if (!video || !video.vsc || !video.vsc.div || !video.vsc.div.shadowRoot)
return; return;
const controller = video.vsc.div; const controller = video.vsc.div;
@@ -1156,7 +1061,7 @@ function handleDrag(video, e) {
} }
var timer = null; var timer = null;
function showController(controller) { function showController(controller) {
/* ... same original ... */ /* ... same original logic ... */
if (!controller || typeof controller.classList === "undefined") return; if (!controller || typeof controller.classList === "undefined") return;
controller.classList.add("vcs-show"); controller.classList.add("vcs-show");
if (timer) clearTimeout(timer); if (timer) clearTimeout(timer);

View File

@@ -1,13 +1,13 @@
{ {
"name": "Video Speed Controller", "name": "Video Speed Controller",
"short_name": "videospeed", "short_name": "videospeed",
"version": "0.6.3.3", "version": "1.1.0",
"manifest_version": 2, "manifest_version": 2,
"description": "Speed up, slow down, advance and rewind HTML5 audio/video with shortcuts", "description": "Speed up, slow down, advance and rewind HTML5 audio/video with shortcuts",
"homepage_url": "https://github.com/codebicycle/videospeed", "homepage_url": "https://github.com/SoPat712/videospeed",
"browser_specific_settings": { "browser_specific_settings": {
"gecko": { "gecko": {
"id": "{7be2ba16-0f1e-4d93-9ebc-5164397477a9}" "id": "{ed860648-f54f-4dc9-9a0d-501aec4313f5}"
} }
}, },
"icons": { "icons": {

View File

@@ -1,4 +1,4 @@
<!DOCTYPE html> <!doctype html>
<html> <html>
<head> <head>
<title>Video Speed Controller: Options</title> <title>Video Speed Controller: Options</title>
@@ -148,8 +148,13 @@
<input id="rememberSpeed" type="checkbox" /> <input id="rememberSpeed" type="checkbox" />
</div> </div>
<div class="row"> <div class="row">
<label for="forceLastSavedSpeed">Force last saved speed<br /> <label for="forceLastSavedSpeed"
<em>Useful for video players that override the speeds set by VideoSpeed</em></label> >Force last saved speed<br />
<em
>Useful for video players that override the speeds set by
VideoSpeed</em
></label
>
<input id="forceLastSavedSpeed" type="checkbox" /> <input id="forceLastSavedSpeed" type="checkbox" />
</div> </div>
<div class="row"> <div class="row">
@@ -175,6 +180,47 @@
</div> </div>
</section> </section>
<section id="nudgeSettings">
<h3>Subtitle Nudge Settings (Experimental - YouTube Only)</h3>
<div class="row">
<label for="enableSubtitleNudge"
>Enable Subtitle Nudge <br /><em
>Periodically 'nudges' video speed by a tiny amount to help keep
subtitles in sync on some sites (e.g. YouTube).</em
>
</label>
<input id="enableSubtitleNudge" type="checkbox" />
</div>
<div class="row">
<label for="subtitleNudgeInterval"
>Nudge Interval (milliseconds) <br /><em
>How often to nudge (e.g., 25-1000). Smaller values are more
frequent. Default: 25.</em
>
</label>
<input
id="subtitleNudgeInterval"
type="text"
value=""
placeholder="25"
/>
</div>
<div class="row">
<label for="subtitleNudgeAmount"
>Nudge Amount (decimal) <br /><em
>How much to change speed by (e.g., 0.001). Very small values
recommended. Default: 0.001.</em
>
</label>
<input
id="subtitleNudgeAmount"
type="text"
value=""
placeholder="0.001"
/>
</div>
</section>
<button id="save">Save</button> <button id="save">Save</button>
<button id="restore">Restore Defaults</button> <button id="restore">Restore Defaults</button>
<button id="experimental">Show Experimental Features</button> <button id="experimental">Show Experimental Features</button>
@@ -186,12 +232,12 @@
<h4>Extension controls not appearing?</h4> <h4>Extension controls not appearing?</h4>
<p> <p>
This extension is only compatible with HTML5 audio and video. If you don't This extension is only compatible with HTML5 audio and video. If you
see the controls showing up, chances are you are viewing a Flash content. don't see the controls showing up, chances are you are viewing a Flash
If you want to confirm, try right-clicking on the content and inspect the content. If you want to confirm, try right-clicking on the content and
menu: if it mentions flash, then that's the issue. That said, <b>most sites inspect the menu: if it mentions flash, then that's the issue. That
will fallback to HTML5</b> if they detect that Flash is not available. You said, <b>most sites will fallback to HTML5</b> if they detect that Flash
can try manually disabling Flash from the browser. is not available. You can try manually disabling Flash from the browser.
</p> </p>
</div> </div>
</body> </body>

View File

@@ -22,13 +22,17 @@ var tcDefaults = {
twitter.com twitter.com
imgur.com imgur.com
teams.microsoft.com teams.microsoft.com
`.replace(regStrip, "") `.replace(regStrip, ""),
// ADDED: Nudge defaults
enableSubtitleNudge: true,
subtitleNudgeInterval: 25,
subtitleNudgeAmount: 0.001
}; };
var keyBindings = []; var keyBindings = []; // This is populated during save/restore
var keyCodeAliases = { var keyCodeAliases = {
0: "null", /* ... same as your original ... */ 0: "null",
null: "null", null: "null",
undefined: "null", undefined: "null",
32: "Space", 32: "Space",
@@ -74,85 +78,55 @@ var keyCodeAliases = {
220: "\\", 220: "\\",
221: "]", 221: "]",
222: "'", 222: "'",
59: ";", 59: ";",
61: "+", 61: "+",
173: "-", 173: "-"
}; };
function recordKeyPress(e) { function recordKeyPress(e) {
/* ... same as your original ... */
if ( if (
(e.keyCode >= 48 && e.keyCode <= 57) || // Numbers 0-9 (e.keyCode >= 48 && e.keyCode <= 57) ||
(e.keyCode >= 65 && e.keyCode <= 90) || // Letters A-Z (e.keyCode >= 65 && e.keyCode <= 90) ||
keyCodeAliases[e.keyCode] // Other character keys keyCodeAliases[e.keyCode]
) { ) {
e.target.value = e.target.value =
keyCodeAliases[e.keyCode] || String.fromCharCode(e.keyCode); keyCodeAliases[e.keyCode] || String.fromCharCode(e.keyCode);
e.target.keyCode = e.keyCode; e.target.keyCode = e.keyCode;
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
} else if (e.keyCode === 8) { } else if (e.keyCode === 8) {
// Clear input when backspace pressed
e.target.value = ""; e.target.value = "";
} else if (e.keyCode === 27) { } else if (e.keyCode === 27) {
// When esc clicked, clear input
e.target.value = "null"; e.target.value = "null";
e.target.keyCode = null; e.target.keyCode = null;
} }
} }
function inputFilterNumbersOnly(e) { function inputFilterNumbersOnly(e) {
/* ... same as your original ... */
var char = String.fromCharCode(e.keyCode); var char = String.fromCharCode(e.keyCode);
if (!/[\d\.]$/.test(char) || !/^\d+(\.\d*)?$/.test(e.target.value + char)) { if (!/[\d\.]$/.test(char) || !/^\d+(\.\d*)?$/.test(e.target.value + char)) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
} }
} }
function inputFocus(e) { function inputFocus(e) {
e.target.value = ""; /* ... same as your original ... */ e.target.value = "";
} }
function inputBlur(e) { function inputBlur(e) {
e.target.value = /* ... same as your original ... */ e.target.value =
keyCodeAliases[e.target.keyCode] || String.fromCharCode(e.target.keyCode); keyCodeAliases[e.target.keyCode] || String.fromCharCode(e.target.keyCode);
} }
// function updateShortcutInputText(inputId, keyCode) { /* ... same as your original ... */ } // Not directly used in provided options.js logic flow
function updateShortcutInputText(inputId, keyCode) {
document.getElementById(inputId).value =
keyCodeAliases[keyCode] || String.fromCharCode(keyCode);
document.getElementById(inputId).keyCode = keyCode;
}
function updateCustomShortcutInputText(inputItem, keyCode) { function updateCustomShortcutInputText(inputItem, keyCode) {
inputItem.value = keyCodeAliases[keyCode] || String.fromCharCode(keyCode); /* ... same as your original ... */ inputItem.value =
keyCodeAliases[keyCode] || String.fromCharCode(keyCode);
inputItem.keyCode = keyCode; inputItem.keyCode = keyCode;
} }
var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"]; // Original
// List of custom actions for which customValue should be disabled
var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"];
function add_shortcut() { function add_shortcut() {
var html = `<select class="customDo"> /* ... same as your original ... */
<option value="slower">Decrease speed</option> var html = `<select class="customDo"><option value="slower">Decrease speed</option><option value="faster">Increase speed</option><option value="rewind">Rewind</option><option value="advance">Advance</option><option value="reset">Reset speed</option><option value="fast">Preferred speed</option><option value="muted">Mute</option><option value="pause">Pause</option><option value="mark">Set marker</option><option value="jump">Jump to marker</option><option value="display">Show/hide controller</option></select><input class="customKey" type="text" placeholder="press a key"/><input class="customValue" type="text" placeholder="value (0.10)"/><select class="customForce"><option value="false">Do not disable website key bindings</option><option value="true">Disable website key bindings</option></select><button class="removeParent">X</button>`;
<option value="faster">Increase speed</option>
<option value="rewind">Rewind</option>
<option value="advance">Advance</option>
<option value="reset">Reset speed</option>
<option value="fast">Preferred speed</option>
<option value="muted">Mute</option>
<option value="pause">Pause</option>
<option value="mark">Set marker</option>
<option value="jump">Jump to marker</option>
<option value="display">Show/hide controller</option>
</select>
<input class="customKey" type="text" placeholder="press a key"/>
<input class="customValue" type="text" placeholder="value (0.10)"/>
<select class="customForce">
<option value="false">Do not disable website key bindings</option>
<option value="true">Disable website key bindings</option>
</select>
<button class="removeParent">X</button>`;
var div = document.createElement("div"); var div = document.createElement("div");
div.setAttribute("class", "row customs"); div.setAttribute("class", "row customs");
div.innerHTML = html; div.innerHTML = html;
@@ -162,14 +136,13 @@ function add_shortcut() {
customs_element.children[customs_element.childElementCount - 1] customs_element.children[customs_element.childElementCount - 1]
); );
} }
function createKeyBindings(item) { function createKeyBindings(item) {
/* ... same as your original ... */
const action = item.querySelector(".customDo").value; const action = item.querySelector(".customDo").value;
const key = item.querySelector(".customKey").keyCode; const key = item.querySelector(".customKey").keyCode;
const value = Number(item.querySelector(".customValue").value); const value = Number(item.querySelector(".customValue").value);
const force = item.querySelector(".customForce").value; const force = item.querySelector(".customForce").value;
const predefined = !!item.id; //item.id ? true : false; const predefined = !!item.id;
keyBindings.push({ keyBindings.push({
action: action, action: action,
key: key, key: key,
@@ -178,9 +151,8 @@ function createKeyBindings(item) {
predefined: predefined predefined: predefined
}); });
} }
// Validates settings before saving
function validate() { function validate() {
/* ... same as your original ... */
var valid = true; var valid = true;
var status = document.getElementById("status"); var status = document.getElementById("status");
document document
@@ -190,7 +162,7 @@ function validate() {
match = match.replace(regStrip, ""); match = match.replace(regStrip, "");
if (match.startsWith("/")) { if (match.startsWith("/")) {
try { try {
var regexp = new RegExp(match); new RegExp(match);
} catch (err) { } catch (err) {
status.textContent = status.textContent =
"Error: Invalid blacklist regex: " + match + ". Unable to save"; "Error: Invalid blacklist regex: " + match + ". Unable to save";
@@ -202,24 +174,45 @@ function validate() {
return valid; return valid;
} }
// Saves options to chrome.storage // MODIFIED: save_options to include nudge settings
function save_options() { function save_options() {
if (validate() === false) { if (validate() === false) return;
return;
} keyBindings = []; // Reset global keyBindings before populating from DOM
keyBindings = [];
Array.from(document.querySelectorAll(".customs")).forEach((item) => Array.from(document.querySelectorAll(".customs")).forEach((item) =>
createKeyBindings(item) createKeyBindings(item)
); // Remove added shortcuts );
var rememberSpeed = document.getElementById("rememberSpeed").checked; var s = {}; // Object to hold all settings to be saved
var forceLastSavedSpeed = document.getElementById("forceLastSavedSpeed").checked; s.rememberSpeed = document.getElementById("rememberSpeed").checked;
var audioBoolean = document.getElementById("audioBoolean").checked; s.forceLastSavedSpeed = document.getElementById(
var enabled = document.getElementById("enabled").checked; "forceLastSavedSpeed"
var startHidden = document.getElementById("startHidden").checked; ).checked;
var controllerOpacity = document.getElementById("controllerOpacity").value; s.audioBoolean = document.getElementById("audioBoolean").checked;
var blacklist = document.getElementById("blacklist").value; s.enabled = document.getElementById("enabled").checked;
s.startHidden = document.getElementById("startHidden").checked;
s.controllerOpacity = document.getElementById("controllerOpacity").value;
s.blacklist = document
.getElementById("blacklist")
.value.replace(regStrip, "");
s.keyBindings = keyBindings; // Use the populated global keyBindings
// ADDED: Save nudge settings
s.enableSubtitleNudge = document.getElementById(
"enableSubtitleNudge"
).checked;
s.subtitleNudgeInterval =
parseInt(document.getElementById("subtitleNudgeInterval").value, 10) ||
tcDefaults.subtitleNudgeInterval;
s.subtitleNudgeAmount =
parseFloat(document.getElementById("subtitleNudgeAmount").value) ||
tcDefaults.subtitleNudgeAmount;
// Basic validation for nudge interval and amount
if (s.subtitleNudgeInterval < 10) s.subtitleNudgeInterval = 10; // Min 10ms
if (s.subtitleNudgeAmount <= 0 || s.subtitleNudgeAmount > 0.1)
s.subtitleNudgeAmount = tcDefaults.subtitleNudgeAmount;
// Remove old flat settings (original logic)
chrome.storage.sync.remove([ chrome.storage.sync.remove([
"resetSpeed", "resetSpeed",
"speedStep", "speedStep",
@@ -233,33 +226,22 @@ function save_options() {
"advanceKeyCode", "advanceKeyCode",
"fastKeyCode" "fastKeyCode"
]); ]);
chrome.storage.sync.set(
{ chrome.storage.sync.set(s, function () {
rememberSpeed: rememberSpeed, var status = document.getElementById("status");
forceLastSavedSpeed: forceLastSavedSpeed, status.textContent = "Options saved";
audioBoolean: audioBoolean, setTimeout(function () {
enabled: enabled, status.textContent = "";
startHidden: startHidden, }, 1000);
controllerOpacity: controllerOpacity, });
keyBindings: keyBindings,
blacklist: blacklist.replace(regStrip, "")
},
function () {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.textContent = "Options saved";
setTimeout(function () {
status.textContent = "";
}, 1000);
}
);
} }
// Restores options from chrome.storage // MODIFIED: restore_options to include nudge settings
function restore_options() { function restore_options() {
chrome.storage.sync.get(tcDefaults, function (storage) { chrome.storage.sync.get(tcDefaults, function (storage) {
document.getElementById("rememberSpeed").checked = storage.rememberSpeed; document.getElementById("rememberSpeed").checked = storage.rememberSpeed;
document.getElementById("forceLastSavedSpeed").checked = storage.forceLastSavedSpeed; document.getElementById("forceLastSavedSpeed").checked =
storage.forceLastSavedSpeed;
document.getElementById("audioBoolean").checked = storage.audioBoolean; document.getElementById("audioBoolean").checked = storage.audioBoolean;
document.getElementById("enabled").checked = storage.enabled; document.getElementById("enabled").checked = storage.enabled;
document.getElementById("startHidden").checked = storage.startHidden; document.getElementById("startHidden").checked = storage.startHidden;
@@ -267,65 +249,86 @@ function restore_options() {
storage.controllerOpacity; storage.controllerOpacity;
document.getElementById("blacklist").value = storage.blacklist; document.getElementById("blacklist").value = storage.blacklist;
// ensure that there is a "display" binding for upgrades from versions that had it as a separate binding // ADDED: Restore nudge settings
document.getElementById("enableSubtitleNudge").checked =
storage.enableSubtitleNudge;
document.getElementById("subtitleNudgeInterval").value =
storage.subtitleNudgeInterval;
document.getElementById("subtitleNudgeAmount").value =
storage.subtitleNudgeAmount;
// Original key binding restoration logic
if (
!Array.isArray(storage.keyBindings) ||
storage.keyBindings.length === 0
) {
// If keyBindings missing or not an array, use defaults from tcDefaults
storage.keyBindings = tcDefaults.keyBindings;
}
if (storage.keyBindings.filter((x) => x.action == "display").length == 0) { if (storage.keyBindings.filter((x) => x.action == "display").length == 0) {
storage.keyBindings.push({ storage.keyBindings.push({
action: "display", action: "display",
value: 0, value: 0,
force: false, force: false,
predefined: true predefined: true,
key: storage.displayKeyCode || tcDefaults.displayKeyCode
}); });
} }
// Clear existing dynamic shortcuts before restoring (if any were added by mistake)
const dynamicShortcuts = document.querySelectorAll(".customs:not([id])");
dynamicShortcuts.forEach((sc) => sc.remove());
for (let i in storage.keyBindings) { for (let i in storage.keyBindings) {
var item = storage.keyBindings[i]; var item = storage.keyBindings[i];
if (item.predefined) { if (item.predefined) {
//do predefined ones because their value needed for overlay
// document.querySelector("#" + item["action"] + " .customDo").value = item["action"];
if (item["action"] == "display" && typeof item["key"] === "undefined") { if (item["action"] == "display" && typeof item["key"] === "undefined") {
item["key"] = storage.displayKeyCode || tcDefaults.displayKeyCode; // V item["key"] = storage.displayKeyCode || tcDefaults.displayKeyCode;
} }
if (customActionsNoValues.includes(item["action"])) {
if (customActionsNoValues.includes(item["action"])) const el = document.querySelector(
document.querySelector(
"#" + item["action"] + " .customValue" "#" + item["action"] + " .customValue"
).disabled = true; );
if (el) el.disabled = true;
updateCustomShortcutInputText( }
document.querySelector("#" + item["action"] + " .customKey"), const keyEl = document.querySelector(
item["key"] "#" + item["action"] + " .customKey"
); );
document.querySelector("#" + item["action"] + " .customValue").value = const valEl = document.querySelector(
item["value"]; "#" + item["action"] + " .customValue"
document.querySelector("#" + item["action"] + " .customForce").value = );
item["force"]; const forceEl = document.querySelector(
"#" + item["action"] + " .customForce"
);
if (keyEl) updateCustomShortcutInputText(keyEl, item["key"]);
if (valEl) valEl.value = item["value"];
if (forceEl) forceEl.value = String(item["force"]); // Ensure string for select value
} else { } else {
// new ones // Non-predefined, dynamically added shortcuts
add_shortcut(); add_shortcut();
const dom = document.querySelector(".customs:last-of-type"); const dom = document.querySelector(".customs:last-of-type"); // Gets the newly added one
dom.querySelector(".customDo").value = item["action"]; dom.querySelector(".customDo").value = item["action"];
if (customActionsNoValues.includes(item["action"])) {
if (customActionsNoValues.includes(item["action"]))
dom.querySelector(".customValue").disabled = true; dom.querySelector(".customValue").disabled = true;
}
updateCustomShortcutInputText( updateCustomShortcutInputText(
dom.querySelector(".customKey"), dom.querySelector(".customKey"),
item["key"] item["key"]
); );
dom.querySelector(".customValue").value = item["value"]; dom.querySelector(".customValue").value = item["value"];
dom.querySelector(".customForce").value = item["force"]; dom.querySelector(".customForce").value = String(item["force"]);
} }
} }
}); });
} }
function restore_defaults() { function restore_defaults() {
/* ... same as your original, tcDefaults now includes nudge defaults ... */
// Remove all dynamically added shortcuts first
document.querySelectorAll(".customs:not([id])").forEach((el) => el.remove());
// Then set defaults and restore options, which will re-add predefined ones correctly
chrome.storage.sync.set(tcDefaults, function () { chrome.storage.sync.set(tcDefaults, function () {
restore_options(); restore_options(); // This will populate based on tcDefaults
document
.querySelectorAll(".removeParent")
.forEach((button) => button.click()); // Remove added shortcuts
// Update status to let user know options were saved.
var status = document.getElementById("status"); var status = document.getElementById("status");
status.textContent = "Default options restored"; status.textContent = "Default options restored";
setTimeout(function () { setTimeout(function () {
@@ -335,14 +338,15 @@ function restore_defaults() {
} }
function show_experimental() { function show_experimental() {
/* ... same as your original ... */
document document
.querySelectorAll(".customForce") .querySelectorAll(".customForce")
.forEach((item) => (item.style.display = "inline-block")); .forEach((item) => (item.style.display = "inline-block"));
} }
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
/* ... same as your original event listeners setup ... */
restore_options(); restore_options();
document.getElementById("save").addEventListener("click", save_options); document.getElementById("save").addEventListener("click", save_options);
document.getElementById("add").addEventListener("click", add_shortcut); document.getElementById("add").addEventListener("click", add_shortcut);
document document
@@ -353,34 +357,32 @@ document.addEventListener("DOMContentLoaded", function () {
.addEventListener("click", show_experimental); .addEventListener("click", show_experimental);
function eventCaller(event, className, funcName) { function eventCaller(event, className, funcName) {
if (!event.target.classList || !event.target.classList.contains(className)) { if (!event.target.classList || !event.target.classList.contains(className))
return; return;
}
funcName(event); funcName(event);
} }
document.addEventListener("keypress", (event) =>
document.addEventListener("keypress", (event) => { eventCaller(event, "customValue", inputFilterNumbersOnly)
eventCaller(event, "customValue", inputFilterNumbersOnly); );
}); document.addEventListener("focus", (event) =>
document.addEventListener("focus", (event) => { eventCaller(event, "customKey", inputFocus)
eventCaller(event, "customKey", inputFocus); );
}); document.addEventListener("blur", (event) =>
document.addEventListener("blur", (event) => { eventCaller(event, "customKey", inputBlur)
eventCaller(event, "customKey", inputBlur); );
}); document.addEventListener("keydown", (event) =>
document.addEventListener("keydown", (event) => { eventCaller(event, "customKey", recordKeyPress)
eventCaller(event, "customKey", recordKeyPress); );
}); document.addEventListener("click", (event) =>
document.addEventListener("click", (event) => {
eventCaller(event, "removeParent", function () { eventCaller(event, "removeParent", function () {
event.target.parentNode.remove(); event.target.parentNode.remove();
}); })
}); );
document.addEventListener("change", (event) => { document.addEventListener("change", (event) => {
eventCaller(event, "customDo", function () { eventCaller(event, "customDo", function () {
if (customActionsNoValues.includes(event.target.value)) { if (customActionsNoValues.includes(event.target.value)) {
event.target.nextElementSibling.nextElementSibling.disabled = true; event.target.nextElementSibling.nextElementSibling.disabled = true;
event.target.nextElementSibling.nextElementSibling.value = 0; event.target.nextElementSibling.nextElementSibling.value = 0; // Or "" if placeholder is preferred
} else { } else {
event.target.nextElementSibling.nextElementSibling.disabled = false; event.target.nextElementSibling.nextElementSibling.disabled = false;
} }

BIN
videospeed.xpi Normal file

Binary file not shown.