Skip to content

Commit

Permalink
Рефракторинг кода
Browse files Browse the repository at this point in the history
  • Loading branch information
SashaXser authored Jan 25, 2024
1 parent 341907c commit 579e36e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 185 deletions.
2 changes: 1 addition & 1 deletion dist/vot-cloudflare-min.user.js

Large diffs are not rendered by default.

97 changes: 38 additions & 59 deletions dist/vot-cloudflare.user.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vot-min.user.js

Large diffs are not rendered by default.

97 changes: 38 additions & 59 deletions dist/vot.user.js

Large diffs are not rendered by default.

92 changes: 47 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2111,11 +2111,11 @@ const videoObserver = new VideoObserver();
const videosWrappers = new WeakMap();

async function main() {
console.log("Loading extension...");
debug.log("Loading extension...");

await localizationProvider.update();

console.log(`Selected menu language: ${localizationProvider.lang}`);
debug.log(`Selected menu language: ${localizationProvider.lang}`);

if (
BUILD_MODE !== "cloudflare" &&
Expand All @@ -2134,70 +2134,72 @@ async function main() {
);
}

console.log("Extension compatibility passed...");

const findContainer = (video, site) => {
if (site.shadowRoot) {
let container = site.selector
? Array.from(document.querySelectorAll(site.selector)).find((e) =>
e.shadowRoot.contains(video),
)
: video.parentElement;
return container?.shadowRoot ? container.parentElement : container;
}

const browserVersion = Number(browserInfo.browser.version.split(".")[0]);
const isOldBrowser =
(browserInfo.browser.name === "Chrome" && browserVersion < 88) ||
(browserInfo.browser.name === "Firefox" && browserVersion < 84);

if (
isOldBrowser &&
site.selector?.includes(":not") &&
site.selector?.includes("*")
) {
const selector = site.selector.split(" *")[0];
return selector
? Array.from(document.querySelectorAll(selector)).find((e) =>
e.contains(video),
)
: video.parentElement;
}

return site.selector
? Array.from(document.querySelectorAll(site.selector)).find((e) =>
e.contains(video),
)
: video.parentElement;
};
debug.log("Extension compatibility passed...");

videoObserver.onVideoAdded.addListener((video) => {
for (const site of getSites()) {
if (!site) continue;

const container = findContainer(video, site);
if (!container) continue;
let container;
if (site.shadowRoot) {
container = site.selector
? Object.values(document.querySelectorAll(site.selector)).find((e) =>
e.shadowRoot.contains(video),
)
: video.parentElement;
container =
container && container.shadowRoot
? container.parentElement
: container;
} else {
const browserVersion = browserInfo.browser.version.split(".")?.[0];

if (site.host === "rumble" && container.querySelector("vot-block"))
if (
site.selector?.includes(":not") &&
site.selector?.includes("*") &&
browserVersion &&
((browserInfo.browser.name === "Chrome" &&
Number(browserVersion) < 88) ||
(browserInfo.browser.name === "Firefox" &&
Number(browserVersion) < 84))
) {
const selector = site.selector?.split(" *")?.[0];
container = selector
? Object.values(document.querySelectorAll(selector)).find((e) =>
e.contains(video),
)
: video.parentElement;
} else {
container = site.selector
? Object.values(document.querySelectorAll(site.selector)).find(
(e) => e.contains(video),
)
: video.parentElement;
}
}
if (!container) continue;
if (site.host === "rumble" && container.querySelector("vot-block")) {
// fix multiply translation buttons in rumble.com
continue;
if (site.host === "peertube") site.url = window.location.origin;
// we set the url of the current site, since peertube doesn't have a main server
}

if (site.host === "peertube") {
// we set the url of the current site, since peertube doesn't have a main server
site.url = window.location.origin;
}

if (!videosWrappers.has(video)) {
videosWrappers.set(video, new VideoHandler(video, container, site));
break;
}
}
});

videoObserver.onVideoRemoved.addListener(async (video) => {
if (videosWrappers.has(video)) {
await videosWrappers.get(video).release();
videosWrappers.delete(video);
}
});

videoObserver.enable();
}

Expand Down
1 change: 0 additions & 1 deletion src/styles/components/menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
&[hidden] {
pointer-events: none;
display: block !important;
pointer-events: none;
visibility: hidden;
opacity: 0;
transform: translate(-50%) scale(0);
Expand Down
36 changes: 17 additions & 19 deletions src/utils/VideoObserver.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import "requestidlecallback-polyfill";
import { EventImpl } from "./EventImpl.js";

function filterVideoNodes(e) {
return Array.from(e)
.map((e) => {
const result = [];
if (e instanceof HTMLVideoElement) {
result.push(e);
}
if (e instanceof HTMLElement) {
result.push(...Array.from(e.querySelectorAll("video")));
}
if (e?.shadowRoot?.querySelectorAll) {
result.push(...Array.from(e.shadowRoot.querySelectorAll("video")));
}
return result;
})
.flat();
function filterVideoNodes(nodes) {
return Array.from(nodes).flatMap((node) => {
if (node instanceof HTMLVideoElement) {
return [node];
}
if (node instanceof HTMLElement) {
return Array.from(node.querySelectorAll("video"));
}
return node.shadowRoot
? Array.from(node.shadowRoot.querySelectorAll("video"))
: [];
});
}

export class VideoObserver {
Expand All @@ -29,7 +25,7 @@ export class VideoObserver {
window.requestIdleCallback(
() => {
mutationsList.forEach((mutation) => {
if ("childList" !== mutation.type) return;
if (mutation.type !== "childList") return;

filterVideoNodes(mutation.addedNodes).forEach(
this.handleVideoAddedBound,
Expand All @@ -39,7 +35,7 @@ export class VideoObserver {
);
});
},
{ timeout: 1e3 },
{ timeout: 1000 },
);
});
}
Expand All @@ -57,6 +53,8 @@ export class VideoObserver {
this.onVideoAdded.dispatch(video);
}
handleVideoRemoved(video) {
document.contains(video) || this.onVideoRemoved.dispatch(video);
if (!document.contains(video)) {
this.onVideoRemoved.dispatch(video);
}
}
}

0 comments on commit 579e36e

Please sign in to comment.