mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 19:47:04 +03:00
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
export function getControls(): HTMLElement | false {
|
|
const controlsSelectors = [
|
|
// YouTube
|
|
".ytp-right-controls",
|
|
// Mobile YouTube
|
|
".player-controls-top",
|
|
// Invidious/videojs video element's controls element
|
|
".vjs-control-bar",
|
|
];
|
|
|
|
for (const controlsSelector of controlsSelectors) {
|
|
const controls = document.querySelectorAll(controlsSelector);
|
|
|
|
if (controls && controls.length > 0) {
|
|
return <HTMLElement> controls[controls.length - 1];
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
export function isVisible(element: HTMLElement): boolean {
|
|
return element && element.offsetWidth > 0 && element.offsetHeight > 0;
|
|
}
|
|
|
|
export function findValidElementFromSelector(selectors: string[]): HTMLElement {
|
|
return findValidElementFromGenerator(selectors, (selector) => document.querySelector(selector));
|
|
}
|
|
|
|
export function findValidElement(elements: HTMLElement[] | NodeListOf<HTMLElement>): HTMLElement {
|
|
return findValidElementFromGenerator(elements);
|
|
}
|
|
|
|
function findValidElementFromGenerator<T>(objects: T[] | NodeListOf<HTMLElement>, generator?: (obj: T) => HTMLElement): HTMLElement {
|
|
for (const obj of objects) {
|
|
const element = generator ? generator(obj as T) : obj as HTMLElement;
|
|
if (element && isVisible(element)) {
|
|
return element;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function getHashParams(): Record<string, unknown> {
|
|
const windowHash = window.location.hash.substr(1);
|
|
if (windowHash) {
|
|
const params: Record<string, unknown> = windowHash.split('&').reduce((acc, param) => {
|
|
const [key, value] = param.split('=');
|
|
acc[key] = value;
|
|
return acc;
|
|
}, {});
|
|
|
|
return params;
|
|
}
|
|
|
|
return {};
|
|
} |