Skip to content

Commit

Permalink
Closes #954
Browse files Browse the repository at this point in the history
Fix not detecting /clip pages as /watch pages
  • Loading branch information
ParticleCore committed Mar 6, 2024
1 parent 1d6d38c commit 98d3deb
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 110 deletions.
6 changes: 6 additions & 0 deletions src/chrome/css/content-script.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* ini | autoplay */
#movie_player.unstarted-mode > *:not(.ytp-cued-thumbnail-overlay) {
display: none;
}
/* end | autoplay */

/* ini | video focus */
html #masthead-container,
html #secondary,
Expand Down
154 changes: 99 additions & 55 deletions src/chrome/js/background-inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function mainScript(extensionId, SettingData, defaultSettings) {
}

},
isWatchPage: () => window.location.pathname === "/watch" || window.location.pathname.startsWith("/clip")
};

const FeatureUpdater = (() => {
Expand Down Expand Up @@ -218,7 +219,6 @@ function mainScript(extensionId, SettingData, defaultSettings) {
const OverrideResponseText = (() => {

const listeners = [];
const original = Response.prototype.text?.["original"] || Response.prototype.text;
const navigationMod = data => {
try {
const response = JSON.parse(data?.replace(")]}'\n", ""));
Expand All @@ -229,6 +229,8 @@ function mainScript(extensionId, SettingData, defaultSettings) {
}
};

const original = Response.prototype.text?.["original"] || Response.prototype.text;

Response.prototype.text = function () {
return original.apply(this, arguments).then(data => navigationMod(data));
};
Expand Down Expand Up @@ -271,6 +273,97 @@ function mainScript(extensionId, SettingData, defaultSettings) {

})();

const OverrideVideoPlay = (() => {

let canPlay = false;
let timer = null;
let previousVideoId = null;

const containers = [
"#player:has(#movie_player)",
"#player-container:has(#movie_player)",
"#ytd-player:has(#movie_player)",
"#movie_player"
].join(",");

const onClick = event => {

clearTimeout(timer);

canPlay = document.querySelector(containers)?.contains(event.target) === true;

if (canPlay) {
previousVideoId = document.getElementById("movie_player")?.["getVideoData"]()?.["video_id"] || null;
}

timer = setTimeout(() => canPlay = false, 300);

}

const override = () => {

const original = HTMLVideoElement.prototype.play?.["original"] || HTMLVideoElement.prototype.play;

HTMLVideoElement.prototype.play = function () {

const moviePlayer = document.getElementById("movie_player");
const isMoviePlayer = moviePlayer?.contains(this) === true;
const currentVideoId = moviePlayer?.["getVideoData"]()?.["video_id"] || "";
const allowed = !isMoviePlayer || iridiumSettings.autoplay || canPlay || previousVideoId === currentVideoId;

if (allowed) {

if (isMoviePlayer) {
previousVideoId = currentVideoId;
} else {
previousVideoId = null;
}

return original.apply(this, arguments);

} else {

previousVideoId = null;

return new Promise((resolve, reject) => reject(new DOMException("rejected")));

}

};

HTMLVideoElement.prototype.play.original = original;

};

const reset = () => {

HTMLVideoElement.prototype.play = HTMLVideoElement.prototype.play?.["original"] || HTMLVideoElement.prototype.play;
delete HTMLVideoElement.prototype.play?.["original"];

canPlay = false;
timer = null;
previousVideoId = null;

};

const update = () => {
if (iridiumSettings.autoplay) {
document.documentElement.removeEventListener("click", onClick, true);
reset();
} else {
document.documentElement.addEventListener("click", onClick, true);
override();
}
};

update();

FeatureUpdater.register(SettingData.autoplay.id, update);

return {};

})();

const OverrideApplicationCreate = (() => {

const createListeners = [];
Expand All @@ -295,15 +388,8 @@ function mainScript(extensionId, SettingData, defaultSettings) {
const cueVideoByPlayerVars = created?.["cueVideoByPlayerVars"];

created["loadVideoByPlayerVars"] = function () {
if (window.location.pathname === "/watch"
&& !iridiumSettings.autoplay
&& arguments?.[0]?.["reload_reason"] !== "signature"
) {
created?.["cueVideoByPlayerVars"]?.apply(this, arguments);
} else {
loadListeners.forEach(listener => listener?.(arguments));
loadVideoByPlayerVars?.apply(this, arguments);
}
loadListeners.forEach(listener => listener?.(arguments));
loadVideoByPlayerVars?.apply(this, arguments);
};

created["cueVideoByPlayerVars"] = function () {
Expand Down Expand Up @@ -386,48 +472,6 @@ function mainScript(extensionId, SettingData, defaultSettings) {

})();

const OverrideCreatePlayerCallback = (() => {

const createPlayerCallbackKey = crypto.randomUUID();

Object.defineProperty(Object.prototype, "createPlayerCallback", {
set(data) {
this[createPlayerCallbackKey] = data;
},
get() {
if (this?.config?.loaded) {
this.config.loaded = false;
}
return this[createPlayerCallbackKey];
}
});

return {};

})();

const OverridePlayerContainer = (() => {

const bootstrapPlayerContainerKey = crypto.randomUUID();

Object.defineProperty(Object.prototype, "bootstrapPlayerContainer", {
set(data) {
this[bootstrapPlayerContainerKey] = data;
},
get() {
const data = this[bootstrapPlayerContainerKey];
if (iridiumSettings.autoplay || !(data instanceof HTMLElement)) {
return data;
} else {
return undefined;
}
}
});

return {};

})();

const OverrideSetInternalSize = (() => {

const setInternalSizeKey = crypto.randomUUID();
Expand Down Expand Up @@ -975,7 +1019,7 @@ function mainScript(extensionId, SettingData, defaultSettings) {

if (iridiumSettings.alwaysVisible
&& !document.fullscreenElement
&& window.location.pathname === "/watch"
&& Util.isWatchPage()
&& parentRects.bottom < parentRects.height * .5
) {
if (!isAlwaysVisible()) {
Expand Down Expand Up @@ -2161,7 +2205,7 @@ function mainScript(extensionId, SettingData, defaultSettings) {

}

} else if (window.location.pathname === "/watch") {
} else if (Util.isWatchPage()) {

// watch page

Expand Down Expand Up @@ -2361,7 +2405,7 @@ function mainScript(extensionId, SettingData, defaultSettings) {
}

if (window.location.pathname !== "/"
&& window.location.pathname !== "/watch"
&& !Util.isWatchPage()
&& window.location.pathname !== "/results"
) {
return;
Expand Down
6 changes: 6 additions & 0 deletions src/firefox/css/content-script.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* ini | autoplay */
#movie_player.unstarted-mode > *:not(.ytp-cued-thumbnail-overlay) {
display: none;
}
/* end | autoplay */

/* ini | video focus */
html #masthead-container,
html #secondary,
Expand Down
Loading

0 comments on commit 98d3deb

Please sign in to comment.