Changeset View
Changeset View
Standalone View
Standalone View
extension/content-script.js
Show First 20 Lines • Show All 346 Lines • ▼ Show 20 Line(s) | 343 | document.addEventListener("DOMContentLoaded", function() { | |||
---|---|---|---|---|---|
347 | // TODO figure out somehow when a <video> tag is added dynamically and autoplays | 347 | // TODO figure out somehow when a <video> tag is added dynamically and autoplays | ||
348 | // as can happen on Ajax-heavy pages like YouTube | 348 | // as can happen on Ajax-heavy pages like YouTube | ||
349 | // could also be done if we just look for the "audio playing in this tab" and only then check for player? | 349 | // could also be done if we just look for the "audio playing in this tab" and only then check for player? | ||
350 | // cf. "checkPlayer" event above | 350 | // cf. "checkPlayer" event above | ||
351 | 351 | | |||
352 | var observer = new MutationObserver(function (mutations) { | 352 | var observer = new MutationObserver(function (mutations) { | ||
353 | mutations.forEach(function (mutation) { | 353 | mutations.forEach(function (mutation) { | ||
354 | mutation.addedNodes.forEach(function (node) { | 354 | mutation.addedNodes.forEach(function (node) { | ||
355 | if (typeof node.querySelectorAll !== "function") { | 355 | if (typeof node.matches !== "function" || typeof node.querySelectorAll !== "function") { | ||
fvogt: Skip the children check if it's a video/audio element. | |||||
356 | return; | 356 | return; | ||
fvogt: Use `node.matches("video,audio")` instead? | |||||
357 | } | 357 | } | ||
358 | | ||||
359 | // first check whether the node itself is audio/video | ||||
360 | if (node.matches("video,audio")) { | ||||
361 | registerPlayer(node); | ||||
362 | return; | ||||
363 | } | ||||
364 | | ||||
365 | // if not, check whether any of its children are | ||||
358 | var players = node.querySelectorAll("video,audio"); | 366 | var players = node.querySelectorAll("video,audio"); | ||
359 | players.forEach(function (player) { | 367 | players.forEach(function (player) { | ||
fvogt: I only notice now how stupid this function call actually is... | |||||
360 | registerPlayer(player); | 368 | registerPlayer(player); | ||
361 | }); | 369 | }); | ||
362 | }); | 370 | }); | ||
363 | }); | 371 | }); | ||
364 | }); | 372 | }); | ||
365 | 373 | | |||
366 | observer.observe(document.documentElement, { | 374 | observer.observe(document.documentElement, { | ||
367 | childList: true, | 375 | childList: true, | ||
▲ Show 20 Lines • Show All 137 Lines • Show Last 20 Lines |
Skip the children check if it's a video/audio element.