mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-01 10:21:22 +01:00
Fix Shorts support and add userscript voting
Harden Shorts controls, reaction state, and navigation; share voting logic with the extension; improve ratio-bar rendering; and add comprehensive Jest, Playwright, artifact, and live-browser regression coverage.
This commit is contained in:
1 parent
4e6dfd1977
commit
3657de9b2c
74 files changed
+23328
-588
No files matched your search
@@ -1,5 +1,109 @@
|
||||
import { isMobile, isShorts, extConfig } from "./state";
|
||||
import { isInViewport, querySelector, querySelectorAll } from "./utils";
|
||||
import { getVideoId, isInViewport, querySelector, querySelectorAll } from "./utils";
|
||||
|
||||
const buttonsVideoOwnership = new WeakMap();
|
||||
|
||||
function hasRenderedBox(element) {
|
||||
if (!element?.isConnected || element.closest("[hidden], [aria-hidden='true'], [inert]")) {
|
||||
return false;
|
||||
}
|
||||
for (let current = element; current; current = current.parentElement) {
|
||||
const style = window.getComputedStyle(current);
|
||||
if (
|
||||
style.display === "none" ||
|
||||
style.visibility === "hidden" ||
|
||||
style.visibility === "collapse" ||
|
||||
Number.parseFloat(style.opacity) === 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const rect = element.getBoundingClientRect();
|
||||
return rect.width > 0 && rect.height > 0;
|
||||
}
|
||||
|
||||
function intersectsViewport(element) {
|
||||
const rect = element.getBoundingClientRect();
|
||||
const height = innerHeight || document.documentElement.clientHeight;
|
||||
const width = innerWidth || document.documentElement.clientWidth;
|
||||
return (
|
||||
rect.width > 0 && rect.height > 0 && rect.bottom > 0 && rect.right > 0 && rect.top < height && rect.left < width
|
||||
);
|
||||
}
|
||||
|
||||
function configuredMatches(selectors) {
|
||||
const matches = [];
|
||||
for (const selector of Array.isArray(selectors) ? selectors : [selectors]) {
|
||||
if (selector) matches.push(...document.querySelectorAll(selector));
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
function getDesktopWatchButtonCandidates() {
|
||||
return Array.from(
|
||||
new Set([
|
||||
...configuredMatches(extConfig.selectors.buttons.regular.desktopMenu),
|
||||
...configuredMatches(extConfig.selectors.buttons.regular.desktopNoMenu),
|
||||
]),
|
||||
).filter((candidate) => {
|
||||
const segmented = querySelector(extConfig.selectors.buttons.segmentedContainer, candidate);
|
||||
const like = querySelector(extConfig.selectors.buttons.likeButton.notSegmented, candidate);
|
||||
return segmented !== undefined || like !== undefined;
|
||||
});
|
||||
}
|
||||
|
||||
function markButtonsForVideo(buttons, videoId) {
|
||||
if (buttons && videoId) {
|
||||
buttonsVideoOwnership.set(buttons, videoId);
|
||||
}
|
||||
}
|
||||
|
||||
function getButtonsVideoOwnership(candidate) {
|
||||
return (
|
||||
buttonsVideoOwnership.get(candidate) ??
|
||||
candidate.getAttribute("video-id") ??
|
||||
candidate.getAttribute("data-video-id")
|
||||
);
|
||||
}
|
||||
|
||||
function selectCurrentWatchButtons(candidates) {
|
||||
const videoId = getVideoId(window.location.href);
|
||||
const ranked = candidates
|
||||
.map((candidate, index) => {
|
||||
const watchRoot = candidate.closest("ytd-watch-flexy, ytd-watch-grid");
|
||||
const rootVideoId = watchRoot?.getAttribute("video-id");
|
||||
const rootMatches = Boolean(videoId && rootVideoId === videoId);
|
||||
const rootConflicts = Boolean(videoId && rootVideoId && rootVideoId !== videoId);
|
||||
const controlsVideoId = getButtonsVideoOwnership(candidate);
|
||||
const controlsMatch = Boolean(videoId && controlsVideoId === videoId);
|
||||
const controlsConflict = Boolean(videoId && controlsVideoId && controlsVideoId !== videoId);
|
||||
const rendered = hasRenderedBox(candidate);
|
||||
const inViewport = rendered && intersectsViewport(candidate);
|
||||
const tier =
|
||||
controlsMatch && inViewport
|
||||
? 8
|
||||
: rootMatches && inViewport
|
||||
? 7
|
||||
: inViewport
|
||||
? 6
|
||||
: controlsMatch && rendered
|
||||
? 5
|
||||
: rootMatches && rendered
|
||||
? 4
|
||||
: rendered
|
||||
? 3
|
||||
: controlsMatch || rootMatches
|
||||
? 2
|
||||
: 1;
|
||||
return { candidate, controlsConflict, index, rootConflicts, tier };
|
||||
})
|
||||
// Do not bind outgoing controls to the destination video while YouTube is
|
||||
// switching the current watch root during an SPA navigation.
|
||||
.filter(({ rootConflicts }) => !rootConflicts);
|
||||
const ownershipCompatible = ranked.filter(({ controlsConflict }) => !controlsConflict);
|
||||
const selectionPool = ownershipCompatible.length > 0 ? ownershipCompatible : ranked;
|
||||
return selectionPool.sort((left, right) => right.tier - left.tier || left.index - right.index)[0]?.candidate;
|
||||
}
|
||||
|
||||
function getNativeButton(buttonContainer) {
|
||||
return querySelector(extConfig.selectors.buttons.nativeButton, buttonContainer);
|
||||
@@ -32,20 +136,15 @@ function getButtons() {
|
||||
if (isMobile()) {
|
||||
return document.querySelector(extConfig.selectors.buttons.regular.mobile);
|
||||
}
|
||||
//--- If Menu Element Is Displayed: ---//
|
||||
if (querySelector(extConfig.selectors.menuContainer)?.offsetParent === null) {
|
||||
return querySelector(extConfig.selectors.buttons.regular.desktopMenu);
|
||||
//--- If Menu Element Isn't Displayed: ---//
|
||||
} else {
|
||||
return querySelector(extConfig.selectors.buttons.regular.desktopNoMenu);
|
||||
}
|
||||
return selectCurrentWatchButtons(getDesktopWatchButtonCandidates());
|
||||
}
|
||||
|
||||
function getLikeButton() {
|
||||
const buttons = getButtons();
|
||||
return isSegmentedButtonLayout()
|
||||
? querySelector(extConfig.selectors.buttons.likeButton.segmented) ??
|
||||
querySelector(extConfig.selectors.buttons.likeButton.segmentedGetButtons, getButtons())
|
||||
: querySelector(extConfig.selectors.buttons.likeButton.notSegmented, getButtons());
|
||||
? querySelector(extConfig.selectors.buttons.likeButton.segmented, buttons) ??
|
||||
querySelector(extConfig.selectors.buttons.likeButton.segmentedGetButtons, buttons)
|
||||
: querySelector(extConfig.selectors.buttons.likeButton.notSegmented, buttons);
|
||||
}
|
||||
|
||||
function getLikeTextContainer() {
|
||||
@@ -53,21 +152,22 @@ function getLikeTextContainer() {
|
||||
}
|
||||
|
||||
function getDislikeButton() {
|
||||
const buttons = getButtons();
|
||||
if (isSegmentedButtonLayout()) {
|
||||
return (
|
||||
querySelector(extConfig.selectors.buttons.dislikeButton.segmented) ??
|
||||
querySelector(extConfig.selectors.buttons.dislikeButton.segmentedGetButtons, getButtons())
|
||||
querySelector(extConfig.selectors.buttons.dislikeButton.segmented, buttons) ??
|
||||
querySelector(extConfig.selectors.buttons.dislikeButton.segmentedGetButtons, buttons)
|
||||
);
|
||||
}
|
||||
|
||||
const notSegmentedMatch = querySelector(extConfig.selectors.buttons.dislikeButton.notSegmented, getButtons());
|
||||
const notSegmentedMatch = querySelector(extConfig.selectors.buttons.dislikeButton.notSegmented, buttons);
|
||||
|
||||
if (notSegmentedMatch != null) {
|
||||
return notSegmentedMatch;
|
||||
}
|
||||
|
||||
if (isShorts()) {
|
||||
return querySelector(extConfig.selectors.buttons.dislikeButton.shortsFallback, getButtons());
|
||||
return querySelector(extConfig.selectors.buttons.dislikeButton.shortsFallback, buttons);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -141,4 +241,6 @@ export {
|
||||
getLikeTextContainer,
|
||||
getDislikeTextContainer,
|
||||
checkForSignInButton,
|
||||
hasRenderedBox,
|
||||
markButtonsForVideo,
|
||||
};
|
||||
Reference in new issue
Block a user