mirror of
https://github.com/SoPat712/videospeed.git
synced 2026-04-23 05:12:37 -04:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
a37054efe1
|
|||
|
f5692e659c
|
|||
|
ef86a70ca5
|
|||
|
5009e83f62
|
|||
|
1f8cb4411e
|
|||
|
05a8adef80
|
@@ -2,5 +2,6 @@
|
||||
local
|
||||
|
||||
# IntelliJ IDEA
|
||||
*.xpi
|
||||
.idea/
|
||||
node_modules
|
||||
|
||||
@@ -63,14 +63,26 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"⚠️ Failed to remove {f}: {e}")
|
||||
|
||||
# Read current version from manifest.json
|
||||
current_dir = os.getcwd()
|
||||
manifest_path = os.path.join(current_dir, TARGET_FILE)
|
||||
current_version = "unknown"
|
||||
|
||||
if os.path.exists(manifest_path):
|
||||
with open(manifest_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
match = re.match(r'\s*"version":\s*"([^"]+)"', line)
|
||||
if match:
|
||||
current_version = match.group(1)
|
||||
break
|
||||
|
||||
print(f"📦 Current version: {current_version}")
|
||||
base_version = input("Enter the new base version (e.g., 2.0.1): ").strip()
|
||||
if not base_version:
|
||||
print("❌ No version entered. Exiting.")
|
||||
return
|
||||
|
||||
firefox_version = f"{base_version}.0"
|
||||
current_dir = os.getcwd()
|
||||
manifest_path = os.path.join(current_dir, TARGET_FILE)
|
||||
|
||||
# Step 1: Update manifest.json on disk to base_version
|
||||
if os.path.exists(manifest_path):
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.ytp-autohide .vcs-show {
|
||||
.ytp-autohide .vsc-show {
|
||||
visibility: visible;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
@@ -24,14 +24,491 @@ var tc = {
|
||||
`.replace(regStrip, ""),
|
||||
defaultLogLevel: 4,
|
||||
logLevel: 5, // Set to 5 to see your debug logs
|
||||
enableSubtitleNudge: true,
|
||||
subtitleNudgeInterval: 25,
|
||||
enableSubtitleNudge: true, // Enabled by default, but only activates on YouTube
|
||||
subtitleNudgeInterval: 100, // Reduced from 25ms to 100ms (10x/sec instead of 40x/sec)
|
||||
subtitleNudgeAmount: 0.001
|
||||
},
|
||||
mediaElements: [],
|
||||
isNudging: false
|
||||
};
|
||||
|
||||
var MIN_SPEED = 0.0625;
|
||||
var MAX_SPEED = 16;
|
||||
var vscObservedRoots = new WeakSet();
|
||||
var requestIdle =
|
||||
typeof window.requestIdleCallback === "function"
|
||||
? window.requestIdleCallback.bind(window)
|
||||
: function (callback, options) {
|
||||
return setTimeout(callback, (options && options.timeout) || 1);
|
||||
};
|
||||
|
||||
var keyCodeToEventKey = {
|
||||
32: " ",
|
||||
37: "ArrowLeft",
|
||||
38: "ArrowUp",
|
||||
39: "ArrowRight",
|
||||
40: "ArrowDown",
|
||||
96: "0",
|
||||
97: "1",
|
||||
98: "2",
|
||||
99: "3",
|
||||
100: "4",
|
||||
101: "5",
|
||||
102: "6",
|
||||
103: "7",
|
||||
104: "8",
|
||||
105: "9",
|
||||
106: "*",
|
||||
107: "+",
|
||||
109: "-",
|
||||
110: ".",
|
||||
111: "/",
|
||||
112: "F1",
|
||||
113: "F2",
|
||||
114: "F3",
|
||||
115: "F4",
|
||||
116: "F5",
|
||||
117: "F6",
|
||||
118: "F7",
|
||||
119: "F8",
|
||||
120: "F9",
|
||||
121: "F10",
|
||||
122: "F11",
|
||||
123: "F12",
|
||||
186: ";",
|
||||
188: "<",
|
||||
189: "-",
|
||||
187: "+",
|
||||
190: ">",
|
||||
191: "/",
|
||||
192: "~",
|
||||
219: "[",
|
||||
220: "\\",
|
||||
221: "]",
|
||||
222: "'",
|
||||
59: ";",
|
||||
61: "+",
|
||||
173: "-"
|
||||
};
|
||||
|
||||
function createDefaultBinding(action, key, keyCode, value) {
|
||||
return {
|
||||
action: action,
|
||||
key: key,
|
||||
keyCode: keyCode,
|
||||
value: value,
|
||||
force: false,
|
||||
predefined: true
|
||||
};
|
||||
}
|
||||
|
||||
function defaultKeyBindings(storage) {
|
||||
return [
|
||||
createDefaultBinding(
|
||||
"slower",
|
||||
"S",
|
||||
Number(storage.slowerKeyCode) || 83,
|
||||
Number(storage.speedStep) || 0.1
|
||||
),
|
||||
createDefaultBinding(
|
||||
"faster",
|
||||
"D",
|
||||
Number(storage.fasterKeyCode) || 68,
|
||||
Number(storage.speedStep) || 0.1
|
||||
),
|
||||
createDefaultBinding(
|
||||
"rewind",
|
||||
"Z",
|
||||
Number(storage.rewindKeyCode) || 90,
|
||||
Number(storage.rewindTime) || 10
|
||||
),
|
||||
createDefaultBinding(
|
||||
"advance",
|
||||
"X",
|
||||
Number(storage.advanceKeyCode) || 88,
|
||||
Number(storage.advanceTime) || 10
|
||||
),
|
||||
createDefaultBinding(
|
||||
"reset",
|
||||
"R",
|
||||
Number(storage.resetKeyCode) || 82,
|
||||
1.0
|
||||
),
|
||||
createDefaultBinding(
|
||||
"fast",
|
||||
"G",
|
||||
Number(storage.fastKeyCode) || 71,
|
||||
Number(storage.fastSpeed) || 1.8
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
function getLegacyKeyCode(binding) {
|
||||
if (!binding) return null;
|
||||
if (Number.isInteger(binding.keyCode)) return binding.keyCode;
|
||||
if (typeof binding.key === "number" && Number.isInteger(binding.key)) {
|
||||
return binding.key;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizeBindingKey(key) {
|
||||
if (typeof key !== "string" || key.length === 0) return null;
|
||||
if (key === "Spacebar") return " ";
|
||||
if (key === "Esc") return "Escape";
|
||||
if (key.length === 1 && /[a-z]/i.test(key)) return key.toUpperCase();
|
||||
return key;
|
||||
}
|
||||
|
||||
function legacyKeyCodeToBinding(keyCode) {
|
||||
if (!Number.isInteger(keyCode)) return null;
|
||||
var key = keyCodeToEventKey[keyCode];
|
||||
if (!key && keyCode >= 48 && keyCode <= 57) {
|
||||
key = String.fromCharCode(keyCode);
|
||||
}
|
||||
if (!key && keyCode >= 65 && keyCode <= 90) {
|
||||
key = String.fromCharCode(keyCode);
|
||||
}
|
||||
return {
|
||||
key: normalizeBindingKey(key),
|
||||
keyCode: keyCode,
|
||||
code: null,
|
||||
disabled: false
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeStoredBinding(binding, fallbackKeyCode) {
|
||||
var fallbackBinding = legacyKeyCodeToBinding(fallbackKeyCode);
|
||||
if (!binding) return fallbackBinding;
|
||||
|
||||
if (
|
||||
binding.disabled === true ||
|
||||
(binding.key === null &&
|
||||
binding.keyCode === null &&
|
||||
binding.code === null)
|
||||
) {
|
||||
return {
|
||||
action: binding.action,
|
||||
key: null,
|
||||
keyCode: null,
|
||||
code: null,
|
||||
disabled: true,
|
||||
value: Number(binding.value),
|
||||
force: String(binding.force) === "true" ? "true" : "false",
|
||||
predefined: Boolean(binding.predefined)
|
||||
};
|
||||
}
|
||||
|
||||
var normalized = {
|
||||
action: binding.action,
|
||||
key: null,
|
||||
keyCode: null,
|
||||
code:
|
||||
typeof binding.code === "string" && binding.code.length > 0
|
||||
? binding.code
|
||||
: null,
|
||||
disabled: false,
|
||||
value: Number(binding.value),
|
||||
force: String(binding.force) === "true" ? "true" : "false",
|
||||
predefined: Boolean(binding.predefined)
|
||||
};
|
||||
|
||||
if (typeof binding.key === "string") {
|
||||
normalized.key = normalizeBindingKey(binding.key);
|
||||
}
|
||||
|
||||
var legacyKeyCode = getLegacyKeyCode(binding);
|
||||
if (Number.isInteger(legacyKeyCode)) {
|
||||
var legacyBinding = legacyKeyCodeToBinding(legacyKeyCode);
|
||||
if (legacyBinding) {
|
||||
normalized.key = normalized.key || legacyBinding.key;
|
||||
normalized.keyCode = legacyKeyCode;
|
||||
}
|
||||
}
|
||||
|
||||
if (Number.isInteger(binding.keyCode)) {
|
||||
normalized.keyCode = binding.keyCode;
|
||||
}
|
||||
|
||||
if (!normalized.key && fallbackBinding) {
|
||||
normalized.key = fallbackBinding.key;
|
||||
if (normalized.keyCode === null) normalized.keyCode = fallbackBinding.keyCode;
|
||||
}
|
||||
|
||||
if (!normalized.key && !normalized.code && normalized.keyCode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function isValidSpeed(speed) {
|
||||
return !isNaN(speed) && speed >= MIN_SPEED && speed <= MAX_SPEED;
|
||||
}
|
||||
|
||||
function sanitizeSpeed(speed, fallback) {
|
||||
var numericSpeed = Number(speed);
|
||||
return isValidSpeed(numericSpeed) ? numericSpeed : fallback;
|
||||
}
|
||||
|
||||
function getVideoSourceKey(video) {
|
||||
return (video && (video.currentSrc || video.src)) || "unknown_src";
|
||||
}
|
||||
|
||||
function getControllerTargetSpeed(video) {
|
||||
if (!video || !video.vsc) return null;
|
||||
return isValidSpeed(video.vsc.targetSpeed) ? video.vsc.targetSpeed : null;
|
||||
}
|
||||
|
||||
function getRememberedSpeed(video) {
|
||||
var sourceKey = getVideoSourceKey(video);
|
||||
if (sourceKey !== "unknown_src") {
|
||||
var videoSpeed = tc.settings.speeds[sourceKey];
|
||||
if (isValidSpeed(videoSpeed)) return videoSpeed;
|
||||
}
|
||||
if (tc.settings.forceLastSavedSpeed && isValidSpeed(tc.settings.lastSpeed)) {
|
||||
return tc.settings.lastSpeed;
|
||||
}
|
||||
if (tc.settings.rememberSpeed && isValidSpeed(tc.settings.lastSpeed)) {
|
||||
return tc.settings.lastSpeed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getDesiredSpeed(video) {
|
||||
return getControllerTargetSpeed(video) || getRememberedSpeed(video) || 1.0;
|
||||
}
|
||||
|
||||
function resolveTargetSpeed(video) {
|
||||
return getDesiredSpeed(video);
|
||||
}
|
||||
|
||||
function extendSpeedRestoreWindow(video, duration) {
|
||||
if (!video || !video.vsc) return;
|
||||
|
||||
var restoreDuration = Number(duration) || 1500;
|
||||
var restoreUntil = Date.now() + restoreDuration;
|
||||
var currentUntil = Number(video.vsc.speedRestoreUntil) || 0;
|
||||
|
||||
video.vsc.speedRestoreUntil = Math.max(currentUntil, restoreUntil);
|
||||
}
|
||||
|
||||
function scheduleSpeedRestore(video, desiredSpeed, reason) {
|
||||
if (!video || !video.vsc || !isValidSpeed(desiredSpeed)) return;
|
||||
|
||||
if (video.vsc.restoreSpeedTimer) {
|
||||
clearTimeout(video.vsc.restoreSpeedTimer);
|
||||
}
|
||||
|
||||
video.vsc.restoreSpeedTimer = setTimeout(function () {
|
||||
if (!video.vsc) return;
|
||||
|
||||
if (Math.abs(video.playbackRate - desiredSpeed) > 0.01) {
|
||||
log(
|
||||
`Restoring playbackRate to ${desiredSpeed.toFixed(2)} after ${reason}`,
|
||||
4
|
||||
);
|
||||
setSpeed(video, desiredSpeed, false, false);
|
||||
}
|
||||
|
||||
if (video.vsc) {
|
||||
video.vsc.restoreSpeedTimer = null;
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
function rememberPendingRateChange(video, speed) {
|
||||
if (!video || !video.vsc || !isValidSpeed(speed)) return;
|
||||
|
||||
video.vsc.pendingRateChange = {
|
||||
speed: Number(speed),
|
||||
expiresAt: Date.now() + 1000
|
||||
};
|
||||
}
|
||||
|
||||
function takePendingRateChange(video, currentSpeed) {
|
||||
if (!video || !video.vsc || !video.vsc.pendingRateChange) return null;
|
||||
|
||||
var pendingRateChange = video.vsc.pendingRateChange;
|
||||
if (
|
||||
!isValidSpeed(pendingRateChange.speed) ||
|
||||
pendingRateChange.expiresAt <= Date.now()
|
||||
) {
|
||||
video.vsc.pendingRateChange = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Math.abs(Number(pendingRateChange.speed) - currentSpeed) > 0.01) {
|
||||
return null;
|
||||
}
|
||||
|
||||
video.vsc.pendingRateChange = null;
|
||||
return pendingRateChange;
|
||||
}
|
||||
|
||||
function matchesKeyBinding(binding, event) {
|
||||
if (!binding || binding.disabled) return false;
|
||||
|
||||
var normalizedEventKey = normalizeBindingKey(event.key);
|
||||
if (binding.key && normalizedEventKey) {
|
||||
return binding.key === normalizedEventKey;
|
||||
}
|
||||
|
||||
if (binding.code && event.code) {
|
||||
return binding.code === event.code;
|
||||
}
|
||||
|
||||
var legacyKeyCode = getLegacyKeyCode(binding);
|
||||
return Number.isInteger(legacyKeyCode) && legacyKeyCode === event.keyCode;
|
||||
}
|
||||
|
||||
function mediaSelector() {
|
||||
return tc.settings.audioBoolean ? "video,audio" : "video";
|
||||
}
|
||||
|
||||
function isMediaElement(node) {
|
||||
return (
|
||||
node &&
|
||||
node.nodeType === Node.ELEMENT_NODE &&
|
||||
(node.nodeName === "VIDEO" ||
|
||||
(node.nodeName === "AUDIO" && tc.settings.audioBoolean))
|
||||
);
|
||||
}
|
||||
|
||||
function hasUsableMediaSource(node) {
|
||||
if (!isMediaElement(node) || !node.isConnected) return false;
|
||||
if (node.currentSrc || node.src || node.srcObject) return true;
|
||||
if (typeof node.readyState === "number" && node.readyState > 0) return true;
|
||||
if (
|
||||
typeof node.networkState === "number" &&
|
||||
typeof HTMLMediaElement !== "undefined" &&
|
||||
(node.networkState === HTMLMediaElement.NETWORK_IDLE ||
|
||||
node.networkState === HTMLMediaElement.NETWORK_LOADING)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (node.querySelectorAll) {
|
||||
return Array.from(node.querySelectorAll("source[src]")).some(function (
|
||||
source
|
||||
) {
|
||||
var src = source.getAttribute("src");
|
||||
return typeof src === "string" && src.trim().length > 0;
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function ensureController(node, parent) {
|
||||
if (!isMediaElement(node) || node.vsc) return node && node.vsc;
|
||||
if (!hasUsableMediaSource(node)) {
|
||||
log(
|
||||
`Deferring controller creation for ${node.tagName}: no usable source yet`,
|
||||
5
|
||||
);
|
||||
return null;
|
||||
}
|
||||
log(
|
||||
`Creating controller for ${node.tagName}: ${node.src || node.currentSrc || "no src"}`,
|
||||
4
|
||||
);
|
||||
node.vsc = new tc.videoController(
|
||||
node,
|
||||
parent || node.parentElement || node.parentNode
|
||||
);
|
||||
return node.vsc;
|
||||
}
|
||||
|
||||
function removeController(node) {
|
||||
if (node && node.vsc) node.vsc.remove();
|
||||
}
|
||||
|
||||
function scanNodeForMedia(node, parent, added) {
|
||||
if (!node || typeof node === "function") return;
|
||||
|
||||
if (node.nodeType === Node.DOCUMENT_NODE) {
|
||||
scanNodeForMedia(node.body || node.documentElement, node.body, added);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
node.nodeType !== Node.ELEMENT_NODE &&
|
||||
node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
var ownerDocument = node.ownerDocument || document;
|
||||
if (!added && ownerDocument.body && ownerDocument.body.contains(node)) return;
|
||||
|
||||
if (isMediaElement(node)) {
|
||||
if (added) ensureController(node, parent);
|
||||
else removeController(node);
|
||||
}
|
||||
|
||||
if (node.children) {
|
||||
Array.from(node.children).forEach(function (child) {
|
||||
scanNodeForMedia(child, child.parentNode || parent, added);
|
||||
});
|
||||
}
|
||||
|
||||
if (node.shadowRoot) {
|
||||
observeRoot(node.shadowRoot);
|
||||
scanNodeForMedia(node.shadowRoot, node, added);
|
||||
}
|
||||
}
|
||||
|
||||
function getScanNodeForRoot(root) {
|
||||
if (!root) return null;
|
||||
if (root.nodeType === Node.DOCUMENT_NODE) {
|
||||
return root.body || root.documentElement;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
function scanRootForMedia(root) {
|
||||
var scanRoot = getScanNodeForRoot(root);
|
||||
if (!scanRoot) return;
|
||||
scanNodeForMedia(scanRoot, root.host || scanRoot.parentNode || scanRoot, true);
|
||||
if (root.nodeType === Node.DOCUMENT_NODE) {
|
||||
attachIframeListeners(root);
|
||||
}
|
||||
}
|
||||
|
||||
function observeRoot(root) {
|
||||
if (!root || vscObservedRoots.has(root)) return;
|
||||
vscObservedRoots.add(root);
|
||||
setupListener(root);
|
||||
attachMutationObserver(root);
|
||||
attachMediaDetectionListeners(root);
|
||||
scanRootForMedia(root);
|
||||
}
|
||||
|
||||
function patchAttachShadow() {
|
||||
if (
|
||||
window.vscAttachShadowPatched ||
|
||||
typeof Element === "undefined" ||
|
||||
typeof Element.prototype.attachShadow !== "function"
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
var originalAttachShadow = Element.prototype.attachShadow;
|
||||
Element.prototype.attachShadow = function () {
|
||||
var shadowRoot = originalAttachShadow.apply(this, arguments);
|
||||
try {
|
||||
if (shadowRoot) {
|
||||
observeRoot(shadowRoot);
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Unable to observe shadow root: ${error.message}`, 3);
|
||||
}
|
||||
return shadowRoot;
|
||||
};
|
||||
window.vscAttachShadowPatched = true;
|
||||
}
|
||||
|
||||
/* Log levels */
|
||||
function log(message, level) {
|
||||
verbosity = tc.settings.logLevel;
|
||||
@@ -50,51 +527,16 @@ function log(message, level) {
|
||||
}
|
||||
|
||||
chrome.storage.sync.get(tc.settings, function (storage) {
|
||||
// Original initialization from your code
|
||||
tc.settings.keyBindings = storage.keyBindings;
|
||||
if (storage.keyBindings.length == 0) {
|
||||
tc.settings.keyBindings.push({
|
||||
action: "slower",
|
||||
key: Number(storage.slowerKeyCode) || 83,
|
||||
value: Number(storage.speedStep) || 0.1,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push({
|
||||
action: "faster",
|
||||
key: Number(storage.fasterKeyCode) || 68,
|
||||
value: Number(storage.speedStep) || 0.1,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push({
|
||||
action: "rewind",
|
||||
key: Number(storage.rewindKeyCode) || 90,
|
||||
value: Number(storage.rewindTime) || 10,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push({
|
||||
action: "advance",
|
||||
key: Number(storage.advanceKeyCode) || 88,
|
||||
value: Number(storage.advanceTime) || 10,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push({
|
||||
action: "reset",
|
||||
key: Number(storage.resetKeyCode) || 82,
|
||||
value: 1.0,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push({
|
||||
action: "fast",
|
||||
key: Number(storage.fastKeyCode) || 71,
|
||||
value: Number(storage.fastSpeed) || 1.8,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
var storedBindings = Array.isArray(storage.keyBindings)
|
||||
? storage.keyBindings
|
||||
: [];
|
||||
|
||||
tc.settings.keyBindings = storedBindings
|
||||
.map((binding) => normalizeStoredBinding(binding))
|
||||
.filter(Boolean);
|
||||
|
||||
if (tc.settings.keyBindings.length === 0) {
|
||||
tc.settings.keyBindings = defaultKeyBindings(storage);
|
||||
tc.settings.version = "0.5.3";
|
||||
chrome.storage.sync.set({
|
||||
keyBindings: tc.settings.keyBindings,
|
||||
@@ -110,6 +552,13 @@ chrome.storage.sync.get(tc.settings, function (storage) {
|
||||
});
|
||||
}
|
||||
tc.settings.lastSpeed = Number(storage.lastSpeed);
|
||||
if (!isValidSpeed(tc.settings.lastSpeed) && tc.settings.lastSpeed !== 1.0) {
|
||||
log(`Invalid lastSpeed detected: ${storage.lastSpeed}, resetting to 1.0`, 3);
|
||||
tc.settings.lastSpeed = 1.0;
|
||||
chrome.storage.sync.set({ lastSpeed: 1.0 });
|
||||
} else if (!isValidSpeed(tc.settings.lastSpeed)) {
|
||||
tc.settings.lastSpeed = 1.0;
|
||||
}
|
||||
tc.settings.displayKeyCode = Number(storage.displayKeyCode);
|
||||
tc.settings.rememberSpeed = Boolean(storage.rememberSpeed);
|
||||
tc.settings.forceLastSavedSpeed = Boolean(storage.forceLastSavedSpeed);
|
||||
@@ -123,20 +572,23 @@ chrome.storage.sync.get(tc.settings, function (storage) {
|
||||
? Boolean(storage.enableSubtitleNudge)
|
||||
: tc.settings.enableSubtitleNudge;
|
||||
tc.settings.subtitleNudgeInterval =
|
||||
Number(storage.subtitleNudgeInterval) || 25;
|
||||
Number(storage.subtitleNudgeInterval) || 100; // Default 100ms for better performance
|
||||
tc.settings.subtitleNudgeAmount =
|
||||
Number(storage.subtitleNudgeAmount) || tc.settings.subtitleNudgeAmount;
|
||||
if (
|
||||
tc.settings.keyBindings.filter((x) => x.action == "display").length == 0
|
||||
) {
|
||||
tc.settings.keyBindings.push({
|
||||
action: "display",
|
||||
key: Number(storage.displayKeyCode) || 86,
|
||||
value: 0,
|
||||
force: false,
|
||||
predefined: true
|
||||
});
|
||||
tc.settings.keyBindings.push(
|
||||
createDefaultBinding(
|
||||
"display",
|
||||
"V",
|
||||
Number(storage.displayKeyCode) || 86,
|
||||
0
|
||||
)
|
||||
);
|
||||
chrome.storage.sync.set({ keyBindings: tc.settings.keyBindings });
|
||||
}
|
||||
patchAttachShadow();
|
||||
// Add a listener for messages from the popup.
|
||||
// We use a global flag to ensure the listener is only attached once.
|
||||
if (!window.vscMessageListener) {
|
||||
@@ -145,12 +597,8 @@ chrome.storage.sync.get(tc.settings, function (storage) {
|
||||
// Check if the message is a request to re-scan the page.
|
||||
if (request.action === "rescan_page") {
|
||||
log("Re-scan command received from popup.", 4);
|
||||
initializeWhenReady(document, true);
|
||||
|
||||
// Call the main initialization function. It's designed to be safe
|
||||
// to run multiple times and will pick up any new videos.
|
||||
initializeWhenReady(document);
|
||||
|
||||
// Send a response to the popup to confirm completion.
|
||||
sendResponse({ status: "complete" });
|
||||
}
|
||||
|
||||
@@ -184,78 +632,76 @@ function defineVideoController() {
|
||||
target.vsc = this;
|
||||
this.video = target;
|
||||
this.parent = target.parentElement || parent;
|
||||
this.nudgeIntervalId = null;
|
||||
this.nudgeAnimationId = null;
|
||||
this.restoreSpeedTimer = null;
|
||||
this.pendingRateChange = null;
|
||||
this.speedRestoreUntil = 0;
|
||||
|
||||
// Determine what speed to use
|
||||
let storedSpeed = tc.settings.speeds[target.currentSrc];
|
||||
if (!tc.settings.rememberSpeed) {
|
||||
if (!storedSpeed) {
|
||||
storedSpeed = 1.0;
|
||||
}
|
||||
} else {
|
||||
storedSpeed = tc.settings.lastSpeed;
|
||||
}
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
storedSpeed = tc.settings.lastSpeed;
|
||||
log(`Creating video controller for ${target.tagName} with src: ${target.src || target.currentSrc || 'none'}`, 4);
|
||||
|
||||
let storedSpeed = sanitizeSpeed(resolveTargetSpeed(target), 1.0);
|
||||
this.targetSpeed = storedSpeed;
|
||||
if (!tc.settings.rememberSpeed && !tc.settings.forceLastSavedSpeed) {
|
||||
setKeyBindings("reset", getKeyBindings("fast"));
|
||||
}
|
||||
|
||||
// FIXED: Actually apply the speed to the video element
|
||||
// Use setSpeed function to properly set the speed with all the necessary logic
|
||||
setTimeout(() => {
|
||||
if (this.video && this.video.vsc) {
|
||||
setSpeed(this.video, storedSpeed, true, false);
|
||||
}
|
||||
}, 0);
|
||||
log("Explicitly setting playbackRate to: " + storedSpeed, 5);
|
||||
target.playbackRate = storedSpeed;
|
||||
|
||||
this.div = this.initializeControls();
|
||||
|
||||
// Make the controller visible for 5 seconds on startup
|
||||
runAction("blink", 5000, null, this.video);
|
||||
if (!this.div) {
|
||||
log("ERROR: Failed to create controller div!", 2);
|
||||
return;
|
||||
}
|
||||
|
||||
log(`Controller created and attached to DOM. Hidden: ${this.div.classList.contains('vsc-hidden')}`, 4);
|
||||
|
||||
// Rewritten mediaEventAction to prevent speed reset on pause.
|
||||
var mediaEventAction = function (event) {
|
||||
// Handle subtitle nudging based on the event type first.
|
||||
if (event.type === "play") {
|
||||
this.startSubtitleNudge();
|
||||
extendSpeedRestoreWindow(event.target);
|
||||
|
||||
// FIXED: Only reapply speed if there's a significant mismatch AND it's a new video
|
||||
const currentSpeed = event.target.playbackRate;
|
||||
const videoId =
|
||||
event.target.currentSrc || event.target.src || "default";
|
||||
if (!tc.settings.rememberSpeed && !tc.settings.forceLastSavedSpeed) {
|
||||
setKeyBindings("reset", getKeyBindings("fast"));
|
||||
}
|
||||
|
||||
// Get the expected speed based on settings
|
||||
let expectedSpeed;
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
expectedSpeed = tc.settings.lastSpeed;
|
||||
var playSpeed = sanitizeSpeed(resolveTargetSpeed(event.target), 1.0);
|
||||
if (Math.abs(event.target.playbackRate - playSpeed) > 0.01) {
|
||||
log("Play event: setting playbackRate to: " + playSpeed, 4);
|
||||
setSpeed(event.target, playSpeed, false, false);
|
||||
} else if (playSpeed === 1.0 || event.target.paused) {
|
||||
this.stopSubtitleNudge();
|
||||
} else {
|
||||
expectedSpeed = tc.settings.speeds[videoId] || tc.settings.lastSpeed;
|
||||
this.startSubtitleNudge();
|
||||
}
|
||||
|
||||
// Only reapply speed if:
|
||||
// 1. The current speed is 1.0 (default) AND we have a stored speed that's different
|
||||
// 2. OR if forceLastSavedSpeed is enabled and speeds don't match
|
||||
const shouldReapplySpeed =
|
||||
(Math.abs(currentSpeed - 1.0) < 0.01 &&
|
||||
Math.abs(expectedSpeed - 1.0) > 0.01) ||
|
||||
(tc.settings.forceLastSavedSpeed &&
|
||||
Math.abs(currentSpeed - expectedSpeed) > 0.01);
|
||||
|
||||
if (shouldReapplySpeed) {
|
||||
setTimeout(() => {
|
||||
if (event.target.vsc) {
|
||||
setSpeed(event.target, expectedSpeed, false, false);
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
} else if (event.type === "pause" || event.type === "ended") {
|
||||
} else if (event.type === "pause") {
|
||||
extendSpeedRestoreWindow(event.target);
|
||||
this.stopSubtitleNudge();
|
||||
tc.isNudging = false;
|
||||
}
|
||||
} else if (event.type === "seeking") {
|
||||
extendSpeedRestoreWindow(event.target);
|
||||
} else if (event.type === "ended") {
|
||||
this.speedRestoreUntil = 0;
|
||||
this.stopSubtitleNudge();
|
||||
tc.isNudging = false;
|
||||
} else if (event.type === "seeked") {
|
||||
extendSpeedRestoreWindow(event.target);
|
||||
var expectedSpeed = sanitizeSpeed(resolveTargetSpeed(event.target), 1.0);
|
||||
var currentSpeed = event.target.playbackRate;
|
||||
|
||||
// For seek events, don't mess with speed
|
||||
if (event.type === "seeked" && isUserSeek) {
|
||||
isUserSeek = false;
|
||||
return;
|
||||
if (
|
||||
Math.abs(currentSpeed - expectedSpeed) > 0.01
|
||||
) {
|
||||
log(
|
||||
`Seeked: speed changed from ${expectedSpeed} to ${currentSpeed}, restoring`,
|
||||
4
|
||||
);
|
||||
setSpeed(event.target, expectedSpeed, false, false);
|
||||
}
|
||||
|
||||
if (isUserSeek) {
|
||||
isUserSeek = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -267,6 +713,10 @@ function defineVideoController() {
|
||||
"pause",
|
||||
(this.handlePause = mediaEventAction.bind(this))
|
||||
);
|
||||
target.addEventListener(
|
||||
"seeking",
|
||||
(this.handleSeeking = mediaEventAction.bind(this))
|
||||
);
|
||||
target.addEventListener(
|
||||
"ended",
|
||||
(this.handleEnded = mediaEventAction.bind(this))
|
||||
@@ -276,31 +726,6 @@ function defineVideoController() {
|
||||
(this.handleSeek = mediaEventAction.bind(this))
|
||||
);
|
||||
|
||||
// ADDITIONAL FIX: Listen for loadedmetadata to reapply speed when video source changes
|
||||
target.addEventListener("loadedmetadata", () => {
|
||||
if (this.video && this.video.vsc) {
|
||||
const currentSpeed = this.video.playbackRate;
|
||||
const videoId = this.video.currentSrc || this.video.src || "default";
|
||||
|
||||
// Get expected speed
|
||||
let expectedSpeed;
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
expectedSpeed = tc.settings.lastSpeed;
|
||||
} else {
|
||||
expectedSpeed = tc.settings.speeds[videoId] || tc.settings.lastSpeed;
|
||||
}
|
||||
|
||||
// Only reapply if current speed is default (1.0) and we have a different stored speed
|
||||
const shouldReapplySpeed =
|
||||
Math.abs(currentSpeed - 1.0) < 0.01 &&
|
||||
Math.abs(expectedSpeed - 1.0) > 0.01;
|
||||
|
||||
if (shouldReapplySpeed) {
|
||||
setSpeed(this.video, expectedSpeed, false, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var srcObserver = new MutationObserver((mutations) => {
|
||||
mutations.forEach((mutation) => {
|
||||
if (
|
||||
@@ -308,31 +733,20 @@ function defineVideoController() {
|
||||
(mutation.attributeName === "src" ||
|
||||
mutation.attributeName === "currentSrc")
|
||||
) {
|
||||
log("mutation of A/V element", 5);
|
||||
if (this.div) {
|
||||
this.stopSubtitleNudge();
|
||||
if (!mutation.target.src && !mutation.target.currentSrc) {
|
||||
this.div.classList.add("vsc-nosource");
|
||||
} else {
|
||||
this.div.classList.remove("vsc-nosource");
|
||||
|
||||
// FIXED: Reapply speed when source changes (like in shorts)
|
||||
const expectedSpeed = tc.settings.forceLastSavedSpeed
|
||||
? tc.settings.lastSpeed
|
||||
: tc.settings.speeds[mutation.target.currentSrc] ||
|
||||
tc.settings.lastSpeed;
|
||||
|
||||
setTimeout(() => {
|
||||
if (mutation.target.vsc) {
|
||||
setSpeed(mutation.target, expectedSpeed, false, false);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
if (!mutation.target.paused) this.startSubtitleNudge();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
this.srcObserver = srcObserver;
|
||||
srcObserver.observe(target, { attributeFilter: ["src", "currentSrc"] });
|
||||
if (!target.paused && target.playbackRate !== 1.0)
|
||||
this.startSubtitleNudge();
|
||||
@@ -341,13 +755,16 @@ function defineVideoController() {
|
||||
tc.videoController.prototype.remove = function () {
|
||||
this.stopSubtitleNudge();
|
||||
if (this.div) this.div.remove();
|
||||
if (this.restoreSpeedTimer) clearTimeout(this.restoreSpeedTimer);
|
||||
if (this.video) {
|
||||
this.video.removeEventListener("play", this.handlePlay);
|
||||
this.video.removeEventListener("pause", this.handlePause);
|
||||
this.video.removeEventListener("seeking", this.handleSeeking);
|
||||
this.video.removeEventListener("ended", this.handleEnded);
|
||||
this.video.removeEventListener("seeked", this.handleSeek);
|
||||
delete this.video.vsc;
|
||||
}
|
||||
if (this.srcObserver) this.srcObserver.disconnect();
|
||||
let idx = tc.mediaElements.indexOf(this.video);
|
||||
if (idx != -1) tc.mediaElements.splice(idx, 1);
|
||||
};
|
||||
@@ -358,76 +775,110 @@ function defineVideoController() {
|
||||
this.video.currentSrc &&
|
||||
this.video.currentSrc.includes("googlevideo.com")) ||
|
||||
location.hostname.includes("youtube.com");
|
||||
if (!isYouTube) return;
|
||||
if (
|
||||
!isYouTube ||
|
||||
!tc.settings.enableSubtitleNudge ||
|
||||
this.nudgeIntervalId !== null ||
|
||||
!this.video
|
||||
this.nudgeAnimationId !== null ||
|
||||
!this.video ||
|
||||
this.video.paused ||
|
||||
this.video.playbackRate === 1.0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (this.video.paused || this.video.playbackRate === 1.0) {
|
||||
this.stopSubtitleNudge();
|
||||
return;
|
||||
}
|
||||
// Additional check to not start if paused
|
||||
if (this.video.paused) {
|
||||
return;
|
||||
}
|
||||
log(`Nudge: Starting interval: ${tc.settings.subtitleNudgeInterval}ms.`, 5);
|
||||
this.nudgeIntervalId = setInterval(() => {
|
||||
if (
|
||||
!this.video ||
|
||||
this.video.paused ||
|
||||
this.video.ended ||
|
||||
this.video.playbackRate === 1.0 ||
|
||||
tc.isNudging
|
||||
) {
|
||||
|
||||
// Store the target speed so we can always revert to it
|
||||
this.targetSpeed = this.video.playbackRate;
|
||||
|
||||
const performNudge = () => {
|
||||
// Check if we should stop
|
||||
if (!this.video || this.video.paused || this.video.playbackRate === 1.0) {
|
||||
this.stopSubtitleNudge();
|
||||
return;
|
||||
}
|
||||
// Double-check pause state before nudging
|
||||
if (this.video.paused) {
|
||||
this.stopSubtitleNudge();
|
||||
|
||||
// CRITICAL: Don't nudge if tab is hidden - prevents speed drift
|
||||
if (document.hidden) {
|
||||
this.nudgeAnimationId = setTimeout(performNudge, tc.settings.subtitleNudgeInterval);
|
||||
return;
|
||||
}
|
||||
const currentRate = this.video.playbackRate;
|
||||
const nudgeAmount = tc.settings.subtitleNudgeAmount;
|
||||
|
||||
// Set flag to prevent ratechange listener from interfering
|
||||
tc.isNudging = true;
|
||||
this.video.playbackRate = currentRate + nudgeAmount;
|
||||
requestAnimationFrame(() => {
|
||||
if (
|
||||
this.video &&
|
||||
Math.abs(this.video.playbackRate - (currentRate + nudgeAmount)) <
|
||||
nudgeAmount * 1.5
|
||||
) {
|
||||
this.video.playbackRate = currentRate;
|
||||
|
||||
// Cache values to avoid repeated property access
|
||||
const targetSpeed = this.targetSpeed;
|
||||
const nudgeAmount = tc.settings.subtitleNudgeAmount;
|
||||
|
||||
// Apply nudge from the stored target speed (not current rate)
|
||||
this.video.playbackRate = targetSpeed + nudgeAmount;
|
||||
|
||||
// Revert synchronously after a microtask to ensure it happens immediately
|
||||
Promise.resolve().then(() => {
|
||||
if (this.video && targetSpeed) {
|
||||
this.video.playbackRate = targetSpeed;
|
||||
}
|
||||
tc.isNudging = false;
|
||||
});
|
||||
}, tc.settings.subtitleNudgeInterval);
|
||||
|
||||
// Schedule next nudge
|
||||
this.nudgeAnimationId = setTimeout(performNudge, tc.settings.subtitleNudgeInterval);
|
||||
};
|
||||
|
||||
// Start the first nudge
|
||||
this.nudgeAnimationId = setTimeout(performNudge, tc.settings.subtitleNudgeInterval);
|
||||
log(`Nudge: Starting with interval ${tc.settings.subtitleNudgeInterval}ms.`, 5);
|
||||
};
|
||||
|
||||
tc.videoController.prototype.stopSubtitleNudge = function () {
|
||||
if (this.nudgeIntervalId !== null) {
|
||||
if (this.nudgeAnimationId !== null) {
|
||||
clearTimeout(this.nudgeAnimationId);
|
||||
this.nudgeAnimationId = null;
|
||||
log(`Nudge: Stopping.`, 5);
|
||||
clearInterval(this.nudgeIntervalId);
|
||||
this.nudgeIntervalId = null;
|
||||
}
|
||||
// Clear the target speed when stopping
|
||||
this.targetSpeed = null;
|
||||
};
|
||||
|
||||
tc.videoController.prototype.performImmediateNudge = function () {
|
||||
const isYouTube =
|
||||
(this.video &&
|
||||
this.video.currentSrc &&
|
||||
this.video.currentSrc.includes("googlevideo.com")) ||
|
||||
location.hostname.includes("youtube.com");
|
||||
|
||||
if (
|
||||
!isYouTube ||
|
||||
!tc.settings.enableSubtitleNudge ||
|
||||
!this.video ||
|
||||
this.video.paused ||
|
||||
this.video.playbackRate === 1.0 ||
|
||||
document.hidden
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const targetRate = this.targetSpeed || this.video.playbackRate;
|
||||
const nudgeAmount = tc.settings.subtitleNudgeAmount;
|
||||
|
||||
tc.isNudging = true;
|
||||
this.video.playbackRate = targetRate + nudgeAmount;
|
||||
|
||||
// Revert synchronously via microtask
|
||||
Promise.resolve().then(() => {
|
||||
if (this.video) {
|
||||
this.video.playbackRate = targetRate;
|
||||
}
|
||||
tc.isNudging = false;
|
||||
});
|
||||
|
||||
log(`Immediate nudge performed at rate ${targetRate.toFixed(2)}`, 5);
|
||||
};
|
||||
|
||||
tc.videoController.prototype.initializeControls = function () {
|
||||
const doc = this.video.ownerDocument;
|
||||
const speed = this.video.playbackRate.toFixed(2);
|
||||
// Fix for videos rendered after page load - use relative positioning
|
||||
var top = "10px",
|
||||
left = "10px";
|
||||
|
||||
// Try to get actual position, but fallback to default if not available
|
||||
if (this.video.offsetTop > 0 || this.video.offsetLeft > 0) {
|
||||
top = Math.max(this.video.offsetTop, 0) + "px";
|
||||
left = Math.max(this.video.offsetLeft, 0) + "px";
|
||||
}
|
||||
var top = "0px",
|
||||
left = "0px";
|
||||
var wrapper = doc.createElement("div");
|
||||
wrapper.classList.add("vsc-controller");
|
||||
if (!this.video.src && !this.video.currentSrc)
|
||||
@@ -471,32 +922,49 @@ function defineVideoController() {
|
||||
var fragment = doc.createDocumentFragment();
|
||||
fragment.appendChild(wrapper);
|
||||
const parentEl = this.parent || this.video.parentElement;
|
||||
|
||||
log(`Inserting controller: parentEl=${!!parentEl}, parentNode=${!!parentEl?.parentNode}, hostname=${location.hostname}`, 4);
|
||||
|
||||
if (!parentEl || !parentEl.parentNode) {
|
||||
log("No suitable parent found, appending to body", 4);
|
||||
doc.body.appendChild(fragment);
|
||||
return wrapper;
|
||||
}
|
||||
switch (true) {
|
||||
case location.hostname == "www.amazon.com":
|
||||
case location.hostname == "www.reddit.com":
|
||||
case /hbogo\./.test(location.hostname):
|
||||
parentEl.parentElement.insertBefore(fragment, parentEl);
|
||||
break;
|
||||
case location.hostname == "www.facebook.com":
|
||||
let p =
|
||||
parentEl.parentElement.parentElement.parentElement.parentElement
|
||||
.parentElement.parentElement.parentElement;
|
||||
if (p && p.firstChild) p.insertBefore(fragment, p.firstChild);
|
||||
else parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
break;
|
||||
case location.hostname == "tv.apple.com":
|
||||
const r = parentEl.getRootNode();
|
||||
const s = r && r.querySelector ? r.querySelector(".scrim") : null;
|
||||
if (s) s.prepend(fragment);
|
||||
else parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
break;
|
||||
default:
|
||||
parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
|
||||
try {
|
||||
switch (true) {
|
||||
case location.hostname == "www.amazon.com":
|
||||
case location.hostname == "www.reddit.com":
|
||||
case /hbogo\./.test(location.hostname):
|
||||
log("Using parentElement.parentElement insertion", 5);
|
||||
parentEl.parentElement.insertBefore(fragment, parentEl);
|
||||
break;
|
||||
case location.hostname == "www.facebook.com":
|
||||
log("Using Facebook-specific insertion", 5);
|
||||
let p =
|
||||
parentEl.parentElement.parentElement.parentElement.parentElement
|
||||
.parentElement.parentElement.parentElement;
|
||||
if (p && p.firstChild) p.insertBefore(fragment, p.firstChild);
|
||||
else parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
break;
|
||||
case location.hostname == "tv.apple.com":
|
||||
log("Using Apple TV-specific insertion", 5);
|
||||
const r = parentEl.getRootNode();
|
||||
const s = r && r.querySelector ? r.querySelector(".scrim") : null;
|
||||
if (s) s.prepend(fragment);
|
||||
else parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
break;
|
||||
default:
|
||||
log("Using default insertion method", 5);
|
||||
parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
}
|
||||
log("Controller successfully inserted into DOM", 4);
|
||||
} catch (error) {
|
||||
log(`Error inserting controller: ${error.message}`, 2);
|
||||
// Fallback to body insertion
|
||||
doc.body.appendChild(fragment);
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
};
|
||||
}
|
||||
@@ -527,70 +995,133 @@ function isBlacklisted() {
|
||||
if (b) log(`Page ${location.href} blacklisted.`, 4);
|
||||
return b;
|
||||
}
|
||||
var coolDown = false;
|
||||
function refreshCoolDown() {
|
||||
if (coolDown) clearTimeout(coolDown);
|
||||
coolDown = setTimeout(function () {
|
||||
coolDown = false;
|
||||
}, 1000);
|
||||
|
||||
function shouldPreserveDesiredSpeed(video, speed) {
|
||||
if (!video || !video.vsc) return false;
|
||||
var desiredSpeed = getDesiredSpeed(video);
|
||||
if (!isValidSpeed(desiredSpeed) || Math.abs(speed - desiredSpeed) <= 0.01) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
video.paused === true ||
|
||||
(typeof video.vsc.speedRestoreUntil === "number" &&
|
||||
video.vsc.speedRestoreUntil > Date.now())
|
||||
);
|
||||
}
|
||||
|
||||
function setupListener() {
|
||||
if (document.vscRateListenerAttached) return;
|
||||
function updateSpeedFromEvent(video, fromUserInput = false) {
|
||||
function setupListener(root) {
|
||||
root = root || document;
|
||||
if (root.vscRateListenerAttached) return;
|
||||
function updateSpeedFromEvent(video) {
|
||||
if (!video.vsc || !video.vsc.speedIndicator) return;
|
||||
var speed = Number(video.playbackRate.toFixed(2));
|
||||
video.vsc.speedIndicator.textContent = speed.toFixed(2);
|
||||
tc.settings.speeds[video.currentSrc || "unknown_src"] = speed;
|
||||
tc.settings.lastSpeed = speed;
|
||||
chrome.storage.sync.set({ lastSpeed: speed }, () => {});
|
||||
if (fromUserInput) {
|
||||
runAction("blink", 1000, null, video);
|
||||
video.vsc.targetSpeed = speed;
|
||||
var sourceKey = getVideoSourceKey(video);
|
||||
if (sourceKey !== "unknown_src") {
|
||||
tc.settings.speeds[sourceKey] = speed;
|
||||
}
|
||||
tc.settings.lastSpeed = speed;
|
||||
chrome.storage.sync.set({ lastSpeed: speed }, () => { });
|
||||
if (video.vsc) {
|
||||
if (speed === 1.0 || video.paused) video.vsc.stopSubtitleNudge();
|
||||
else video.vsc.startSubtitleNudge();
|
||||
}
|
||||
}
|
||||
document.addEventListener(
|
||||
root.addEventListener(
|
||||
"ratechange",
|
||||
function (event) {
|
||||
if (tc.isNudging) return;
|
||||
if (coolDown) {
|
||||
event.stopImmediatePropagation();
|
||||
return;
|
||||
}
|
||||
var video = event.target;
|
||||
if (!video || typeof video.playbackRate === "undefined" || !video.vsc)
|
||||
return;
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
if (event.detail && event.detail.origin === "videoSpeed") {
|
||||
video.playbackRate = event.detail.speed;
|
||||
updateSpeedFromEvent(video, event.detail.fromUserInput === true);
|
||||
updateSpeedFromEvent(video);
|
||||
} else {
|
||||
video.playbackRate = tc.settings.lastSpeed;
|
||||
video.playbackRate = sanitizeSpeed(tc.settings.lastSpeed, 1.0);
|
||||
}
|
||||
event.stopImmediatePropagation();
|
||||
} else {
|
||||
updateSpeedFromEvent(video, video.vscIsDirectlySettingRate === true);
|
||||
if (video.vscIsDirectlySettingRate)
|
||||
delete video.vscIsDirectlySettingRate;
|
||||
var currentSpeed = Number(video.playbackRate.toFixed(2));
|
||||
var desiredSpeed = getDesiredSpeed(video);
|
||||
var pendingRateChange = takePendingRateChange(video, currentSpeed);
|
||||
|
||||
if (pendingRateChange) {
|
||||
updateSpeedFromEvent(video);
|
||||
return;
|
||||
}
|
||||
|
||||
if (shouldPreserveDesiredSpeed(video, currentSpeed)) {
|
||||
log(
|
||||
`Ignoring external rate change to ${currentSpeed.toFixed(2)} while preserving ${desiredSpeed.toFixed(2)}`,
|
||||
4
|
||||
);
|
||||
video.vsc.speedIndicator.textContent = desiredSpeed.toFixed(2);
|
||||
scheduleSpeedRestore(video, desiredSpeed, "pause/play or seek");
|
||||
return;
|
||||
}
|
||||
|
||||
updateSpeedFromEvent(video);
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
document.vscRateListenerAttached = true;
|
||||
root.vscRateListenerAttached = true;
|
||||
}
|
||||
|
||||
var vscInitializedDocuments = new Set();
|
||||
function initializeWhenReady(doc) {
|
||||
if (vscInitializedDocuments.has(doc) || !doc.body) return;
|
||||
if (doc.readyState === "complete") {
|
||||
initializeNow(doc);
|
||||
function clearPendingInitialization(doc) {
|
||||
if (!doc || !doc.vscPendingInitializeHandler) return;
|
||||
|
||||
var handler = doc.vscPendingInitializeHandler;
|
||||
doc.removeEventListener("DOMContentLoaded", handler);
|
||||
doc.removeEventListener("readystatechange", handler);
|
||||
|
||||
if (doc.defaultView) {
|
||||
doc.defaultView.removeEventListener("load", handler);
|
||||
}
|
||||
|
||||
delete doc.vscPendingInitializeHandler;
|
||||
doc.vscPendingForceReinit = false;
|
||||
}
|
||||
|
||||
function tryInitializeDocument(doc, forceReinit) {
|
||||
if (!doc) return false;
|
||||
if ((!forceReinit && vscInitializedDocuments.has(doc)) || !doc.body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
initializeNow(doc, forceReinit);
|
||||
clearPendingInitialization(doc);
|
||||
return true;
|
||||
}
|
||||
|
||||
function initializeWhenReady(doc, forceReinit = false) {
|
||||
if (!doc) return;
|
||||
doc.vscPendingForceReinit = doc.vscPendingForceReinit === true || forceReinit;
|
||||
|
||||
if (tryInitializeDocument(doc, doc.vscPendingForceReinit)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (doc.vscPendingInitializeHandler) return;
|
||||
|
||||
var pendingInitializeHandler = function () {
|
||||
tryInitializeDocument(doc, doc.vscPendingForceReinit === true);
|
||||
};
|
||||
|
||||
doc.vscPendingInitializeHandler = pendingInitializeHandler;
|
||||
doc.addEventListener("DOMContentLoaded", pendingInitializeHandler);
|
||||
doc.addEventListener("readystatechange", pendingInitializeHandler);
|
||||
|
||||
if (doc.defaultView) {
|
||||
doc.defaultView.addEventListener("load", pendingInitializeHandler);
|
||||
doc.defaultView.setTimeout(pendingInitializeHandler, 0);
|
||||
} else {
|
||||
doc.addEventListener("DOMContentLoaded", () => initializeNow(doc), {
|
||||
once: true
|
||||
});
|
||||
setTimeout(pendingInitializeHandler, 0);
|
||||
}
|
||||
}
|
||||
function inIframe() {
|
||||
@@ -600,41 +1131,18 @@ function inIframe() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
function getShadow(parent) {
|
||||
let r = [];
|
||||
function gC(p) {
|
||||
if (p.firstElementChild) {
|
||||
var c = p.firstElementChild;
|
||||
do {
|
||||
r.push(c);
|
||||
gC(c);
|
||||
if (c.shadowRoot) r.push(...getShadow(c.shadowRoot));
|
||||
c = c.nextElementSibling;
|
||||
} while (c);
|
||||
}
|
||||
}
|
||||
gC(parent);
|
||||
return r;
|
||||
}
|
||||
|
||||
function initializeNow(doc) {
|
||||
if (vscInitializedDocuments.has(doc) || !doc.body) return;
|
||||
if (!tc.settings.enabled) return;
|
||||
if (!doc.body.classList.contains("vsc-initialized"))
|
||||
doc.body.classList.add("vsc-initialized");
|
||||
if (typeof tc.videoController === "undefined") defineVideoController();
|
||||
setupListener();
|
||||
|
||||
var docs = Array(doc);
|
||||
function attachKeydownListeners(doc) {
|
||||
var docs = [doc];
|
||||
try {
|
||||
if (inIframe()) docs.push(window.top.document);
|
||||
if (inIframe() && window.top.document !== doc) docs.push(window.top.document);
|
||||
} catch (e) {}
|
||||
docs.forEach(function (d) {
|
||||
if (d.vscKeydownListenerAttached) return; // Prevent duplicate listeners
|
||||
d.addEventListener(
|
||||
|
||||
docs.forEach(function (keyDoc) {
|
||||
if (keyDoc.vscKeydownListenerAttached) return;
|
||||
keyDoc.addEventListener(
|
||||
"keydown",
|
||||
function (event) {
|
||||
var keyCode = event.keyCode;
|
||||
if (
|
||||
!event.getModifierState ||
|
||||
event.getModifierState("Alt") ||
|
||||
@@ -643,16 +1151,24 @@ function initializeNow(doc) {
|
||||
event.getModifierState("Meta") ||
|
||||
event.getModifierState("Hyper") ||
|
||||
event.getModifierState("OS")
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
event.target.nodeName === "INPUT" ||
|
||||
event.target.nodeName === "TEXTAREA" ||
|
||||
event.target.isContentEditable
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tc.mediaElements.length) return;
|
||||
var item = tc.settings.keyBindings.find((item) => item.key === keyCode);
|
||||
|
||||
var item = tc.settings.keyBindings.find(function (binding) {
|
||||
return matchesKeyBinding(binding, event);
|
||||
});
|
||||
|
||||
if (item) {
|
||||
runAction(item.action, item.value, event);
|
||||
if (item.force === "true") {
|
||||
@@ -660,116 +1176,172 @@ function initializeNow(doc) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
true
|
||||
);
|
||||
d.vscKeydownListenerAttached = true;
|
||||
keyDoc.vscKeydownListenerAttached = true;
|
||||
});
|
||||
}
|
||||
|
||||
function attachMutationObserver(root) {
|
||||
if (root.vscMutationObserverAttached) return;
|
||||
|
||||
var pendingMutations = [];
|
||||
var mutationProcessingScheduled = false;
|
||||
var observer = new MutationObserver(function (mutations) {
|
||||
pendingMutations.push(...mutations);
|
||||
if (mutationProcessingScheduled) return;
|
||||
|
||||
mutationProcessingScheduled = true;
|
||||
requestIdle(
|
||||
function () {
|
||||
var mutationsToProcess = pendingMutations.splice(0);
|
||||
mutationProcessingScheduled = false;
|
||||
|
||||
mutationsToProcess.forEach(function (mutation) {
|
||||
if (mutation.type === "childList") {
|
||||
mutation.addedNodes.forEach(function (node) {
|
||||
scanNodeForMedia(node, node.parentNode || mutation.target, true);
|
||||
});
|
||||
mutation.removedNodes.forEach(function (node) {
|
||||
scanNodeForMedia(node, node.parentNode || mutation.target, false);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (mutation.type !== "attributes") return;
|
||||
|
||||
var target = mutation.target;
|
||||
if (
|
||||
isMediaElement(target) &&
|
||||
(mutation.attributeName === "src" ||
|
||||
mutation.attributeName === "currentSrc")
|
||||
) {
|
||||
ensureController(target, target.parentElement || target.parentNode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
mutation.attributeName === "aria-hidden" &&
|
||||
target.attributes["aria-hidden"] &&
|
||||
target.attributes["aria-hidden"].value === "false"
|
||||
) {
|
||||
scanRootForMedia(root);
|
||||
}
|
||||
});
|
||||
},
|
||||
{ timeout: 1000 }
|
||||
);
|
||||
});
|
||||
|
||||
if (!doc.vscMutationObserverAttached) {
|
||||
const observer = new MutationObserver(function (mutations) {
|
||||
requestIdleCallback(
|
||||
(_) => {
|
||||
mutations.forEach(function (mutation) {
|
||||
switch (mutation.type) {
|
||||
case "childList":
|
||||
mutation.addedNodes.forEach(function (node) {
|
||||
if (typeof node === "function") return;
|
||||
checkForVideo(node, node.parentNode || mutation.target, true);
|
||||
});
|
||||
mutation.removedNodes.forEach(function (node) {
|
||||
if (typeof node === "function") return;
|
||||
checkForVideo(
|
||||
node,
|
||||
node.parentNode || mutation.target,
|
||||
false
|
||||
);
|
||||
});
|
||||
break;
|
||||
case "attributes":
|
||||
if (
|
||||
mutation.target.attributes["aria-hidden"] &&
|
||||
mutation.target.attributes["aria-hidden"].value == "false"
|
||||
) {
|
||||
var flattenedNodes = getShadow(document.body);
|
||||
var node = flattenedNodes.filter(
|
||||
(x) => x.tagName == "VIDEO"
|
||||
)[0];
|
||||
if (node) {
|
||||
if (node.vsc) node.vsc.remove();
|
||||
checkForVideo(
|
||||
node,
|
||||
node.parentNode || mutation.target,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
},
|
||||
{ timeout: 1000 }
|
||||
);
|
||||
});
|
||||
function checkForVideo(node, parent, added) {
|
||||
if (!added && document.body.contains(node)) return;
|
||||
if (
|
||||
node.nodeName === "VIDEO" ||
|
||||
(node.nodeName === "AUDIO" && tc.settings.audioBoolean)
|
||||
) {
|
||||
if (added) {
|
||||
if (!node.vsc) node.vsc = new tc.videoController(node, parent);
|
||||
} else {
|
||||
if (node.vsc) node.vsc.remove();
|
||||
}
|
||||
} else if (node.children != undefined) {
|
||||
for (var i = 0; i < node.children.length; i++) {
|
||||
const child = node.children[i];
|
||||
checkForVideo(child, child.parentNode || parent, added);
|
||||
}
|
||||
}
|
||||
observer.observe(root, {
|
||||
attributeFilter: ["aria-hidden", "src", "currentSrc"],
|
||||
childList: true,
|
||||
subtree: true,
|
||||
attributes: true
|
||||
});
|
||||
|
||||
root.vscMutationObserverAttached = true;
|
||||
}
|
||||
|
||||
function attachMediaDetectionListeners(root) {
|
||||
if (root.vscMediaEventListenersAttached) return;
|
||||
|
||||
var handleDetectedMedia = function (event) {
|
||||
var target = event.target;
|
||||
if (!isMediaElement(target)) return;
|
||||
ensureController(target, target.parentElement || target.parentNode);
|
||||
};
|
||||
|
||||
[
|
||||
"loadstart",
|
||||
"loadeddata",
|
||||
"loadedmetadata",
|
||||
"canplay",
|
||||
"playing",
|
||||
"play"
|
||||
].forEach(function (eventName) {
|
||||
root.addEventListener(eventName, handleDetectedMedia, true);
|
||||
});
|
||||
root.vscMediaEventListenersAttached = true;
|
||||
}
|
||||
|
||||
function attachIframeListeners(doc) {
|
||||
Array.from(doc.getElementsByTagName("iframe")).forEach(function (frame) {
|
||||
if (!frame.vscLoadListenerAttached) {
|
||||
frame.addEventListener("load", function () {
|
||||
try {
|
||||
if (frame.contentDocument) {
|
||||
initializeWhenReady(frame.contentDocument, true);
|
||||
}
|
||||
} catch (e) {}
|
||||
});
|
||||
frame.vscLoadListenerAttached = true;
|
||||
}
|
||||
observer.observe(doc, {
|
||||
attributeFilter: ["aria-hidden"],
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
doc.vscMutationObserverAttached = true;
|
||||
|
||||
try {
|
||||
if (frame.contentDocument) {
|
||||
initializeWhenReady(frame.contentDocument);
|
||||
}
|
||||
} catch (e) {}
|
||||
});
|
||||
}
|
||||
|
||||
function attachNavigationListeners() {
|
||||
if (window.vscNavigationListenersAttached) return;
|
||||
|
||||
var scheduleRescan = function () {
|
||||
clearTimeout(window.vscNavigationRescanTimer);
|
||||
window.vscNavigationRescanTimer = setTimeout(function () {
|
||||
initializeWhenReady(document, true);
|
||||
}, 300);
|
||||
};
|
||||
|
||||
["pushState", "replaceState"].forEach(function (method) {
|
||||
if (typeof history[method] !== "function") return;
|
||||
var original = history[method];
|
||||
history[method] = function () {
|
||||
var result = original.apply(this, arguments);
|
||||
scheduleRescan();
|
||||
return result;
|
||||
};
|
||||
});
|
||||
|
||||
window.addEventListener("popstate", scheduleRescan);
|
||||
window.addEventListener("hashchange", scheduleRescan);
|
||||
window.vscNavigationListenersAttached = true;
|
||||
}
|
||||
|
||||
function initializeNow(doc, forceReinit = false) {
|
||||
if ((!forceReinit && vscInitializedDocuments.has(doc)) || !doc.body) return;
|
||||
if (!tc.settings.enabled) return;
|
||||
|
||||
if (!doc.body.classList.contains("vsc-initialized")) {
|
||||
doc.body.classList.add("vsc-initialized");
|
||||
}
|
||||
if (typeof tc.videoController === "undefined") defineVideoController();
|
||||
attachKeydownListeners(doc);
|
||||
attachNavigationListeners();
|
||||
observeRoot(doc);
|
||||
|
||||
if (forceReinit) {
|
||||
log("Force re-initialization requested", 4);
|
||||
}
|
||||
|
||||
const q = tc.settings.audioBoolean ? "video,audio" : "video";
|
||||
const foundVideos = doc.querySelectorAll(q);
|
||||
foundVideos.forEach((v) => {
|
||||
if (!v.vsc) new tc.videoController(v, v.parentElement);
|
||||
});
|
||||
|
||||
Array.from(doc.getElementsByTagName("iframe")).forEach((f) => {
|
||||
if (f.vscLoadListenerAttached) return;
|
||||
f.addEventListener("load", () => {
|
||||
try {
|
||||
if (f.contentDocument) {
|
||||
initializeWhenReady(f.contentDocument);
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore CORS errors
|
||||
}
|
||||
});
|
||||
f.vscLoadListenerAttached = true;
|
||||
try {
|
||||
if (f.contentDocument) {
|
||||
initializeWhenReady(f.contentDocument);
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore CORS errors
|
||||
}
|
||||
});
|
||||
vscInitializedDocuments.add(doc);
|
||||
}
|
||||
|
||||
function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
const numericSpeed = Number(speed);
|
||||
if (isNaN(numericSpeed) || numericSpeed <= 0 || numericSpeed > 16) return;
|
||||
|
||||
if (!isValidSpeed(numericSpeed)) {
|
||||
log(
|
||||
`Invalid speed rejected: ${speed}, must be between ${MIN_SPEED} and ${MAX_SPEED}`,
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!video || !video.vsc || !video.vsc.speedIndicator) return;
|
||||
|
||||
log(
|
||||
@@ -779,6 +1351,9 @@ function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
tc.settings.lastSpeed = numericSpeed;
|
||||
video.vsc.speedIndicator.textContent = numericSpeed.toFixed(2);
|
||||
|
||||
// Update the target speed for nudge so it knows what to revert to
|
||||
video.vsc.targetSpeed = numericSpeed;
|
||||
|
||||
if (isUserKeyPress && !isInitialCall && video.vsc && video.vsc.div) {
|
||||
runAction("blink", 1000, null, video); // Pass video to blink
|
||||
}
|
||||
@@ -795,13 +1370,10 @@ function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
);
|
||||
} else {
|
||||
if (Math.abs(video.playbackRate - numericSpeed) > 0.001) {
|
||||
if (isUserKeyPress && !isInitialCall) {
|
||||
video.vscIsDirectlySettingRate = true; // Set flag for ratechange listener
|
||||
}
|
||||
rememberPendingRateChange(video, numericSpeed);
|
||||
video.playbackRate = numericSpeed;
|
||||
}
|
||||
}
|
||||
if (!isInitialCall) refreshCoolDown();
|
||||
if (video.vsc) {
|
||||
if (numericSpeed === 1.0 || video.paused) video.vsc.stopSubtitleNudge();
|
||||
else video.vsc.startSubtitleNudge();
|
||||
@@ -858,25 +1430,28 @@ function runAction(action, value, e) {
|
||||
switch (action) {
|
||||
case "rewind":
|
||||
isUserSeek = true;
|
||||
extendSpeedRestoreWindow(v);
|
||||
v.currentTime -= numValue;
|
||||
break;
|
||||
case "advance":
|
||||
isUserSeek = true;
|
||||
extendSpeedRestoreWindow(v);
|
||||
v.currentTime += numValue;
|
||||
break;
|
||||
case "faster":
|
||||
setSpeed(
|
||||
v,
|
||||
Math.min(
|
||||
(v.playbackRate < 0.07 ? 0.07 : v.playbackRate) + numValue,
|
||||
16
|
||||
),
|
||||
false,
|
||||
true
|
||||
);
|
||||
// Round to the step precision to avoid floating-point issues (e.g., 1.80 + 0.1 = 1.9000000000000001)
|
||||
var fasterStep = numValue;
|
||||
var fasterPrecision = Math.round(1 / fasterStep); // e.g., 0.1 -> 10, 0.05 -> 20, 0.25 -> 4
|
||||
var newFasterSpeed = (v.playbackRate < MIN_SPEED ? MIN_SPEED : v.playbackRate) + fasterStep;
|
||||
newFasterSpeed = Math.round(newFasterSpeed * fasterPrecision) / fasterPrecision;
|
||||
setSpeed(v, Math.min(newFasterSpeed, MAX_SPEED), false, true);
|
||||
break;
|
||||
case "slower":
|
||||
setSpeed(v, Math.max(v.playbackRate - numValue, 0.07), false, true);
|
||||
var slowerStep = numValue;
|
||||
var slowerPrecision = Math.round(1 / slowerStep);
|
||||
var newSlowerSpeed = v.playbackRate - slowerStep;
|
||||
newSlowerSpeed = Math.round(newSlowerSpeed * slowerPrecision) / slowerPrecision;
|
||||
setSpeed(v, Math.max(newSlowerSpeed, MIN_SPEED), false, true);
|
||||
break;
|
||||
case "reset":
|
||||
resetSpeed(v, 1.0, false); // Use enhanced resetSpeed
|
||||
@@ -885,27 +1460,43 @@ function runAction(action, value, e) {
|
||||
resetSpeed(v, numValue, true); // Use enhanced resetSpeed
|
||||
break;
|
||||
case "display":
|
||||
controller.classList.add("vsc-manual");
|
||||
controller.classList.toggle("vsc-hidden");
|
||||
if (controller.classList.contains("vsc-hidden")) {
|
||||
controller.classList.remove("vsc-hidden");
|
||||
controller.classList.add("vsc-manual");
|
||||
} else {
|
||||
controller.classList.add("vsc-hidden");
|
||||
controller.classList.remove("vsc-manual");
|
||||
}
|
||||
break;
|
||||
case "blink":
|
||||
log(`Blink action: controller hidden=${controller.classList.contains("vsc-hidden")}, timeout=${controller.blinkTimeOut !== undefined}, duration=${numValue}`, 5);
|
||||
|
||||
if (
|
||||
controller.classList.contains("vsc-hidden") ||
|
||||
controller.blinkTimeOut !== undefined
|
||||
) {
|
||||
clearTimeout(controller.blinkTimeOut);
|
||||
var restoreHidden =
|
||||
controller.restoreHiddenAfterBlink === true ||
|
||||
controller.classList.contains("vsc-hidden");
|
||||
|
||||
if (controller.blinkTimeOut !== undefined) {
|
||||
clearTimeout(controller.blinkTimeOut);
|
||||
}
|
||||
|
||||
controller.restoreHiddenAfterBlink = restoreHidden;
|
||||
controller.classList.remove("vsc-hidden");
|
||||
log(`Controller shown, setting timeout for ${numValue || 1000}ms`, 5);
|
||||
|
||||
controller.blinkTimeOut = setTimeout(() => {
|
||||
if (
|
||||
!(
|
||||
controller.classList.contains("vsc-manual") &&
|
||||
!controller.classList.contains("vsc-hidden")
|
||||
)
|
||||
) {
|
||||
if (controller.restoreHiddenAfterBlink === true) {
|
||||
controller.classList.add("vsc-hidden");
|
||||
log("Controller auto-hidden after blink timeout", 5);
|
||||
} else {
|
||||
log("Controller kept visible", 5);
|
||||
}
|
||||
controller.restoreHiddenAfterBlink = false;
|
||||
controller.blinkTimeOut = undefined;
|
||||
}, numValue || 1000); // FIXED: Use numValue for consistency
|
||||
}, numValue || 1000);
|
||||
}
|
||||
break;
|
||||
case "drag":
|
||||
@@ -934,7 +1525,7 @@ function pause(v) {
|
||||
}
|
||||
|
||||
function resetSpeed(v, target, isFastKey = false) {
|
||||
const videoId = v.currentSrc || v.src || "default";
|
||||
const videoId = getVideoSourceKey(v);
|
||||
const currentSpeed = v.playbackRate;
|
||||
|
||||
if (isFastKey) {
|
||||
@@ -977,7 +1568,10 @@ function setMark(v) {
|
||||
v.vsc.mark = v.currentTime;
|
||||
}
|
||||
function jumpToMark(v) {
|
||||
if (v.vsc && typeof v.vsc.mark === "number") v.currentTime = v.vsc.mark;
|
||||
if (v.vsc && typeof v.vsc.mark === "number") {
|
||||
extendSpeedRestoreWindow(v);
|
||||
v.currentTime = v.vsc.mark;
|
||||
}
|
||||
}
|
||||
function handleDrag(video, e) {
|
||||
const c = video.vsc.div;
|
||||
@@ -1009,13 +1603,26 @@ function handleDrag(video, e) {
|
||||
pE.addEventListener("mouseleave", eD);
|
||||
pE.addEventListener("mousemove", sD);
|
||||
}
|
||||
var timer = null;
|
||||
function showController(controller) {
|
||||
function showController(controller, duration = 2000) {
|
||||
if (!controller || typeof controller.classList === "undefined") return;
|
||||
var restoreHidden =
|
||||
controller.restoreHiddenAfterShow === true ||
|
||||
controller.classList.contains("vsc-hidden");
|
||||
|
||||
controller.restoreHiddenAfterShow = restoreHidden;
|
||||
controller.classList.remove("vsc-hidden");
|
||||
controller.classList.add("vsc-show");
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(function () {
|
||||
|
||||
if (controller.showTimeOut !== undefined) {
|
||||
clearTimeout(controller.showTimeOut);
|
||||
}
|
||||
|
||||
controller.showTimeOut = setTimeout(function () {
|
||||
controller.classList.remove("vsc-show");
|
||||
timer = false;
|
||||
}, 2000);
|
||||
if (controller.restoreHiddenAfterShow === true) {
|
||||
controller.classList.add("vsc-hidden");
|
||||
}
|
||||
controller.restoreHiddenAfterShow = false;
|
||||
controller.showTimeOut = undefined;
|
||||
}, duration);
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "Video Speed Controller",
|
||||
"short_name": "videospeed",
|
||||
"version": "1.6.1",
|
||||
"version": "2.1.1",
|
||||
"manifest_version": 2,
|
||||
"description": "Speed up, slow down, advance and rewind HTML5 audio/video with shortcuts",
|
||||
"homepage_url": "https://github.com/SoPat712/videospeed",
|
||||
|
||||
+10
-1
@@ -20,6 +20,11 @@ h1 {
|
||||
font-size: 1.5em;
|
||||
margin: 21px 0 13px;
|
||||
}
|
||||
.version {
|
||||
margin: 0 0 12px;
|
||||
color: #6b6b6b;
|
||||
font-size: 0.95em;
|
||||
}
|
||||
h3 {
|
||||
font-size: 1.2em;
|
||||
margin-bottom: 0.8em;
|
||||
@@ -123,6 +128,10 @@ select {
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.version {
|
||||
color: #a8a8a8;
|
||||
}
|
||||
|
||||
header {
|
||||
border-bottom: 1px solid #333;
|
||||
background: linear-gradient(#1a1a1a, #1a1a1a 40%, rgba(26, 26, 26, 0.92));
|
||||
@@ -183,4 +192,4 @@ select {
|
||||
hr {
|
||||
border-color: #333;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<body>
|
||||
<header>
|
||||
<h1>Video Speed Controller</h1>
|
||||
<div class="version">Version <span id="app-version"></span></div>
|
||||
</header>
|
||||
|
||||
<section id="customs">
|
||||
|
||||
+420
-178
@@ -1,38 +1,9 @@
|
||||
var regStrip = /^[\r\t\f\v ]+|[\r\t\f\v ]+$/gm;
|
||||
|
||||
var tcDefaults = {
|
||||
speed: 1.0, // default:
|
||||
displayKeyCode: 86, // default: V
|
||||
rememberSpeed: false, // default: false
|
||||
audioBoolean: false, // default: false
|
||||
startHidden: false, // default: false
|
||||
forceLastSavedSpeed: false, //default: false
|
||||
enabled: true, // default enabled
|
||||
controllerOpacity: 0.3, // default: 0.3
|
||||
keyBindings: [
|
||||
{ action: "display", key: 86, value: 0, force: false, predefined: true }, // V
|
||||
{ action: "slower", key: 83, value: 0.1, force: false, predefined: true }, // S
|
||||
{ action: "faster", key: 68, value: 0.1, force: false, predefined: true }, // D
|
||||
{ action: "rewind", key: 90, value: 10, force: false, predefined: true }, // Z
|
||||
{ action: "advance", key: 88, value: 10, force: false, predefined: true }, // X
|
||||
{ action: "reset", key: 82, value: 1, force: false, predefined: true }, // R
|
||||
{ action: "fast", key: 71, value: 1.8, force: false, predefined: true } // G
|
||||
],
|
||||
blacklist: `www.instagram.com
|
||||
twitter.com
|
||||
imgur.com
|
||||
teams.microsoft.com
|
||||
`.replace(regStrip, ""),
|
||||
// ADDED: Nudge defaults
|
||||
enableSubtitleNudge: true,
|
||||
subtitleNudgeInterval: 25,
|
||||
subtitleNudgeAmount: 0.001
|
||||
};
|
||||
|
||||
var keyBindings = []; // This is populated during save/restore
|
||||
var keyBindings = [];
|
||||
|
||||
var keyCodeAliases = {
|
||||
/* ... same as your original ... */ 0: "null",
|
||||
0: "null",
|
||||
null: "null",
|
||||
undefined: "null",
|
||||
32: "Space",
|
||||
@@ -83,76 +54,347 @@ var keyCodeAliases = {
|
||||
173: "-"
|
||||
};
|
||||
|
||||
function recordKeyPress(e) {
|
||||
/* ... same as your original ... */
|
||||
var keyCodeToKey = {
|
||||
32: " ",
|
||||
37: "ArrowLeft",
|
||||
38: "ArrowUp",
|
||||
39: "ArrowRight",
|
||||
40: "ArrowDown",
|
||||
96: "0",
|
||||
97: "1",
|
||||
98: "2",
|
||||
99: "3",
|
||||
100: "4",
|
||||
101: "5",
|
||||
102: "6",
|
||||
103: "7",
|
||||
104: "8",
|
||||
105: "9",
|
||||
106: "*",
|
||||
107: "+",
|
||||
109: "-",
|
||||
110: ".",
|
||||
111: "/",
|
||||
112: "F1",
|
||||
113: "F2",
|
||||
114: "F3",
|
||||
115: "F4",
|
||||
116: "F5",
|
||||
117: "F6",
|
||||
118: "F7",
|
||||
119: "F8",
|
||||
120: "F9",
|
||||
121: "F10",
|
||||
122: "F11",
|
||||
123: "F12",
|
||||
186: ";",
|
||||
188: "<",
|
||||
189: "-",
|
||||
187: "+",
|
||||
190: ">",
|
||||
191: "/",
|
||||
192: "~",
|
||||
219: "[",
|
||||
220: "\\",
|
||||
221: "]",
|
||||
222: "'",
|
||||
59: ";",
|
||||
61: "+",
|
||||
173: "-"
|
||||
};
|
||||
|
||||
var modifierKeys = new Set([
|
||||
"Alt",
|
||||
"AltGraph",
|
||||
"Control",
|
||||
"Fn",
|
||||
"Hyper",
|
||||
"Meta",
|
||||
"OS",
|
||||
"Shift"
|
||||
]);
|
||||
|
||||
var displayKeyAliases = {
|
||||
" ": "Space",
|
||||
ArrowLeft: "Left",
|
||||
ArrowUp: "Up",
|
||||
ArrowRight: "Right",
|
||||
ArrowDown: "Down"
|
||||
};
|
||||
|
||||
function createDefaultBinding(action, key, keyCode, value) {
|
||||
return {
|
||||
action: action,
|
||||
key: key,
|
||||
keyCode: keyCode,
|
||||
value: value,
|
||||
force: false,
|
||||
predefined: true
|
||||
};
|
||||
}
|
||||
|
||||
var tcDefaults = {
|
||||
speed: 1.0,
|
||||
lastSpeed: 1.0,
|
||||
displayKeyCode: 86,
|
||||
rememberSpeed: false,
|
||||
audioBoolean: false,
|
||||
startHidden: false,
|
||||
forceLastSavedSpeed: false,
|
||||
enabled: true,
|
||||
controllerOpacity: 0.3,
|
||||
keyBindings: [
|
||||
createDefaultBinding("display", "V", 86, 0),
|
||||
createDefaultBinding("slower", "S", 83, 0.1),
|
||||
createDefaultBinding("faster", "D", 68, 0.1),
|
||||
createDefaultBinding("rewind", "Z", 90, 10),
|
||||
createDefaultBinding("advance", "X", 88, 10),
|
||||
createDefaultBinding("reset", "R", 82, 1),
|
||||
createDefaultBinding("fast", "G", 71, 1.8)
|
||||
],
|
||||
blacklist: `www.instagram.com
|
||||
twitter.com
|
||||
imgur.com
|
||||
teams.microsoft.com
|
||||
`.replace(regStrip, ""),
|
||||
enableSubtitleNudge: true,
|
||||
subtitleNudgeInterval: 25,
|
||||
subtitleNudgeAmount: 0.001
|
||||
};
|
||||
|
||||
var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"];
|
||||
|
||||
function normalizeBindingKey(key) {
|
||||
if (typeof key !== "string" || key.length === 0) return null;
|
||||
if (key === "Spacebar") return " ";
|
||||
if (key === "Esc") return "Escape";
|
||||
if (key.length === 1 && /[a-z]/i.test(key)) return key.toUpperCase();
|
||||
return key;
|
||||
}
|
||||
|
||||
function getLegacyKeyCode(binding) {
|
||||
if (!binding) return null;
|
||||
if (Number.isInteger(binding.keyCode)) return binding.keyCode;
|
||||
if (typeof binding.key === "number" && Number.isInteger(binding.key)) {
|
||||
return binding.key;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function legacyKeyCodeToBinding(keyCode) {
|
||||
if (!Number.isInteger(keyCode)) return null;
|
||||
var normalizedKey = keyCodeToKey[keyCode];
|
||||
if (!normalizedKey && keyCode >= 48 && keyCode <= 57) {
|
||||
normalizedKey = String.fromCharCode(keyCode);
|
||||
}
|
||||
if (!normalizedKey && keyCode >= 65 && keyCode <= 90) {
|
||||
normalizedKey = String.fromCharCode(keyCode);
|
||||
}
|
||||
return {
|
||||
key: normalizeBindingKey(normalizedKey),
|
||||
keyCode: keyCode,
|
||||
code: null,
|
||||
disabled: false
|
||||
};
|
||||
}
|
||||
|
||||
function createDisabledBinding() {
|
||||
return {
|
||||
key: null,
|
||||
keyCode: null,
|
||||
code: null,
|
||||
disabled: true
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeStoredBinding(binding, fallbackKeyCode) {
|
||||
var fallbackBinding = legacyKeyCodeToBinding(fallbackKeyCode);
|
||||
if (!binding) {
|
||||
return fallbackBinding;
|
||||
}
|
||||
|
||||
if (
|
||||
(e.keyCode >= 48 && e.keyCode <= 57) ||
|
||||
(e.keyCode >= 65 && e.keyCode <= 90) ||
|
||||
keyCodeAliases[e.keyCode]
|
||||
binding.disabled === true ||
|
||||
(binding.key === null &&
|
||||
binding.keyCode === null &&
|
||||
binding.code === null)
|
||||
) {
|
||||
e.target.value =
|
||||
keyCodeAliases[e.keyCode] || String.fromCharCode(e.keyCode);
|
||||
e.target.keyCode = e.keyCode;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
} else if (e.keyCode === 8) {
|
||||
e.target.value = "";
|
||||
} else if (e.keyCode === 27) {
|
||||
e.target.value = "null";
|
||||
e.target.keyCode = null;
|
||||
return createDisabledBinding();
|
||||
}
|
||||
|
||||
var normalized = {
|
||||
key: null,
|
||||
keyCode: null,
|
||||
code:
|
||||
typeof binding.code === "string" && binding.code.length > 0
|
||||
? binding.code
|
||||
: null,
|
||||
disabled: false
|
||||
};
|
||||
|
||||
if (typeof binding.key === "string") {
|
||||
normalized.key = normalizeBindingKey(binding.key);
|
||||
}
|
||||
|
||||
var legacyKeyCode = getLegacyKeyCode(binding);
|
||||
if (Number.isInteger(legacyKeyCode)) {
|
||||
var legacyBinding = legacyKeyCodeToBinding(legacyKeyCode);
|
||||
if (legacyBinding) {
|
||||
normalized.key = normalized.key || legacyBinding.key;
|
||||
normalized.keyCode = legacyKeyCode;
|
||||
}
|
||||
}
|
||||
|
||||
if (Number.isInteger(binding.keyCode)) {
|
||||
normalized.keyCode = binding.keyCode;
|
||||
}
|
||||
|
||||
if (!normalized.key && fallbackBinding) {
|
||||
normalized.key = fallbackBinding.key;
|
||||
if (normalized.keyCode === null) normalized.keyCode = fallbackBinding.keyCode;
|
||||
}
|
||||
|
||||
if (!normalized.key && !normalized.code && normalized.keyCode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function getBindingLabel(binding) {
|
||||
if (!binding) return "";
|
||||
if (binding.disabled) return "null";
|
||||
if (binding.key) {
|
||||
return displayKeyAliases[binding.key] || binding.key;
|
||||
}
|
||||
var legacyKeyCode = getLegacyKeyCode(binding);
|
||||
if (keyCodeAliases[legacyKeyCode]) return keyCodeAliases[legacyKeyCode];
|
||||
if (Number.isInteger(legacyKeyCode)) return String.fromCharCode(legacyKeyCode);
|
||||
return "";
|
||||
}
|
||||
|
||||
function setShortcutInputBinding(input, binding) {
|
||||
input.vscBinding = binding ? Object.assign({}, binding) : null;
|
||||
input.keyCode =
|
||||
binding && Number.isInteger(binding.keyCode) ? binding.keyCode : null;
|
||||
input.value = getBindingLabel(binding);
|
||||
}
|
||||
|
||||
function captureBindingFromEvent(event) {
|
||||
var normalizedKey = normalizeBindingKey(event.key);
|
||||
if (!normalizedKey || modifierKeys.has(normalizedKey)) return null;
|
||||
return {
|
||||
key: normalizedKey,
|
||||
keyCode: Number.isInteger(event.keyCode) ? event.keyCode : null,
|
||||
code: event.code || null,
|
||||
disabled: false
|
||||
};
|
||||
}
|
||||
|
||||
function recordKeyPress(event) {
|
||||
if (event.key === "Tab") return;
|
||||
|
||||
if (event.key === "Backspace") {
|
||||
setShortcutInputBinding(event.target, null);
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key === "Escape") {
|
||||
setShortcutInputBinding(event.target, createDisabledBinding());
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
|
||||
var binding = captureBindingFromEvent(event);
|
||||
if (!binding) return;
|
||||
|
||||
setShortcutInputBinding(event.target, binding);
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
function inputFilterNumbersOnly(event) {
|
||||
var char = String.fromCharCode(event.keyCode);
|
||||
if (
|
||||
!/[\d\.]$/.test(char) ||
|
||||
!/^\d+(\.\d*)?$/.test(event.target.value + char)
|
||||
) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
}
|
||||
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(event) {
|
||||
event.target.value = "";
|
||||
}
|
||||
|
||||
function inputBlur(event) {
|
||||
setShortcutInputBinding(event.target, event.target.vscBinding || null);
|
||||
}
|
||||
|
||||
function updateCustomShortcutInputText(inputItem, bindingOrKeyCode) {
|
||||
if (
|
||||
bindingOrKeyCode &&
|
||||
typeof bindingOrKeyCode === "object" &&
|
||||
!Array.isArray(bindingOrKeyCode)
|
||||
) {
|
||||
setShortcutInputBinding(inputItem, bindingOrKeyCode);
|
||||
return;
|
||||
}
|
||||
|
||||
setShortcutInputBinding(inputItem, legacyKeyCodeToBinding(bindingOrKeyCode));
|
||||
}
|
||||
function inputFocus(e) {
|
||||
/* ... same as your original ... */ e.target.value = "";
|
||||
}
|
||||
function inputBlur(e) {
|
||||
/* ... same as your original ... */ e.target.value =
|
||||
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 updateCustomShortcutInputText(inputItem, keyCode) {
|
||||
/* ... same as your original ... */ inputItem.value =
|
||||
keyCodeAliases[keyCode] || String.fromCharCode(keyCode);
|
||||
inputItem.keyCode = keyCode;
|
||||
}
|
||||
var customActionsNoValues = ["pause", "muted", "mark", "jump", "display"]; // Original
|
||||
|
||||
function add_shortcut() {
|
||||
/* ... same as your original ... */
|
||||
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>`;
|
||||
var div = document.createElement("div");
|
||||
div.setAttribute("class", "row customs");
|
||||
div.innerHTML = html;
|
||||
var customs_element = document.getElementById("customs");
|
||||
customs_element.insertBefore(
|
||||
var customsElement = document.getElementById("customs");
|
||||
customsElement.insertBefore(
|
||||
div,
|
||||
customs_element.children[customs_element.childElementCount - 1]
|
||||
customsElement.children[customsElement.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;
|
||||
var action = item.querySelector(".customDo").value;
|
||||
var input = item.querySelector(".customKey");
|
||||
var valueInput = item.querySelector(".customValue");
|
||||
var predefined = !!item.id;
|
||||
var fallbackKeyCode =
|
||||
predefined && action === "display"
|
||||
? tcDefaults.displayKeyCode
|
||||
: undefined;
|
||||
var binding = normalizeStoredBinding(input.vscBinding, fallbackKeyCode);
|
||||
|
||||
if (!binding) {
|
||||
return {
|
||||
valid: false,
|
||||
message: "Error: Shortcut for " + action + " is invalid. Unable to save"
|
||||
};
|
||||
}
|
||||
|
||||
keyBindings.push({
|
||||
action: action,
|
||||
key: key,
|
||||
value: value,
|
||||
force: force,
|
||||
key: binding.key,
|
||||
keyCode: binding.keyCode,
|
||||
code: binding.code,
|
||||
disabled: binding.disabled === true,
|
||||
value: customActionsNoValues.includes(action)
|
||||
? 0
|
||||
: Number(valueInput.value),
|
||||
force: item.querySelector(".customForce").value,
|
||||
predefined: predefined
|
||||
});
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
function validate() {
|
||||
/* ... same as your original ... */
|
||||
var valid = true;
|
||||
var status = document.getElementById("status");
|
||||
document
|
||||
@@ -174,45 +416,56 @@ function validate() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
// MODIFIED: save_options to include nudge settings
|
||||
function save_options() {
|
||||
if (validate() === false) return;
|
||||
|
||||
keyBindings = []; // Reset global keyBindings before populating from DOM
|
||||
Array.from(document.querySelectorAll(".customs")).forEach((item) =>
|
||||
createKeyBindings(item)
|
||||
);
|
||||
keyBindings = [];
|
||||
var status = document.getElementById("status");
|
||||
var saveError = null;
|
||||
|
||||
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
|
||||
Array.from(document.querySelectorAll(".customs")).forEach((item) => {
|
||||
if (saveError) return;
|
||||
var result = createKeyBindings(item);
|
||||
if (!result.valid) saveError = result.message;
|
||||
});
|
||||
|
||||
if (saveError) {
|
||||
status.textContent = saveError;
|
||||
return;
|
||||
}
|
||||
|
||||
var settings = {};
|
||||
settings.rememberSpeed = document.getElementById("rememberSpeed").checked;
|
||||
settings.forceLastSavedSpeed =
|
||||
document.getElementById("forceLastSavedSpeed").checked;
|
||||
settings.audioBoolean = document.getElementById("audioBoolean").checked;
|
||||
settings.enabled = document.getElementById("enabled").checked;
|
||||
settings.startHidden = document.getElementById("startHidden").checked;
|
||||
settings.controllerOpacity =
|
||||
document.getElementById("controllerOpacity").value;
|
||||
settings.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 =
|
||||
settings.keyBindings = keyBindings;
|
||||
settings.enableSubtitleNudge =
|
||||
document.getElementById("enableSubtitleNudge").checked;
|
||||
settings.subtitleNudgeInterval =
|
||||
parseInt(document.getElementById("subtitleNudgeInterval").value, 10) ||
|
||||
tcDefaults.subtitleNudgeInterval;
|
||||
s.subtitleNudgeAmount =
|
||||
settings.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)
|
||||
if (settings.subtitleNudgeInterval < 10) {
|
||||
settings.subtitleNudgeInterval = 10;
|
||||
}
|
||||
if (
|
||||
settings.subtitleNudgeAmount <= 0 ||
|
||||
settings.subtitleNudgeAmount > 0.1
|
||||
) {
|
||||
settings.subtitleNudgeAmount = tcDefaults.subtitleNudgeAmount;
|
||||
}
|
||||
|
||||
chrome.storage.sync.remove([
|
||||
"resetSpeed",
|
||||
"speedStep",
|
||||
@@ -227,8 +480,7 @@ function save_options() {
|
||||
"fastKeyCode"
|
||||
]);
|
||||
|
||||
chrome.storage.sync.set(s, function () {
|
||||
var status = document.getElementById("status");
|
||||
chrome.storage.sync.set(settings, function () {
|
||||
status.textContent = "Options saved";
|
||||
setTimeout(function () {
|
||||
status.textContent = "";
|
||||
@@ -236,7 +488,18 @@ function save_options() {
|
||||
});
|
||||
}
|
||||
|
||||
// MODIFIED: restore_options to include nudge settings
|
||||
function ensureDisplayBinding(storage) {
|
||||
if (storage.keyBindings.some((item) => item.action === "display")) return;
|
||||
storage.keyBindings.push(
|
||||
createDefaultBinding(
|
||||
"display",
|
||||
"V",
|
||||
storage.displayKeyCode || tcDefaults.displayKeyCode,
|
||||
0
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function restore_options() {
|
||||
chrome.storage.sync.get(tcDefaults, function (storage) {
|
||||
document.getElementById("rememberSpeed").checked = storage.rememberSpeed;
|
||||
@@ -248,8 +511,6 @@ function restore_options() {
|
||||
document.getElementById("controllerOpacity").value =
|
||||
storage.controllerOpacity;
|
||||
document.getElementById("blacklist").value = storage.blacklist;
|
||||
|
||||
// ADDED: Restore nudge settings
|
||||
document.getElementById("enableSubtitleNudge").checked =
|
||||
storage.enableSubtitleNudge;
|
||||
document.getElementById("subtitleNudgeInterval").value =
|
||||
@@ -257,78 +518,52 @@ function restore_options() {
|
||||
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,
|
||||
key: storage.displayKeyCode || tcDefaults.displayKeyCode
|
||||
});
|
||||
if (!Array.isArray(storage.keyBindings) || storage.keyBindings.length === 0) {
|
||||
storage.keyBindings = tcDefaults.keyBindings.slice();
|
||||
}
|
||||
|
||||
// Clear existing dynamic shortcuts before restoring (if any were added by mistake)
|
||||
const dynamicShortcuts = document.querySelectorAll(".customs:not([id])");
|
||||
dynamicShortcuts.forEach((sc) => sc.remove());
|
||||
ensureDisplayBinding(storage);
|
||||
|
||||
document.querySelectorAll(".customs:not([id])").forEach((row) => row.remove());
|
||||
|
||||
storage.keyBindings.forEach((item) => {
|
||||
var fallbackKeyCode =
|
||||
item.action === "display"
|
||||
? storage.displayKeyCode || tcDefaults.displayKeyCode
|
||||
: undefined;
|
||||
var normalizedBinding = normalizeStoredBinding(item, fallbackKeyCode);
|
||||
var row;
|
||||
|
||||
for (let i in storage.keyBindings) {
|
||||
var item = storage.keyBindings[i];
|
||||
if (item.predefined) {
|
||||
if (item["action"] == "display" && typeof item["key"] === "undefined") {
|
||||
item["key"] = storage.displayKeyCode || tcDefaults.displayKeyCode;
|
||||
}
|
||||
if (customActionsNoValues.includes(item["action"])) {
|
||||
const el = document.querySelector(
|
||||
"#" + item["action"] + " .customValue"
|
||||
);
|
||||
if (el) el.disabled = true;
|
||||
}
|
||||
const keyEl = document.querySelector(
|
||||
"#" + item["action"] + " .customKey"
|
||||
);
|
||||
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
|
||||
row = document.getElementById(item.action);
|
||||
} else {
|
||||
// Non-predefined, dynamically added shortcuts
|
||||
add_shortcut();
|
||||
const dom = document.querySelector(".customs:last-of-type"); // Gets the newly added one
|
||||
dom.querySelector(".customDo").value = 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 = String(item["force"]);
|
||||
row = document.querySelector(".customs:last-of-type");
|
||||
row.querySelector(".customDo").value = item.action;
|
||||
}
|
||||
}
|
||||
|
||||
if (!row) return;
|
||||
|
||||
var valueInput = row.querySelector(".customValue");
|
||||
if (customActionsNoValues.includes(item.action)) {
|
||||
valueInput.disabled = true;
|
||||
}
|
||||
|
||||
updateCustomShortcutInputText(
|
||||
row.querySelector(".customKey"),
|
||||
normalizedBinding || createDisabledBinding()
|
||||
);
|
||||
valueInput.value = item.value;
|
||||
row.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(); // This will populate based on tcDefaults
|
||||
restore_options();
|
||||
var status = document.getElementById("status");
|
||||
status.textContent = "Default options restored";
|
||||
setTimeout(function () {
|
||||
@@ -338,14 +573,18 @@ 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 ... */
|
||||
var manifest = chrome.runtime.getManifest();
|
||||
var versionElement = document.getElementById("app-version");
|
||||
if (versionElement) {
|
||||
versionElement.textContent = manifest.version;
|
||||
}
|
||||
|
||||
restore_options();
|
||||
document.getElementById("save").addEventListener("click", save_options);
|
||||
document.getElementById("add").addEventListener("click", add_shortcut);
|
||||
@@ -357,10 +596,12 @@ 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)
|
||||
);
|
||||
@@ -380,11 +621,12 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
);
|
||||
document.addEventListener("change", (event) => {
|
||||
eventCaller(event, "customDo", function () {
|
||||
var valueInput = event.target.nextElementSibling.nextElementSibling;
|
||||
if (customActionsNoValues.includes(event.target.value)) {
|
||||
event.target.nextElementSibling.nextElementSibling.disabled = true;
|
||||
event.target.nextElementSibling.nextElementSibling.value = 0; // Or "" if placeholder is preferred
|
||||
valueInput.disabled = true;
|
||||
valueInput.value = 0;
|
||||
} else {
|
||||
event.target.nextElementSibling.nextElementSibling.disabled = false;
|
||||
valueInput.disabled = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
body {
|
||||
min-width: 8em;
|
||||
background-color: white;
|
||||
color: #333;
|
||||
}
|
||||
body {
|
||||
min-width: 8em;
|
||||
background-color: white;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.version {
|
||||
margin-top: 0.7em;
|
||||
font-size: 0.85em;
|
||||
text-align: center;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
hr {
|
||||
width: 100%;
|
||||
@@ -62,7 +69,11 @@ button {
|
||||
background-image: linear-gradient(#353535, #353535 38%, #2a2a2a);
|
||||
}
|
||||
|
||||
#status {
|
||||
color: #ccc;
|
||||
}
|
||||
}
|
||||
#status {
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
.version {
|
||||
color: #aaa;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,5 +16,6 @@
|
||||
<hr />
|
||||
<button id="feedback" class="secondary">Send feedback</button>
|
||||
<button id="about" class="secondary">About</button>
|
||||
<div class="version">Version <span id="app-version"></span></div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
var manifest = chrome.runtime.getManifest();
|
||||
var versionElement = document.querySelector("#app-version");
|
||||
if (versionElement) {
|
||||
versionElement.innerText = manifest.version;
|
||||
}
|
||||
|
||||
document.querySelector("#config").addEventListener("click", function () {
|
||||
window.open(chrome.runtime.getURL("options.html"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user