Bug 1768201 - add seek forward and backward key shortcuts for pip on netflix. r=pip-reviewers,mhowell

Differential Revision: https://phabricator.services.mozilla.com/D145840
This commit is contained in:
Katherine Patenio 2022-05-09 18:30:47 +00:00
parent eeb4a961fa
commit 5e87539c25
3 changed files with 38 additions and 2 deletions

View File

@ -12,13 +12,14 @@ let AVAILABLE_PIP_OVERRIDES;
// See PictureInPictureControls.jsm for these values.
// eslint-disable-next-line no-unused-vars
const TOGGLE_POLICIES = browser.pictureInPictureChild.getPolicies();
const KEYBOARD_CONTROLS = browser.pictureInPictureChild.getKeyboardControls();
AVAILABLE_PIP_OVERRIDES = {
// The keys of this object are match patterns for URLs, as documented in
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns
//
// Example:
// const KEYBOARD_CONTROLS = browser.pictureInPictureChild.getKeyboardControls();
//
//
// "https://*.youtube.com/*": {
// policy: TOGGLE_POLICIES.THREE_QUARTERS,
@ -73,7 +74,6 @@ let AVAILABLE_PIP_OVERRIDES;
netflix: {
"https://*.netflix.com/*": {
keyboardControls: ~KEYBOARD_CONTROLS.SEEK,
videoWrapperScriptPath: "video-wrappers/netflix.js",
},
"https://*.netflix.com/browse*": { policy: TOGGLE_POLICIES.HIDDEN },

View File

@ -17,6 +17,12 @@ class PictureInPictureVideoWrapper {
}
this.player = netflixPlayerAPI.getVideoPlayerBySessionId(sessionId);
}
getCurrentTime(video) {
return this.player.getCurrentTime();
}
getDuration(video) {
return this.player.getDuration();
}
play() {
this.player.play();
}
@ -55,6 +61,31 @@ class PictureInPictureVideoWrapper {
});
}
}
setCurrentTime(video, position) {
let oldTime = this.player.getCurrentTime();
let duration = this.player.getDuration();
let isHome = position == 0;
let isEnd = position >= duration;
// Read pipChild's expected seek result to determine if we want
// to move forward/backwards, or go to the start/end
let seekDirection = position - oldTime;
// But ignore pipChild's proposed seek forward/backward time for a better viewing
// experience. 10 seconds (10000ms) seems to be the best value for compatibility.
// The new currentTime will not always be 10 seconds forward/backward though, since Netflix
// adjusts the new currentTime when seek() is called, according to the current timestamp.
let seekTimeMS = 10000;
let newTime = 0;
if (isHome || isEnd) {
newTime = position;
} else if (seekDirection < 0) {
newTime = Math.max(oldTime - seekTimeMS, 0);
} else if (seekDirection > 0) {
newTime = Math.min(oldTime + seekTimeMS, duration);
}
this.player.seek(newTime);
}
}
this.PictureInPictureVideoWrapper = PictureInPictureVideoWrapper;

View File

@ -2306,6 +2306,9 @@ class PictureInPictureChildVideoWrapper {
let retVal = wrappedMethod.call(this.#siteWrapper, ...args);
if (!validateRetVal) {
logConsole.debug(
`Invalid return value validator was found for method ${name}(). Replacing return value ${retVal} with null.`
);
Cu.reportError(
`No return value validator was provided for method ${name}(). Returning null.`
);
@ -2313,6 +2316,7 @@ class PictureInPictureChildVideoWrapper {
}
if (!validateRetVal(retVal)) {
logConsole.debug("Invalid return value:", retVal);
Cu.reportError(
`Calling method ${name}() returned an unexpected value: ${retVal}. Returning null.`
);
@ -2322,6 +2326,7 @@ class PictureInPictureChildVideoWrapper {
return retVal;
}
} catch (e) {
logConsole.debug("Error:", e.message);
Cu.reportError(`There was an error while calling ${name}(): `, e.message);
}