2022-11-25 01:37:27 -06:00
|
|
|
let introSkipper = {
|
2024-06-01 14:15:30 +02:00
|
|
|
allowEnter: true,
|
2022-11-25 01:37:27 -06:00
|
|
|
skipSegments: {},
|
|
|
|
videoPlayer: {},
|
|
|
|
// .bind() is used here to prevent illegal invocation errors
|
|
|
|
originalFetch: window.fetch.bind(window),
|
|
|
|
};
|
|
|
|
introSkipper.d = function (msg) {
|
2024-03-05 10:08:29 -05:00
|
|
|
console.debug("[intro skipper] ", msg);
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
|
|
|
/** Setup event listeners */
|
|
|
|
introSkipper.setup = function () {
|
2022-11-25 01:37:27 -06:00
|
|
|
document.addEventListener("viewshow", introSkipper.viewShow);
|
|
|
|
window.fetch = introSkipper.fetchWrapper;
|
|
|
|
introSkipper.d("Registered hooks");
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
|
|
|
/** Wrapper around fetch() that retrieves skip segments for the currently playing item. */
|
|
|
|
introSkipper.fetchWrapper = async function (...args) {
|
2022-11-17 01:19:44 -06:00
|
|
|
// Based on JellyScrub's trickplay.js
|
|
|
|
let [resource, options] = args;
|
2022-11-25 01:37:27 -06:00
|
|
|
let response = await introSkipper.originalFetch(resource, options);
|
2022-11-17 01:19:44 -06:00
|
|
|
// Bail early if this isn't a playback info URL
|
2022-11-25 01:37:27 -06:00
|
|
|
try {
|
2024-05-12 23:59:41 +02:00
|
|
|
let path = new URL(resource).pathname;
|
|
|
|
if (!path.includes("/PlaybackInfo")) { return response; }
|
|
|
|
introSkipper.d("Retrieving skip segments from URL");
|
|
|
|
introSkipper.d(path);
|
2024-05-16 12:23:35 -05:00
|
|
|
|
|
|
|
// Check for context root and set id accordingly
|
|
|
|
let path_arr = path.split("/");
|
|
|
|
let id = "";
|
|
|
|
if (path_arr[1] == "Items") {
|
|
|
|
id = path_arr[2];
|
|
|
|
} else {
|
|
|
|
id = path_arr[3];
|
|
|
|
}
|
|
|
|
|
2024-05-12 23:59:41 +02:00
|
|
|
introSkipper.skipSegments = await introSkipper.secureFetch(`Episode/${id}/IntroSkipperSegments`);
|
|
|
|
introSkipper.d("Successfully retrieved skip segments");
|
|
|
|
introSkipper.d(introSkipper.skipSegments);
|
2022-11-17 01:19:44 -06:00
|
|
|
}
|
2022-11-25 01:37:27 -06:00
|
|
|
catch (e) {
|
2024-05-12 23:59:41 +02:00
|
|
|
console.error("Unable to get skip segments from", resource, e);
|
2022-11-17 01:19:44 -06:00
|
|
|
}
|
|
|
|
return response;
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Event handler that runs whenever the current view changes.
|
|
|
|
* Used to detect the start of video playback.
|
|
|
|
*/
|
|
|
|
introSkipper.viewShow = function () {
|
2022-11-06 21:20:52 -06:00
|
|
|
const location = window.location.hash;
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Location changed to " + location);
|
2024-05-13 00:02:18 +02:00
|
|
|
if (location !== "#/video") {
|
2024-05-12 23:59:41 +02:00
|
|
|
introSkipper.d("Ignoring location change");
|
|
|
|
return;
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.injectCss();
|
|
|
|
introSkipper.injectButton();
|
|
|
|
introSkipper.videoPlayer = document.querySelector("video");
|
2024-03-07 15:41:00 -05:00
|
|
|
if (introSkipper.videoPlayer != null) {
|
2024-05-12 23:59:41 +02:00
|
|
|
introSkipper.d("Hooking video timeupdate");
|
|
|
|
introSkipper.videoPlayer.addEventListener("timeupdate", introSkipper.videoPositionChanged);
|
2024-06-01 14:15:30 +02:00
|
|
|
document.body.addEventListener('keydown', introSkipper.eventHandler, true);
|
2024-03-07 15:41:00 -05:00
|
|
|
}
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Injects the CSS used by the skip intro button.
|
|
|
|
* Calling this function is a no-op if the CSS has already been injected.
|
|
|
|
*/
|
|
|
|
introSkipper.injectCss = function () {
|
2022-11-25 01:37:27 -06:00
|
|
|
if (introSkipper.testElement("style#introSkipperCss")) {
|
2024-05-12 23:59:41 +02:00
|
|
|
introSkipper.d("CSS already added");
|
|
|
|
return;
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Adding CSS");
|
2022-11-06 21:20:52 -06:00
|
|
|
let styleElement = document.createElement("style");
|
|
|
|
styleElement.id = "introSkipperCss";
|
|
|
|
styleElement.innerText = `
|
2024-05-18 20:05:04 +02:00
|
|
|
:root {
|
|
|
|
--rounding: .2em;
|
|
|
|
--accent: 0, 164, 220;
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
|
|
|
#skipIntro.upNextContainer {
|
|
|
|
width: unset;
|
2024-06-15 17:44:38 +02:00
|
|
|
margin: unset;
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
2022-11-06 21:20:52 -06:00
|
|
|
#skipIntro {
|
|
|
|
position: absolute;
|
2024-05-18 20:05:04 +02:00
|
|
|
bottom: 6em;
|
|
|
|
right: 4.5em;
|
|
|
|
background-color: transparent;
|
|
|
|
font-size: 1.2em;
|
|
|
|
}
|
|
|
|
#skipIntro .emby-button {
|
|
|
|
text-shadow: 0 0 3px rgba(0, 0, 0, 0.7);
|
|
|
|
border-radius: var(--rounding);
|
|
|
|
background-color: rgba(0, 0, 0, 0.3);
|
|
|
|
will-change: opacity, transform;
|
2024-04-17 21:21:25 +02:00
|
|
|
opacity: 0;
|
2024-05-18 20:05:04 +02:00
|
|
|
transition: opacity 0.3s ease-in, transform 0.3s ease-out;
|
|
|
|
}
|
2024-07-07 22:42:45 +02:00
|
|
|
#skipIntro.show .emby-button {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
2024-05-18 20:05:04 +02:00
|
|
|
#skipIntro .emby-button:hover,
|
|
|
|
#skipIntro .emby-button:focus {
|
|
|
|
background-color: rgba(var(--accent),0.7);
|
|
|
|
transform: scale(1.05);
|
2024-05-12 23:59:41 +02:00
|
|
|
}
|
2024-05-18 20:05:04 +02:00
|
|
|
#btnSkipSegmentText {
|
|
|
|
padding-right: 0.15em;
|
|
|
|
padding-left: 0.2em;
|
|
|
|
margin-top: -0.1em;
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
document.querySelector("head").appendChild(styleElement);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Inject the skip intro button into the video player.
|
|
|
|
* Calling this function is a no-op if the CSS has already been injected.
|
|
|
|
*/
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.injectButton = async function () {
|
2023-03-13 21:00:21 -05:00
|
|
|
// Ensure the button we're about to inject into the page doesn't conflict with a pre-existing one
|
|
|
|
const preExistingButton = introSkipper.testElement("div.skipIntro");
|
|
|
|
if (preExistingButton) {
|
|
|
|
preExistingButton.style.display = "none";
|
|
|
|
}
|
|
|
|
if (introSkipper.testElement(".btnSkipIntro.injected")) {
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Button already added");
|
2022-11-06 21:20:52 -06:00
|
|
|
return;
|
|
|
|
}
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Adding button");
|
|
|
|
let config = await introSkipper.secureFetch("Intros/UserInterfaceConfiguration");
|
2022-11-06 21:20:52 -06:00
|
|
|
if (!config.SkipButtonVisible) {
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Not adding button: not visible");
|
2022-11-06 21:20:52 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Construct the skip button div
|
|
|
|
const button = document.createElement("div");
|
|
|
|
button.id = "skipIntro"
|
|
|
|
button.classList.add("hide");
|
2022-11-25 01:37:27 -06:00
|
|
|
button.addEventListener("click", introSkipper.doSkip);
|
2022-11-06 21:20:52 -06:00
|
|
|
button.innerHTML = `
|
2024-05-18 20:05:04 +02:00
|
|
|
<button is="emby-button" type="button" class="btnSkipIntro injected">
|
2023-03-04 00:15:26 -06:00
|
|
|
<span id="btnSkipSegmentText"></span>
|
2022-11-06 21:20:52 -06:00
|
|
|
<span class="material-icons skip_next"></span>
|
|
|
|
</button>
|
|
|
|
`;
|
2023-03-04 00:15:26 -06:00
|
|
|
button.dataset["intro_text"] = config.SkipButtonIntroText;
|
|
|
|
button.dataset["credits_text"] = config.SkipButtonEndCreditsText;
|
2022-11-06 21:20:52 -06:00
|
|
|
/*
|
|
|
|
* Alternative workaround for #44. Jellyfin's video component registers a global click handler
|
|
|
|
* (located at src/controllers/playback/video/index.js:1492) that pauses video playback unless
|
|
|
|
* the clicked element has a parent with the class "videoOsdBottom" or "upNextContainer".
|
|
|
|
*/
|
|
|
|
button.classList.add("upNextContainer");
|
|
|
|
// Append the button to the video OSD
|
|
|
|
let controls = document.querySelector("div#videoOsdPage");
|
|
|
|
controls.appendChild(button);
|
2023-03-04 00:15:26 -06:00
|
|
|
}
|
2023-03-22 00:19:27 -05:00
|
|
|
/** Tests if the OSD controls are visible. */
|
2023-03-16 05:07:20 +01:00
|
|
|
introSkipper.osdVisible = function () {
|
2023-03-22 00:19:27 -05:00
|
|
|
const osd = document.querySelector("div.videoOsdBottom");
|
|
|
|
return osd ? !osd.classList.contains("hide") : false;
|
2023-03-16 05:07:20 +01:00
|
|
|
}
|
2023-03-04 00:15:26 -06:00
|
|
|
/** Get the currently playing skippable segment. */
|
|
|
|
introSkipper.getCurrentSegment = function (position) {
|
|
|
|
for (let key in introSkipper.skipSegments) {
|
|
|
|
const segment = introSkipper.skipSegments[key];
|
2023-03-16 05:07:20 +01:00
|
|
|
if ((position >= segment.ShowSkipPromptAt && position < segment.HideSkipPromptAt) || (introSkipper.osdVisible() && position >= segment.IntroStart && position < segment.IntroEnd)) {
|
2023-03-04 00:15:26 -06:00
|
|
|
segment["SegmentType"] = key;
|
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { "SegmentType": "None" };
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
2024-06-01 14:15:30 +02:00
|
|
|
introSkipper.overrideBlur = function(embyButton) {
|
|
|
|
if (!embyButton.originalBlur) {
|
|
|
|
embyButton.originalBlur = embyButton.blur;
|
|
|
|
}
|
|
|
|
embyButton.blur = function () {
|
|
|
|
if (!introSkipper.osdVisible() || !embyButton.contains(document.activeElement)) {
|
|
|
|
embyButton.originalBlur.call(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
introSkipper.restoreBlur = function(embyButton) {
|
|
|
|
if (embyButton.originalBlur) {
|
|
|
|
embyButton.blur = embyButton.originalBlur;
|
|
|
|
delete embyButton.originalBlur;
|
|
|
|
}
|
|
|
|
};
|
2022-11-06 21:20:52 -06:00
|
|
|
/** Playback position changed, check if the skip button needs to be displayed. */
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.videoPositionChanged = function () {
|
2022-11-06 21:20:52 -06:00
|
|
|
const skipButton = document.querySelector("#skipIntro");
|
2024-06-01 14:15:30 +02:00
|
|
|
if (introSkipper.videoPlayer.currentTime === 0 || !skipButton || !introSkipper.allowEnter) return;
|
2024-05-18 20:05:04 +02:00
|
|
|
const embyButton = skipButton.querySelector(".emby-button");
|
2024-06-01 14:15:30 +02:00
|
|
|
const tvLayout = document.documentElement.classList.contains("layout-tv");
|
2023-03-04 00:15:26 -06:00
|
|
|
const segment = introSkipper.getCurrentSegment(introSkipper.videoPlayer.currentTime);
|
2024-05-18 20:05:04 +02:00
|
|
|
switch (segment.SegmentType) {
|
2023-03-04 00:15:26 -06:00
|
|
|
case "None":
|
2024-07-07 22:42:45 +02:00
|
|
|
if (!skipButton.classList.contains('show')) return;
|
|
|
|
skipButton.classList.remove('show');
|
2024-05-18 20:05:04 +02:00
|
|
|
embyButton.addEventListener("transitionend", () => {
|
2024-04-17 21:21:25 +02:00
|
|
|
skipButton.classList.add("hide");
|
2024-06-01 14:15:30 +02:00
|
|
|
if (tvLayout) {
|
|
|
|
introSkipper.restoreBlur(embyButton);
|
|
|
|
embyButton.blur();
|
|
|
|
}
|
2024-04-17 21:21:25 +02:00
|
|
|
}, { once: true });
|
2023-03-04 00:15:26 -06:00
|
|
|
return;
|
|
|
|
case "Introduction":
|
2024-05-18 20:05:04 +02:00
|
|
|
skipButton.querySelector("#btnSkipSegmentText").textContent = skipButton.dataset.intro_text;
|
2023-03-04 00:15:26 -06:00
|
|
|
break;
|
|
|
|
case "Credits":
|
2024-05-18 20:05:04 +02:00
|
|
|
skipButton.querySelector("#btnSkipSegmentText").textContent = skipButton.dataset.credits_text;
|
2023-03-04 00:15:26 -06:00
|
|
|
break;
|
2022-11-25 01:37:27 -06:00
|
|
|
}
|
2024-04-17 21:21:25 +02:00
|
|
|
if (!skipButton.classList.contains("hide")) return;
|
2024-07-07 22:42:45 +02:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
skipButton.classList.remove("hide");
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
skipButton.classList.add('show');
|
|
|
|
if (tvLayout) {
|
|
|
|
introSkipper.overrideBlur(embyButton);
|
|
|
|
embyButton.focus({ focusVisible: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-06-01 14:15:30 +02:00
|
|
|
}
|
|
|
|
introSkipper.throttle = function (func, limit) {
|
|
|
|
let inThrottle;
|
|
|
|
return function(...args) {
|
|
|
|
const context = this;
|
|
|
|
if (!inThrottle) {
|
|
|
|
func.apply(context, args);
|
|
|
|
inThrottle = true;
|
|
|
|
setTimeout(() => inThrottle = false, limit);
|
|
|
|
}
|
|
|
|
};
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
|
|
|
/** Seeks to the end of the intro. */
|
2024-07-02 20:11:31 +02:00
|
|
|
introSkipper.doSkip = introSkipper.throttle(async function () {
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.d("Skipping intro");
|
|
|
|
introSkipper.d(introSkipper.skipSegments);
|
2023-03-04 00:15:26 -06:00
|
|
|
const segment = introSkipper.getCurrentSegment(introSkipper.videoPlayer.currentTime);
|
2024-06-01 14:15:30 +02:00
|
|
|
if (segment.SegmentType === "None") {
|
2023-03-04 00:15:26 -06:00
|
|
|
console.warn("[intro skipper] doSkip() called without an active segment");
|
|
|
|
return;
|
|
|
|
}
|
2024-07-07 22:42:45 +02:00
|
|
|
const currentSrc = introSkipper.videoPlayer.currentSrc;
|
|
|
|
const controller = new AbortController();
|
|
|
|
const signal = controller.signal;
|
2024-07-02 20:11:31 +02:00
|
|
|
const seekToSegmentEnd = () => new Promise((resolve) => {
|
2024-07-07 22:42:45 +02:00
|
|
|
const onSeeked = () => requestAnimationFrame(resolve);
|
|
|
|
introSkipper.videoPlayer.addEventListener('seeked', onSeeked, { once: true, signal });
|
2024-07-02 20:11:31 +02:00
|
|
|
introSkipper.videoPlayer.currentTime = segment.IntroEnd;
|
|
|
|
});
|
2024-07-07 22:42:45 +02:00
|
|
|
const nextEpisodeLoaded = () => new Promise(resolve => {
|
|
|
|
const onLoadStart = () => resolve(true);
|
|
|
|
const timeoutId = setTimeout(() => resolve(false), 500);
|
|
|
|
introSkipper.videoPlayer.addEventListener('loadstart', onLoadStart, { signal });
|
|
|
|
signal.addEventListener('abort', () => clearTimeout(timeoutId));
|
|
|
|
});
|
2024-06-01 14:15:30 +02:00
|
|
|
// Disable keydown events
|
|
|
|
introSkipper.allowEnter = false;
|
2024-07-07 22:42:45 +02:00
|
|
|
try {
|
|
|
|
// Check if the segment is "Credits" and skipping would leave less than 3 seconds of video
|
|
|
|
if (segment.SegmentType === "Credits" && introSkipper.videoPlayer.duration - segment.IntroEnd < 3) {
|
|
|
|
// Simulate 'N' key press to go to the next episode
|
|
|
|
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'N', shiftKey: true, bubbles: true }));
|
|
|
|
// Check if the next episode actually starts loading
|
|
|
|
const loaded = await nextEpisodeLoaded();
|
|
|
|
// If the next episode didn't load, just seek to the end of the current segment
|
|
|
|
if (!loaded) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100)); // Short delay
|
|
|
|
if (introSkipper.videoPlayer.currentSrc === currentSrc) {
|
|
|
|
await seekToSegmentEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Default behavior: seek to the end of the current segment
|
|
|
|
await seekToSegmentEnd();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
introSkipper.allowEnter = true; // Always re-enable keydown events
|
|
|
|
controller.abort(); // Cleanup any remaining listeners
|
2024-07-02 20:11:31 +02:00
|
|
|
}
|
2024-06-01 14:15:30 +02:00
|
|
|
}, 3000);
|
2022-11-17 01:19:44 -06:00
|
|
|
/** Tests if an element with the provided selector exists. */
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.testElement = function (selector) { return document.querySelector(selector); }
|
2022-11-17 01:19:44 -06:00
|
|
|
/** Make an authenticated fetch to the Jellyfin server and parse the response body as JSON. */
|
2022-11-25 01:37:27 -06:00
|
|
|
introSkipper.secureFetch = async function (url) {
|
2022-11-17 01:19:44 -06:00
|
|
|
url = ApiClient.serverAddress() + "/" + url;
|
2024-03-05 10:08:29 -05:00
|
|
|
const reqInit = { headers: { "Authorization": "MediaBrowser Token=" + ApiClient.accessToken() } };
|
2022-11-17 01:19:44 -06:00
|
|
|
const res = await fetch(url, reqInit);
|
2024-03-05 10:08:29 -05:00
|
|
|
if (res.status !== 200) { throw new Error(`Expected status 200 from ${url}, but got ${res.status}`); }
|
2022-11-17 01:19:44 -06:00
|
|
|
return await res.json();
|
|
|
|
}
|
2024-06-01 14:15:30 +02:00
|
|
|
/** Handle keydown events. */
|
|
|
|
introSkipper.eventHandler = function (e) {
|
|
|
|
const skipButton = document.querySelector("#skipIntro");
|
|
|
|
if (!skipButton || skipButton.classList.contains("hide")) return;
|
|
|
|
// Ignore all keydown events
|
|
|
|
if (!introSkipper.allowEnter) {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.key !== "Enter") return;
|
|
|
|
const embyButton = skipButton.querySelector(".emby-button");
|
|
|
|
if (document.documentElement.classList.contains("layout-tv") && embyButton.contains(document.activeElement)) {
|
|
|
|
e.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (document.documentElement.classList.contains("layout-desktop")) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
introSkipper.doSkip();
|
|
|
|
}
|
|
|
|
}
|
2024-05-18 20:05:04 +02:00
|
|
|
introSkipper.setup();
|