mirror of
https://github.com/SoPat712/videospeed.git
synced 2025-08-22 02:18:45 -04:00
Fix issue where youtube resets speed when fullscreen (#447)
* Fix issue where youtube resets speed Track the speed of a video based on its source at time of last ratechange. Newly initialized videos with same source automatically will use the same speed as previous video with that source. * Make controller button press only affect that controller * Remove deprecated settings * Initialize speed on Play if not yet initialized
This commit is contained in:

committed by
Ilya Grigorik

parent
40be1c528d
commit
005fceac83
53
inject.js
53
inject.js
@@ -1,26 +1,7 @@
|
|||||||
var tc = {
|
var tc = {
|
||||||
settings: {
|
settings: {
|
||||||
speed: 1.0, // default 1x
|
speed: 1.0, // default 1x
|
||||||
|
speeds: {}, // empty object to hold speed for each source
|
||||||
/**
|
|
||||||
* these are not used and deprecated, will be removed in next update
|
|
||||||
* but should be stay there because chrome.storage.sync.get needs them
|
|
||||||
*/
|
|
||||||
resetSpeed: 1.0, // default 1.0
|
|
||||||
speedStep: null, // default 0.1x just for buttons
|
|
||||||
fastSpeed: null, // default 1.8x
|
|
||||||
rewindTime: null, // default 10s just for buttons
|
|
||||||
advanceTime: null, // default 10s just for buttons
|
|
||||||
resetKeyCode: null, // default: R
|
|
||||||
slowerKeyCode: null, // default: S
|
|
||||||
fasterKeyCode: null, // default: D
|
|
||||||
rewindKeyCode: null, // default: Z
|
|
||||||
advanceKeyCode: null, // default: X
|
|
||||||
fastKeyCode: null, // default: G
|
|
||||||
/**
|
|
||||||
* these(above) are not used and deprecated, will be removed in next update
|
|
||||||
* but should be stay there because chrome.storage.sync.get needs them.
|
|
||||||
*/
|
|
||||||
|
|
||||||
displayKeyCode: 86, // default: V
|
displayKeyCode: 86, // default: V
|
||||||
rememberSpeed: false, // default: false
|
rememberSpeed: false, // default: false
|
||||||
@@ -127,13 +108,25 @@
|
|||||||
this.document = target.ownerDocument;
|
this.document = target.ownerDocument;
|
||||||
this.id = Math.random().toString(36).substr(2, 9);
|
this.id = Math.random().toString(36).substr(2, 9);
|
||||||
if (!tc.settings.rememberSpeed) {
|
if (!tc.settings.rememberSpeed) {
|
||||||
tc.settings.speed = 1.0;
|
if (!tc.settings.speeds[target.src]) {
|
||||||
|
tc.settings.speeds[target.src] = 1.0;
|
||||||
|
}
|
||||||
setKeyBindings("reset", getKeyBindings("fast")); // resetSpeed = fastSpeed
|
setKeyBindings("reset", getKeyBindings("fast")); // resetSpeed = fastSpeed
|
||||||
|
} else{
|
||||||
|
tc.settings.speeds[target.src] = tc.settings.speed;
|
||||||
}
|
}
|
||||||
this.initializeControls();
|
this.initializeControls();
|
||||||
|
|
||||||
target.addEventListener('play', function(event) {
|
target.addEventListener('play', function(event) {
|
||||||
target.playbackRate = tc.settings.speed;
|
if (!tc.settings.rememberSpeed) {
|
||||||
|
if (!tc.settings.speeds[target.src]) {
|
||||||
|
tc.settings.speeds[target.src] = 1.0;
|
||||||
|
}
|
||||||
|
setKeyBindings("reset", getKeyBindings("fast")); // resetSpeed = fastSpeed
|
||||||
|
} else{
|
||||||
|
tc.settings.speeds[target.src] = tc.settings.speed;
|
||||||
|
}
|
||||||
|
target.playbackRate = tc.settings.speeds[target.src];
|
||||||
});
|
});
|
||||||
|
|
||||||
target.addEventListener('ratechange', function(event) {
|
target.addEventListener('ratechange', function(event) {
|
||||||
@@ -142,14 +135,14 @@
|
|||||||
if (event.target.readyState > 0) {
|
if (event.target.readyState > 0) {
|
||||||
var speed = this.getSpeed();
|
var speed = this.getSpeed();
|
||||||
this.speedIndicator.textContent = speed;
|
this.speedIndicator.textContent = speed;
|
||||||
tc.settings.speed = speed;
|
tc.settings.speeds[this.video.src] = speed;
|
||||||
chrome.storage.sync.set({'speed': speed}, function() {
|
chrome.storage.sync.set({'speed': speed}, function() {
|
||||||
console.log('Speed setting saved: ' + speed);
|
console.log('Speed setting saved: ' + speed);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
target.playbackRate = tc.settings.speed;
|
target.playbackRate = tc.settings.speeds[target.src];
|
||||||
};
|
};
|
||||||
|
|
||||||
tc.videoController.prototype.getSpeed = function() {
|
tc.videoController.prototype.getSpeed = function() {
|
||||||
@@ -162,7 +155,7 @@
|
|||||||
|
|
||||||
tc.videoController.prototype.initializeControls = function() {
|
tc.videoController.prototype.initializeControls = function() {
|
||||||
var document = this.document;
|
var document = this.document;
|
||||||
var speed = parseFloat(tc.settings.speed).toFixed(2),
|
var speed = parseFloat(tc.settings.speeds[this.video.src]).toFixed(2),
|
||||||
top = Math.max(this.video.offsetTop, 0) + "px",
|
top = Math.max(this.video.offsetTop, 0) + "px",
|
||||||
left = Math.max(this.video.offsetLeft, 0) + "px";
|
left = Math.max(this.video.offsetLeft, 0) + "px";
|
||||||
|
|
||||||
@@ -393,11 +386,19 @@
|
|||||||
function runAction(action, document, value, e) {
|
function runAction(action, document, value, e) {
|
||||||
var videoTags = document.getElementsByTagName('video');
|
var videoTags = document.getElementsByTagName('video');
|
||||||
videoTags.forEach = Array.prototype.forEach;
|
videoTags.forEach = Array.prototype.forEach;
|
||||||
|
// Get the controller that was used if called from a button press event e
|
||||||
|
if (e){
|
||||||
|
var targetController = e.target.getRootNode().host;
|
||||||
|
}
|
||||||
|
|
||||||
videoTags.forEach(function(v) {
|
videoTags.forEach(function(v) {
|
||||||
var id = v.dataset['vscid'];
|
var id = v.dataset['vscid'];
|
||||||
var controller = document.querySelector(`div[data-vscid="${id}"]`);
|
|
||||||
|
|
||||||
|
var controller = document.querySelector(`div[data-vscid="${id}"]`);
|
||||||
|
// Don't change video speed if the video has a different controller
|
||||||
|
if (e && !(targetController == controller)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
showController(controller);
|
showController(controller);
|
||||||
|
|
||||||
if (!v.classList.contains('vsc-cancelled')) {
|
if (!v.classList.contains('vsc-cancelled')) {
|
||||||
|
Reference in New Issue
Block a user