Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect <video> inside custom elements (web components) (shadow DOM) #74

Open
WofWca opened this issue Jun 12, 2022 · 0 comments
Open

Comments

@WofWca
Copy link
Owner

WofWca commented Jun 12, 2022

See

Affects: msn.com.

Where to fix:

for (const tagName of tagNames) {
const allMediaElementsWThisTag = document.getElementsByTagName(tagName);
if (allMediaElementsWThisTag.length) {
ensureInitAllMediaElementsController().then(allMediaElementsController => {
allMediaElementsController.onNewMediaElements(...allMediaElementsWThisTag);
});
}
}
// Peeked at https://github.com/igrigorik/videospeed/blob/a25373f1d831fe06430c2e9e87dc1bd1aabd25b1/inject.js#L631
function handleMutations(mutations: MutationRecord[]) {
const newElements: HTMLMediaElement[] = [];
for (const m of mutations) {
if (m.type !== 'childList') {
continue;
}
for (const node of m.addedNodes) {
if (node.nodeType !== Node.ELEMENT_NODE) {
continue;
}
// Keep in mind that the same element may get removed then added to the tree again. This is handled
// inside `handleNewElements` (`this.handledElements.has(el)`).
// Also the fact that we have an array of `addedNodes` in an array of mutations may mean (idk actually)
// that we can have duplicate nodes in the array, which currently is fine thanks to
// `this.handledElements.has(el)`.
if ((tagNames as string[]).includes(node.nodeName)) {
newElements.push(node as HTMLMediaElement);
} else {
// TODO here https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
// it says "The returned list is live, which means it updates itself with the DOM tree automatically".
// Does it mean that it would be better to somehow use the `allMediaElements` variable from a few lines above?
// But here https://dom.spec.whatwg.org/#introduction-to-dom-ranges it says that upgdating live ranges can be
// costly.
for (const tagName of tagNames) {
const childMediaElements = (node as HTMLElement).getElementsByTagName(tagName);

@WofWca WofWca changed the title fix: detect <video> elements inside custom elements (web components) (shadow DOM) fix: detect <video> inside custom elements (web components) (shadow DOM) Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant