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

Subtitle Nudge Settings (Experimental - YouTube Only)

+
+ + +
+
+ + +
+
+ + +
+
+ @@ -186,12 +232,12 @@

Extension controls not appearing?

- This extension is only compatible with HTML5 audio and video. If you don't - see the controls showing up, chances are you are viewing a Flash content. - If you want to confirm, try right-clicking on the content and inspect the - menu: if it mentions flash, then that's the issue. That said, most sites - will fallback to HTML5 if they detect that Flash is not available. You - can try manually disabling Flash from the browser. + This extension is only compatible with HTML5 audio and video. If you + don't see the controls showing up, chances are you are viewing a Flash + content. If you want to confirm, try right-clicking on the content and + inspect the menu: if it mentions flash, then that's the issue. That + said, most sites will fallback to HTML5 if they detect that Flash + is not available. You can try manually disabling Flash from the browser.

diff --git a/options.js b/options.js index e1b8b95..d33d794 100644 --- a/options.js +++ b/options.js @@ -22,13 +22,17 @@ var tcDefaults = { twitter.com imgur.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 = { - 0: "null", + /* ... same as your original ... */ 0: "null", null: "null", undefined: "null", 32: "Space", @@ -74,85 +78,55 @@ var keyCodeAliases = { 220: "\\", 221: "]", 222: "'", - 59: ";", - 61: "+", - 173: "-", + 59: ";", + 61: "+", + 173: "-" }; function recordKeyPress(e) { + /* ... same as your original ... */ if ( - (e.keyCode >= 48 && e.keyCode <= 57) || // Numbers 0-9 - (e.keyCode >= 65 && e.keyCode <= 90) || // Letters A-Z - keyCodeAliases[e.keyCode] // Other character keys + (e.keyCode >= 48 && e.keyCode <= 57) || + (e.keyCode >= 65 && e.keyCode <= 90) || + keyCodeAliases[e.keyCode] ) { e.target.value = keyCodeAliases[e.keyCode] || String.fromCharCode(e.keyCode); e.target.keyCode = e.keyCode; - e.preventDefault(); e.stopPropagation(); } else if (e.keyCode === 8) { - // Clear input when backspace pressed e.target.value = ""; } else if (e.keyCode === 27) { - // When esc clicked, clear input e.target.value = "null"; e.target.keyCode = null; } } - function inputFilterNumbersOnly(e) { + /* ... same as your original ... */ var char = String.fromCharCode(e.keyCode); if (!/[\d\.]$/.test(char) || !/^\d+(\.\d*)?$/.test(e.target.value + char)) { e.preventDefault(); e.stopPropagation(); } } - function inputFocus(e) { - e.target.value = ""; + /* ... same as your original ... */ e.target.value = ""; } - function inputBlur(e) { - e.target.value = + /* ... same as your original ... */ e.target.value = keyCodeAliases[e.target.keyCode] || String.fromCharCode(e.target.keyCode); } - -function updateShortcutInputText(inputId, keyCode) { - document.getElementById(inputId).value = - keyCodeAliases[keyCode] || String.fromCharCode(keyCode); - document.getElementById(inputId).keyCode = keyCode; -} - +// function updateShortcutInputText(inputId, keyCode) { /* ... same as your original ... */ } // Not directly used in provided options.js logic flow 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; } - -// List of custom actions for which customValue should be disabled -var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"]; - +var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"]; // Original function add_shortcut() { - var html = ` - - - - `; + /* ... same as your original ... */ + var html = ``; var div = document.createElement("div"); div.setAttribute("class", "row customs"); div.innerHTML = html; @@ -162,14 +136,13 @@ function add_shortcut() { customs_element.children[customs_element.childElementCount - 1] ); } - function createKeyBindings(item) { + /* ... same as your original ... */ const action = item.querySelector(".customDo").value; const key = item.querySelector(".customKey").keyCode; const value = Number(item.querySelector(".customValue").value); const force = item.querySelector(".customForce").value; - const predefined = !!item.id; //item.id ? true : false; - + const predefined = !!item.id; keyBindings.push({ action: action, key: key, @@ -178,9 +151,8 @@ function createKeyBindings(item) { predefined: predefined }); } - -// Validates settings before saving function validate() { + /* ... same as your original ... */ var valid = true; var status = document.getElementById("status"); document @@ -190,7 +162,7 @@ function validate() { match = match.replace(regStrip, ""); if (match.startsWith("/")) { try { - var regexp = new RegExp(match); + new RegExp(match); } catch (err) { status.textContent = "Error: Invalid blacklist regex: " + match + ". Unable to save"; @@ -202,24 +174,45 @@ function validate() { return valid; } -// Saves options to chrome.storage +// MODIFIED: save_options to include nudge settings function save_options() { - if (validate() === false) { - return; - } - keyBindings = []; + if (validate() === false) return; + + keyBindings = []; // Reset global keyBindings before populating from DOM Array.from(document.querySelectorAll(".customs")).forEach((item) => createKeyBindings(item) - ); // Remove added shortcuts + ); - var rememberSpeed = document.getElementById("rememberSpeed").checked; - var forceLastSavedSpeed = document.getElementById("forceLastSavedSpeed").checked; - var audioBoolean = document.getElementById("audioBoolean").checked; - var enabled = document.getElementById("enabled").checked; - var startHidden = document.getElementById("startHidden").checked; - var controllerOpacity = document.getElementById("controllerOpacity").value; - var blacklist = document.getElementById("blacklist").value; + var s = {}; // Object to hold all settings to be saved + s.rememberSpeed = document.getElementById("rememberSpeed").checked; + s.forceLastSavedSpeed = document.getElementById( + "forceLastSavedSpeed" + ).checked; + s.audioBoolean = document.getElementById("audioBoolean").checked; + 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([ "resetSpeed", "speedStep", @@ -233,33 +226,22 @@ function save_options() { "advanceKeyCode", "fastKeyCode" ]); - chrome.storage.sync.set( - { - rememberSpeed: rememberSpeed, - forceLastSavedSpeed: forceLastSavedSpeed, - audioBoolean: audioBoolean, - enabled: enabled, - startHidden: startHidden, - 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); - } - ); + + chrome.storage.sync.set(s, function () { + 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() { chrome.storage.sync.get(tcDefaults, function (storage) { 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("enabled").checked = storage.enabled; document.getElementById("startHidden").checked = storage.startHidden; @@ -267,65 +249,86 @@ function restore_options() { storage.controllerOpacity; 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) { storage.keyBindings.push({ action: "display", value: 0, 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) { var item = storage.keyBindings[i]; 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") { - item["key"] = storage.displayKeyCode || tcDefaults.displayKeyCode; // V + item["key"] = storage.displayKeyCode || tcDefaults.displayKeyCode; } - - if (customActionsNoValues.includes(item["action"])) - document.querySelector( + if (customActionsNoValues.includes(item["action"])) { + const el = document.querySelector( "#" + item["action"] + " .customValue" - ).disabled = true; - - updateCustomShortcutInputText( - document.querySelector("#" + item["action"] + " .customKey"), - item["key"] + ); + if (el) el.disabled = true; + } + const keyEl = document.querySelector( + "#" + item["action"] + " .customKey" ); - document.querySelector("#" + item["action"] + " .customValue").value = - item["value"]; - document.querySelector("#" + item["action"] + " .customForce").value = - item["force"]; + const valEl = document.querySelector( + "#" + item["action"] + " .customValue" + ); + 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 { - // new ones + // Non-predefined, dynamically added shortcuts 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"]; - - if (customActionsNoValues.includes(item["action"])) + if (customActionsNoValues.includes(item["action"])) { dom.querySelector(".customValue").disabled = true; - + } updateCustomShortcutInputText( dom.querySelector(".customKey"), item["key"] ); dom.querySelector(".customValue").value = item["value"]; - dom.querySelector(".customForce").value = item["force"]; + dom.querySelector(".customForce").value = String(item["force"]); } } }); } 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 () { - restore_options(); - document - .querySelectorAll(".removeParent") - .forEach((button) => button.click()); // Remove added shortcuts - // Update status to let user know options were saved. + restore_options(); // This will populate based on tcDefaults var status = document.getElementById("status"); status.textContent = "Default options restored"; setTimeout(function () { @@ -335,14 +338,15 @@ function restore_defaults() { } function show_experimental() { + /* ... same as your original ... */ document .querySelectorAll(".customForce") .forEach((item) => (item.style.display = "inline-block")); } document.addEventListener("DOMContentLoaded", function () { + /* ... same as your original event listeners setup ... */ restore_options(); - document.getElementById("save").addEventListener("click", save_options); document.getElementById("add").addEventListener("click", add_shortcut); document @@ -353,34 +357,32 @@ document.addEventListener("DOMContentLoaded", function () { .addEventListener("click", show_experimental); function eventCaller(event, className, funcName) { - if (!event.target.classList || !event.target.classList.contains(className)) { + if (!event.target.classList || !event.target.classList.contains(className)) return; - } funcName(event); } - - document.addEventListener("keypress", (event) => { - eventCaller(event, "customValue", inputFilterNumbersOnly); - }); - document.addEventListener("focus", (event) => { - eventCaller(event, "customKey", inputFocus); - }); - document.addEventListener("blur", (event) => { - eventCaller(event, "customKey", inputBlur); - }); - document.addEventListener("keydown", (event) => { - eventCaller(event, "customKey", recordKeyPress); - }); - document.addEventListener("click", (event) => { + document.addEventListener("keypress", (event) => + eventCaller(event, "customValue", inputFilterNumbersOnly) + ); + document.addEventListener("focus", (event) => + eventCaller(event, "customKey", inputFocus) + ); + document.addEventListener("blur", (event) => + eventCaller(event, "customKey", inputBlur) + ); + document.addEventListener("keydown", (event) => + eventCaller(event, "customKey", recordKeyPress) + ); + document.addEventListener("click", (event) => eventCaller(event, "removeParent", function () { event.target.parentNode.remove(); - }); - }); + }) + ); document.addEventListener("change", (event) => { eventCaller(event, "customDo", function () { if (customActionsNoValues.includes(event.target.value)) { event.target.nextElementSibling.nextElementSibling.disabled = true; - event.target.nextElementSibling.nextElementSibling.value = 0; + event.target.nextElementSibling.nextElementSibling.value = 0; // Or "" if placeholder is preferred } else { event.target.nextElementSibling.nextElementSibling.disabled = false; } diff --git a/videospeed.xpi b/videospeed.xpi new file mode 100644 index 0000000..3a81ecb Binary files /dev/null and b/videospeed.xpi differ