mirror of
https://github.com/SoPat712/videospeed.git
synced 2025-08-21 09:58:45 -04:00
Compare commits
11 Commits
73827b5ee0
...
v1.6.2
Author | SHA1 | Date | |
---|---|---|---|
![]() |
01b9b576eb | ||
![]() |
893c811802 | ||
![]() |
3fed3b425e | ||
![]() |
d89853b4d2 | ||
![]() |
d94ab958d5 | ||
![]() |
1277750716 | ||
![]() |
247a46d430 | ||
![]() |
703658335c | ||
![]() |
b2ed0fcb41 | ||
![]() |
3dfee251ec | ||
![]() |
8e0183d8af |
118
build.py
Normal file
118
build.py
Normal file
@@ -0,0 +1,118 @@
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import zipfile
|
||||
|
||||
SCRIPT_NAME = os.path.basename(__file__)
|
||||
TARGET_FILE = "manifest.json"
|
||||
|
||||
|
||||
def zip_folder(output_name, folder, exclude_files, exclude_dirs):
|
||||
with zipfile.ZipFile(output_name, "w", zipfile.ZIP_DEFLATED) as zipf:
|
||||
for root, dirs, files in os.walk(folder):
|
||||
dirs[:] = [
|
||||
d
|
||||
for d in dirs
|
||||
if os.path.relpath(os.path.join(root, d), folder) not in exclude_dirs
|
||||
]
|
||||
for file in files:
|
||||
rel_path = os.path.relpath(os.path.join(root, file), folder)
|
||||
if (
|
||||
file in exclude_files
|
||||
or rel_path in exclude_files
|
||||
or any(rel_path.startswith(ed + os.sep) for ed in exclude_dirs)
|
||||
):
|
||||
continue
|
||||
zipf.write(os.path.join(root, file), arcname=rel_path)
|
||||
|
||||
|
||||
def update_version_line(file_path, new_version):
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
updated = False
|
||||
for i, line in enumerate(lines):
|
||||
match = re.match(r'\s*"version":\s*"([^"]+)"', line)
|
||||
if match:
|
||||
old_version = match.group(1)
|
||||
lines[i] = re.sub(
|
||||
r'"version":\s*".+?"', f'"version": "{new_version}"', line
|
||||
)
|
||||
updated = True
|
||||
print(
|
||||
f"🛠️ Changed version in {file_path} from {old_version} ➜ {new_version}"
|
||||
)
|
||||
break
|
||||
|
||||
if updated:
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.writelines(lines)
|
||||
else:
|
||||
print(f"⚠️ No version line found in {file_path}.")
|
||||
|
||||
|
||||
def main():
|
||||
# Step 0: Remove all existing .xpi files upfront
|
||||
xpi_files = glob.glob("*.xpi")
|
||||
for f in xpi_files:
|
||||
try:
|
||||
os.remove(f)
|
||||
print(f"🗑️ Removed existing archive: {f}")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Failed to remove {f}: {e}")
|
||||
|
||||
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):
|
||||
update_version_line(manifest_path, base_version)
|
||||
else:
|
||||
print(f"❌ {TARGET_FILE} not found. Aborting.")
|
||||
return
|
||||
|
||||
# Step 2: Create videospeed-github.xpi (exclude script, .git, AND videospeed-github.xpi itself)
|
||||
exclude_files = [SCRIPT_NAME, "videospeed-github.xpi"]
|
||||
exclude_dirs = [".git"]
|
||||
zip_folder("videospeed-github.xpi", current_dir, exclude_files, exclude_dirs)
|
||||
print("✅ Created videospeed-github.xpi")
|
||||
|
||||
# Step 3: Re-scan for .xpi files after GitHub archive creation, exclude them for Firefox zip
|
||||
current_xpi_files = set(glob.glob("*.xpi"))
|
||||
exclude_temp_files = current_xpi_files.union({SCRIPT_NAME})
|
||||
exclude_temp_dirs = set(exclude_dirs)
|
||||
|
||||
# Step 4: Create videospeed-firefox.xpi from temp folder with version bumped to .0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
for item in os.listdir(current_dir):
|
||||
if item in exclude_temp_files or item in exclude_temp_dirs:
|
||||
continue
|
||||
src = os.path.join(current_dir, item)
|
||||
dst = os.path.join(temp_dir, item)
|
||||
if os.path.isdir(src):
|
||||
shutil.copytree(src, dst)
|
||||
else:
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
temp_manifest = os.path.join(temp_dir, TARGET_FILE)
|
||||
if os.path.exists(temp_manifest):
|
||||
update_version_line(temp_manifest, firefox_version)
|
||||
else:
|
||||
print(f"⚠️ {TARGET_FILE} not found in temp folder.")
|
||||
|
||||
zip_folder(
|
||||
"videospeed-firefox.xpi", temp_dir, exclude_files=[], exclude_dirs=[]
|
||||
)
|
||||
print("✅ Created videospeed-firefox.xpi")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
301
inject.js
301
inject.js
@@ -1,5 +1,8 @@
|
||||
var regStrip = /^[\r\t\f\v ]+|[\r\t\f\v ]+$/gm;
|
||||
|
||||
var isUserSeek = false; // Track if seek was user-initiated
|
||||
var lastToggleSpeed = {}; // Store last toggle speeds per video
|
||||
|
||||
var tc = {
|
||||
settings: {
|
||||
lastSpeed: 1.0,
|
||||
@@ -134,6 +137,31 @@ chrome.storage.sync.get(tc.settings, function (storage) {
|
||||
predefined: true
|
||||
});
|
||||
}
|
||||
// Add a listener for messages from the popup.
|
||||
// We use a global flag to ensure the listener is only attached once.
|
||||
if (!window.vscMessageListener) {
|
||||
chrome.runtime.onMessage.addListener(
|
||||
function (request, sender, sendResponse) {
|
||||
// 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);
|
||||
|
||||
// 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" });
|
||||
}
|
||||
|
||||
// Required to allow for asynchronous responses.
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
// Set the flag to prevent adding the listener again.
|
||||
window.vscMessageListener = true;
|
||||
}
|
||||
initializeWhenReady(document);
|
||||
});
|
||||
|
||||
@@ -157,36 +185,80 @@ function defineVideoController() {
|
||||
this.video = target;
|
||||
this.parent = target.parentElement || parent;
|
||||
this.nudgeIntervalId = null;
|
||||
|
||||
// Determine what speed to use
|
||||
let storedSpeed = tc.settings.speeds[target.currentSrc];
|
||||
if (!tc.settings.rememberSpeed) {
|
||||
if (!storedSpeed) {
|
||||
storedSpeed = 1.0;
|
||||
}
|
||||
setKeyBindings("reset", getKeyBindings("fast"));
|
||||
} else {
|
||||
storedSpeed = tc.settings.lastSpeed;
|
||||
}
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
storedSpeed = tc.settings.lastSpeed;
|
||||
}
|
||||
target.playbackRate = storedSpeed;
|
||||
this.div = this.initializeControls();
|
||||
var mediaEventAction = function (event) {
|
||||
let storedSpeed = tc.settings.speeds[event.target.currentSrc];
|
||||
if (!tc.settings.rememberSpeed) {
|
||||
if (!storedSpeed) {
|
||||
storedSpeed = 1.0;
|
||||
}
|
||||
setKeyBindings("reset", getKeyBindings("fast"));
|
||||
} else {
|
||||
storedSpeed = tc.settings.lastSpeed;
|
||||
|
||||
// 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);
|
||||
}
|
||||
if (tc.settings.forceLastSavedSpeed) storedSpeed = tc.settings.lastSpeed;
|
||||
setSpeed(event.target, storedSpeed);
|
||||
if (event.type === "play") this.startSubtitleNudge();
|
||||
else if (event.type === "pause" || event.type === "ended")
|
||||
}, 0);
|
||||
|
||||
this.div = this.initializeControls();
|
||||
|
||||
// Make the controller visible for 5 seconds on startup
|
||||
runAction("blink", 5000, null, this.video);
|
||||
|
||||
// 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();
|
||||
|
||||
// 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";
|
||||
|
||||
// Get the expected speed based on settings
|
||||
let expectedSpeed;
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
expectedSpeed = tc.settings.lastSpeed;
|
||||
} else {
|
||||
expectedSpeed = tc.settings.speeds[videoId] || tc.settings.lastSpeed;
|
||||
}
|
||||
|
||||
// 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") {
|
||||
this.stopSubtitleNudge();
|
||||
tc.isNudging = false;
|
||||
}
|
||||
|
||||
// For seek events, don't mess with speed
|
||||
if (event.type === "seeked" && isUserSeek) {
|
||||
isUserSeek = false;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
target.addEventListener(
|
||||
"play",
|
||||
(this.handlePlay = mediaEventAction.bind(this))
|
||||
@@ -203,6 +275,32 @@ function defineVideoController() {
|
||||
"seeked",
|
||||
(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 (
|
||||
@@ -216,6 +314,19 @@ function defineVideoController() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -241,31 +352,7 @@ function defineVideoController() {
|
||||
if (idx != -1) tc.mediaElements.splice(idx, 1);
|
||||
};
|
||||
|
||||
// MODIFIED: Using your debug-enhanced startSubtitleNudge function
|
||||
tc.videoController.prototype.startSubtitleNudge = function () {
|
||||
console.log("[VSC DEBUG] startSubtitleNudge called");
|
||||
console.log("[VSC DEBUG] location.hostname:", location.hostname);
|
||||
console.log(
|
||||
"[VSC DEBUG] enableSubtitleNudge:",
|
||||
tc.settings.enableSubtitleNudge
|
||||
);
|
||||
console.log("[VSC DEBUG] video element:", this.video);
|
||||
console.log(
|
||||
"[VSC DEBUG] video src:",
|
||||
this.video ? this.video.src : "no video"
|
||||
);
|
||||
console.log(
|
||||
"[VSC DEBUG] video currentSrc:",
|
||||
this.video ? this.video.currentSrc : "no video"
|
||||
);
|
||||
console.log(
|
||||
"[VSC DEBUG] video paused:",
|
||||
this.video ? this.video.paused : "no video"
|
||||
);
|
||||
console.log(
|
||||
"[VSC DEBUG] video playbackRate:",
|
||||
this.video ? this.video.playbackRate : "no video"
|
||||
);
|
||||
const isYouTube =
|
||||
(this.video &&
|
||||
this.video.currentSrc &&
|
||||
@@ -277,30 +364,33 @@ function defineVideoController() {
|
||||
this.nudgeIntervalId !== null ||
|
||||
!this.video
|
||||
) {
|
||||
console.log("[VSC DEBUG] Nudge blocked - reasons:", {
|
||||
enableSubtitleNudge: tc.settings.enableSubtitleNudge,
|
||||
nudgeIntervalId: this.nudgeIntervalId,
|
||||
hasVideo: !!this.video
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (this.video.paused || this.video.playbackRate === 1.0) {
|
||||
console.log("[VSC DEBUG] Nudge stopped - video paused or 1.0x speed");
|
||||
this.stopSubtitleNudge();
|
||||
return;
|
||||
}
|
||||
console.log("[VSC DEBUG] Starting nudge interval");
|
||||
// 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
|
||||
) {
|
||||
this.stopSubtitleNudge();
|
||||
return;
|
||||
}
|
||||
// Double-check pause state before nudging
|
||||
if (this.video.paused) {
|
||||
this.stopSubtitleNudge();
|
||||
return;
|
||||
}
|
||||
const currentRate = this.video.playbackRate;
|
||||
const nudgeAmount = tc.settings.subtitleNudgeAmount;
|
||||
tc.isNudging = true;
|
||||
@@ -329,8 +419,15 @@ function defineVideoController() {
|
||||
tc.videoController.prototype.initializeControls = function () {
|
||||
const doc = this.video.ownerDocument;
|
||||
const speed = this.video.playbackRate.toFixed(2);
|
||||
var top = Math.max(this.video.offsetTop, 0) + "px",
|
||||
// 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 wrapper = doc.createElement("div");
|
||||
wrapper.classList.add("vsc-controller");
|
||||
if (!this.video.src && !this.video.currentSrc)
|
||||
@@ -345,8 +442,7 @@ function defineVideoController() {
|
||||
runAction(
|
||||
e.target.dataset["action"],
|
||||
getKeyBindings(e.target.dataset["action"], "value"),
|
||||
e,
|
||||
this.video
|
||||
e
|
||||
);
|
||||
e.stopPropagation();
|
||||
},
|
||||
@@ -359,8 +455,7 @@ function defineVideoController() {
|
||||
runAction(
|
||||
e.target.dataset["action"],
|
||||
getKeyBindings(e.target.dataset["action"]),
|
||||
e,
|
||||
this.video
|
||||
e
|
||||
);
|
||||
e.stopPropagation();
|
||||
},
|
||||
@@ -397,7 +492,7 @@ function defineVideoController() {
|
||||
const r = parentEl.getRootNode();
|
||||
const s = r && r.querySelector ? r.querySelector(".scrim") : null;
|
||||
if (s) s.prepend(fragment);
|
||||
else parentEl.insertBefore(fragment, pEl.firstChild);
|
||||
else parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
break;
|
||||
default:
|
||||
parentEl.insertBefore(fragment, parentEl.firstChild);
|
||||
@@ -450,7 +545,7 @@ function setupListener() {
|
||||
tc.settings.lastSpeed = speed;
|
||||
chrome.storage.sync.set({ lastSpeed: speed }, () => {});
|
||||
if (fromUserInput) {
|
||||
runAction("blink", getKeyBindings("blink", "value") || 1000, null, video);
|
||||
runAction("blink", 1000, null, video);
|
||||
}
|
||||
if (video.vsc) {
|
||||
if (speed === 1.0 || video.paused) video.vsc.stopSubtitleNudge();
|
||||
@@ -522,12 +617,7 @@ function getShadow(parent) {
|
||||
return r;
|
||||
}
|
||||
|
||||
// MODIFIED: Replaced with your debug-enhanced initializeNow
|
||||
function initializeNow(doc) {
|
||||
console.log(
|
||||
"[VSC DEBUG] initializeNow called for:",
|
||||
doc.location ? doc.location.hostname : "unknown doc"
|
||||
);
|
||||
if (vscInitializedDocuments.has(doc) || !doc.body) return;
|
||||
if (!tc.settings.enabled) return;
|
||||
if (!doc.body.classList.contains("vsc-initialized"))
|
||||
@@ -535,7 +625,6 @@ function initializeNow(doc) {
|
||||
if (typeof tc.videoController === "undefined") defineVideoController();
|
||||
setupListener();
|
||||
|
||||
// Re-inserting original keydown listener logic from your codebase
|
||||
var docs = Array(doc);
|
||||
try {
|
||||
if (inIframe()) docs.push(window.top.document);
|
||||
@@ -578,7 +667,6 @@ function initializeNow(doc) {
|
||||
d.vscKeydownListenerAttached = true;
|
||||
});
|
||||
|
||||
// Original MutationObserver logic
|
||||
if (!doc.vscMutationObserverAttached) {
|
||||
const observer = new MutationObserver(function (mutations) {
|
||||
requestIdleCallback(
|
||||
@@ -652,31 +740,19 @@ function initializeNow(doc) {
|
||||
|
||||
const q = tc.settings.audioBoolean ? "video,audio" : "video";
|
||||
const foundVideos = doc.querySelectorAll(q);
|
||||
console.log(
|
||||
"[VSC DEBUG] Found videos:",
|
||||
foundVideos.length,
|
||||
"in doc:",
|
||||
doc.location ? doc.location.hostname : "unknown"
|
||||
);
|
||||
foundVideos.forEach((v) => {
|
||||
if (!v.vsc) new tc.videoController(v, v.parentElement);
|
||||
});
|
||||
|
||||
// Your enhanced iframe handling
|
||||
Array.from(doc.getElementsByTagName("iframe")).forEach((f) => {
|
||||
console.log("[VSC DEBUG] Found iframe:", f.src);
|
||||
if (f.vscLoadListenerAttached) return; // Prevent attaching multiple load listeners
|
||||
if (f.vscLoadListenerAttached) return;
|
||||
f.addEventListener("load", () => {
|
||||
console.log("[VSC DEBUG] Iframe loaded, attempting to access");
|
||||
try {
|
||||
if (f.contentDocument) {
|
||||
initializeWhenReady(f.contentDocument);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(
|
||||
"[VSC DEBUG] Still cannot access iframe after load:",
|
||||
e.message
|
||||
);
|
||||
// Silently ignore CORS errors
|
||||
}
|
||||
});
|
||||
f.vscLoadListenerAttached = true;
|
||||
@@ -685,13 +761,12 @@ function initializeNow(doc) {
|
||||
initializeWhenReady(f.contentDocument);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[VSC DEBUG] Error accessing iframe immediately:", e.message);
|
||||
// Silently ignore CORS errors
|
||||
}
|
||||
});
|
||||
vscInitializedDocuments.add(doc);
|
||||
}
|
||||
|
||||
// MODIFIED: setSpeed now takes isUserKeyPress for blink logic
|
||||
function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
const numericSpeed = Number(speed);
|
||||
if (isNaN(numericSpeed) || numericSpeed <= 0 || numericSpeed > 16) return;
|
||||
@@ -705,7 +780,7 @@ function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
video.vsc.speedIndicator.textContent = numericSpeed.toFixed(2);
|
||||
|
||||
if (isUserKeyPress && !isInitialCall && video.vsc && video.vsc.div) {
|
||||
runAction("blink", null, null, video); // Pass video to blink
|
||||
runAction("blink", 1000, null, video); // Pass video to blink
|
||||
}
|
||||
|
||||
if (tc.settings.forceLastSavedSpeed) {
|
||||
@@ -733,11 +808,14 @@ function setSpeed(video, speed, isInitialCall = false, isUserKeyPress = false) {
|
||||
}
|
||||
}
|
||||
|
||||
// MODIFIED: runAction is now context-aware and calls the new simpler resetSpeed
|
||||
function runAction(action, value, e) {
|
||||
log("runAction Begin", 5);
|
||||
var mediaTagsToProcess;
|
||||
if (e && e.target && e.target.getRootNode) {
|
||||
const specificVideo = arguments[3] || null;
|
||||
|
||||
if (specificVideo) {
|
||||
mediaTagsToProcess = [specificVideo];
|
||||
} else if (e && e.target && e.target.getRootNode) {
|
||||
// Event-driven action
|
||||
const docContext = e.target.ownerDocument || document;
|
||||
mediaTagsToProcess = tc.mediaElements.filter(
|
||||
@@ -746,20 +824,18 @@ function runAction(action, value, e) {
|
||||
const targetController = e.target.getRootNode().host;
|
||||
if (targetController) {
|
||||
// If it's a click on a controller, only use that one video
|
||||
const specificVideo = tc.mediaElements.find(
|
||||
const videoFromController = tc.mediaElements.find(
|
||||
(v) => v.vsc && v.vsc.div === targetController
|
||||
);
|
||||
if (specificVideo) mediaTagsToProcess = [specificVideo];
|
||||
if (videoFromController) mediaTagsToProcess = [videoFromController];
|
||||
}
|
||||
} else {
|
||||
// No event context (e.g., internal blink call) or a passed specificVideo
|
||||
const specificVideo = arguments[3] || null; // The optional 4th argument
|
||||
if (specificVideo) mediaTagsToProcess = [specificVideo];
|
||||
else mediaTagsToProcess = tc.mediaElements;
|
||||
mediaTagsToProcess = tc.mediaElements;
|
||||
}
|
||||
if (mediaTagsToProcess.length === 0 && action !== "display") return;
|
||||
|
||||
mediaTagsToProcess.forEach(function (v) {
|
||||
if (!v.vsc) return; // Don't process videos without a controller
|
||||
var controller = v.vsc.div;
|
||||
const userDrivenActionsThatShowController = [
|
||||
"rewind",
|
||||
@@ -781,9 +857,11 @@ function runAction(action, value, e) {
|
||||
const numValue = parseFloat(value);
|
||||
switch (action) {
|
||||
case "rewind":
|
||||
isUserSeek = true;
|
||||
v.currentTime -= numValue;
|
||||
break;
|
||||
case "advance":
|
||||
isUserSeek = true;
|
||||
v.currentTime += numValue;
|
||||
break;
|
||||
case "faster":
|
||||
@@ -801,11 +879,11 @@ function runAction(action, value, e) {
|
||||
setSpeed(v, Math.max(v.playbackRate - numValue, 0.07), false, true);
|
||||
break;
|
||||
case "reset":
|
||||
resetSpeed(v, 1.0);
|
||||
break; // Use new simpler resetSpeed
|
||||
resetSpeed(v, 1.0, false); // Use enhanced resetSpeed
|
||||
break;
|
||||
case "fast":
|
||||
resetSpeed(v, numValue, true);
|
||||
break; // Use new simpler resetSpeed
|
||||
resetSpeed(v, numValue, true); // Use enhanced resetSpeed
|
||||
break;
|
||||
case "display":
|
||||
controller.classList.add("vsc-manual");
|
||||
controller.classList.toggle("vsc-hidden");
|
||||
@@ -827,7 +905,7 @@ function runAction(action, value, e) {
|
||||
controller.classList.add("vsc-hidden");
|
||||
}
|
||||
controller.blinkTimeOut = undefined;
|
||||
}, value || 1000);
|
||||
}, numValue || 1000); // FIXED: Use numValue for consistency
|
||||
}
|
||||
break;
|
||||
case "drag":
|
||||
@@ -855,22 +933,39 @@ function pause(v) {
|
||||
else v.pause();
|
||||
}
|
||||
|
||||
// MODIFIED: Replaced with new, simpler resetSpeed function
|
||||
function resetSpeed(v, target, isFastKey = false) {
|
||||
const fastSpeed = getKeyBindings("fast", "value") || 1.8;
|
||||
const videoId = v.currentSrc || v.src || "default";
|
||||
const currentSpeed = v.playbackRate;
|
||||
|
||||
if (isFastKey) {
|
||||
// Called by 'fast' action
|
||||
if (Math.abs(v.playbackRate - target) < 0.01) {
|
||||
setSpeed(v, 1.0, false, true); // Toggle to 1.0
|
||||
// G key: Toggle between current speed and preferred speed (e.g., 1.8)
|
||||
const preferredSpeed = target;
|
||||
const lastToggle = lastToggleSpeed[videoId] || currentSpeed;
|
||||
|
||||
if (Math.abs(currentSpeed - preferredSpeed) < 0.01) {
|
||||
// Currently at preferred speed, toggle to the last speed
|
||||
setSpeed(v, lastToggle, false, true);
|
||||
} else {
|
||||
setSpeed(v, target, false, true); // Set to preferred speed
|
||||
// Not at preferred speed, save current as toggle speed and go to preferred
|
||||
lastToggleSpeed[videoId] = currentSpeed;
|
||||
setSpeed(v, preferredSpeed, false, true);
|
||||
}
|
||||
} else {
|
||||
// Called by 'reset' action
|
||||
if (Math.abs(v.playbackRate - 1.0) < 0.01) {
|
||||
setSpeed(v, fastSpeed, false, true); // Toggle to fast speed
|
||||
// R key: Toggle between current speed and 1.0
|
||||
const resetSpeedValue = 1.0;
|
||||
const lastToggle = lastToggleSpeed[videoId] || currentSpeed;
|
||||
|
||||
if (Math.abs(currentSpeed - resetSpeedValue) < 0.01) {
|
||||
// Currently at 1.0, toggle to the last speed (or 1.8 if no history)
|
||||
const speedToRestore =
|
||||
Math.abs(lastToggle - 1.0) < 0.01
|
||||
? getKeyBindings("fast") || 1.8
|
||||
: lastToggle;
|
||||
setSpeed(v, speedToRestore, false, true);
|
||||
} else {
|
||||
setSpeed(v, 1.0, false, true); // Set to 1.0
|
||||
// Not at 1.0, save current as toggle speed and go to 1.0
|
||||
lastToggleSpeed[videoId] = currentSpeed;
|
||||
setSpeed(v, resetSpeedValue, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -885,7 +980,6 @@ function jumpToMark(v) {
|
||||
if (v.vsc && typeof v.vsc.mark === "number") v.currentTime = v.vsc.mark;
|
||||
}
|
||||
function handleDrag(video, e) {
|
||||
/* ... Same original logic ... */
|
||||
const c = video.vsc.div;
|
||||
const sC = c.shadowRoot.querySelector("#controller");
|
||||
var pE = c.parentElement;
|
||||
@@ -917,9 +1011,8 @@ function handleDrag(video, e) {
|
||||
}
|
||||
var timer = null;
|
||||
function showController(controller) {
|
||||
/* ... Same original logic ... */
|
||||
if (!controller || typeof controller.classList === "undefined") return;
|
||||
controller.classList.add("vcs-show");
|
||||
controller.classList.add("vsc-show");
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(function () {
|
||||
controller.classList.remove("vsc-show");
|
||||
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "Video Speed Controller",
|
||||
"short_name": "videospeed",
|
||||
"version": "1.1.3",
|
||||
"version": "1.6.1",
|
||||
"manifest_version": 2,
|
||||
"description": "Speed up, slow down, advance and rewind HTML5 audio/video with shortcuts",
|
||||
"homepage_url": "https://github.com/SoPat712/videospeed",
|
||||
|
74
options.css
74
options.css
@@ -5,6 +5,7 @@ body {
|
||||
font-family: sans-serif;
|
||||
font-size: 12px;
|
||||
color: rgb(48, 57, 66);
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
h1,
|
||||
@@ -110,3 +111,76 @@ select {
|
||||
color: transparent;
|
||||
text-shadow: 0 0 0 #000000;
|
||||
}
|
||||
|
||||
/* Dark mode styles */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
body {
|
||||
background-color: #1a1a1a;
|
||||
color: #e0e0e0;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
header {
|
||||
border-bottom: 1px solid #333;
|
||||
background: linear-gradient(#1a1a1a, #1a1a1a 40%, rgba(26, 26, 26, 0.92));
|
||||
}
|
||||
|
||||
button {
|
||||
background-image: linear-gradient(#404040, #404040 38%, #353535);
|
||||
border: 1px solid rgba(255, 255, 255, 0.25);
|
||||
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08),
|
||||
inset 0 1px 2px rgba(0, 0, 0, 0.75);
|
||||
color: #e0e0e0;
|
||||
text-shadow: 0 1px 0 rgb(20, 20, 20);
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-image: linear-gradient(#4a4a4a, #4a4a4a 38%, #3f3f3f);
|
||||
}
|
||||
|
||||
input[type="text"],
|
||||
input[type="checkbox"],
|
||||
select,
|
||||
textarea {
|
||||
background-color: #2a2a2a;
|
||||
color: #e0e0e0;
|
||||
border: 1px solid #555;
|
||||
}
|
||||
|
||||
input[type="text"]:focus,
|
||||
select:focus,
|
||||
textarea:focus {
|
||||
background-color: #333;
|
||||
border-color: #777;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
select option {
|
||||
background-color: #2a2a2a;
|
||||
color: #e0e0e0;
|
||||
}
|
||||
|
||||
.customKey {
|
||||
color: transparent;
|
||||
text-shadow: 0 0 0 #e0e0e0;
|
||||
}
|
||||
|
||||
#status {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #66b3ff;
|
||||
}
|
||||
|
||||
a:visited {
|
||||
color: #cc99ff;
|
||||
}
|
||||
|
||||
hr {
|
||||
border-color: #333;
|
||||
}
|
||||
}
|
34
popup.css
34
popup.css
@@ -1,5 +1,7 @@
|
||||
body {
|
||||
min-width: 8em;
|
||||
background-color: white;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
hr {
|
||||
@@ -32,3 +34,35 @@ button {
|
||||
.hide {
|
||||
display: none;
|
||||
}
|
||||
/* Dark mode styles */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
body {
|
||||
background-color: #1a1a1a;
|
||||
color: #e0e0e0;
|
||||
}
|
||||
|
||||
hr {
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
button {
|
||||
background-image: linear-gradient(#404040, #404040 38%, #353535);
|
||||
border: 1px solid rgba(255, 255, 255, 0.25);
|
||||
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08),
|
||||
inset 0 1px 2px rgba(0, 0, 0, 0.75);
|
||||
color: #e0e0e0;
|
||||
text-shadow: 0 1px 0 rgb(20, 20, 20);
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-image: linear-gradient(#4a4a4a, #4a4a4a 38%, #3f3f3f);
|
||||
}
|
||||
|
||||
button:active {
|
||||
background-image: linear-gradient(#353535, #353535 38%, #2a2a2a);
|
||||
}
|
||||
|
||||
#status {
|
||||
color: #ccc;
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Video Speed Controller: Popup</title>
|
||||
@@ -6,6 +6,8 @@
|
||||
<script src="popup.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<button id="refresh">Re-scan Page for Videos</button>
|
||||
<hr />
|
||||
<button id="enable" class="hide">Enable</button>
|
||||
<button id="disable">Disable</button>
|
||||
<span id="status" class="hide"></span>
|
||||
|
31
popup.js
31
popup.js
@@ -19,6 +19,31 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
toggleEnabled(false, settingsSavedReloadMessage);
|
||||
});
|
||||
|
||||
// --- REVISED: "Re-scan" button functionality ---
|
||||
document.querySelector("#refresh").addEventListener("click", function () {
|
||||
setStatusMessage("Re-scanning page...");
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
||||
if (tabs[0] && tabs[0].id) {
|
||||
// Send a message to the content script, asking it to re-initialize.
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{ action: "rescan_page" },
|
||||
function (response) {
|
||||
if (chrome.runtime.lastError) {
|
||||
// This error is expected on pages where content scripts cannot run.
|
||||
setStatusMessage("Cannot run on this page.");
|
||||
} else if (response && response.status === "complete") {
|
||||
setStatusMessage("Scan complete. Closing...");
|
||||
setTimeout(() => window.close(), 500); // Close popup on success.
|
||||
} else {
|
||||
setStatusMessage("Scan failed. Please reload the page.");
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
chrome.storage.sync.get({ enabled: true }, function (storage) {
|
||||
toggleEnabledUI(storage.enabled);
|
||||
});
|
||||
@@ -42,9 +67,9 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
const suffix = `${enabled ? "" : "_disabled"}.png`;
|
||||
chrome.browserAction.setIcon({
|
||||
path: {
|
||||
"19": "icons/icon19" + suffix,
|
||||
"38": "icons/icon38" + suffix,
|
||||
"48": "icons/icon48" + suffix
|
||||
19: "icons/icon19" + suffix,
|
||||
38: "icons/icon38" + suffix,
|
||||
48: "icons/icon48" + suffix
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user