switch to mutationobserver, check child nodes

This commit is contained in:
Ilya Grigorik
2016-05-07 22:02:34 -07:00
parent b40b02b769
commit 3a3ac88e10
2 changed files with 21 additions and 14 deletions

View File

@@ -71,13 +71,6 @@
margin-top: 60px; margin-top: 60px;
} }
/* shift controller on Vine embeds: mute always visible */
/* e.g. https://twitter.com/Vine_Football/status/550614684575858688 */
.video-container .tc-videoController {
margin-top: 12px;
margin-left: 55px;
}
/* shift controller on vine.co */ /* shift controller on vine.co */
/* e.g. https://vine.co/v/OrJj39YlL57 */ /* e.g. https://vine.co/v/OrJj39YlL57 */
.video-container .vine-video-container .tc-videoController { .video-container .vine-video-container .tc-videoController {

View File

@@ -98,6 +98,7 @@ chrome.extension.sendMessage({}, function(response) {
container.style.top = this.video.offsetTop+"px"; container.style.top = this.video.offsetTop+"px";
fragment.appendChild(container); fragment.appendChild(container);
this.video.classList.add('tc-initialized');
this.video.parentElement.insertBefore(fragment, this.video); this.video.parentElement.insertBefore(fragment, this.video);
var speed = parseFloat(tc.settings.speed).toFixed(2); var speed = parseFloat(tc.settings.speed).toFixed(2);
@@ -181,14 +182,27 @@ chrome.extension.sendMessage({}, function(response) {
return false; return false;
}, true); }, true);
document.addEventListener('DOMNodeInserted', function(event) {
var node = event.target || null;
if (node && node.nodeName === 'VIDEO') {
new tc.videoController(node);
}
});
var forEach = Array.prototype.forEach; var forEach = Array.prototype.forEach;
function checkForVideo(node) {
if (node.nodeName === 'VIDEO') {
if (!node.classList.contains('tc-initialized')) {
new tc.videoController(node);
}
} else if (node.children != undefined) {
for (var i = 0; i < node.children.length; i++) {
checkForVideo(node.children[i]);
}
}
}
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
forEach.call(mutation.addedNodes, function(node) {
checkForVideo(node);
})
});
});
observer.observe(document, { childList: true, subtree: true });
var videoTags = document.getElementsByTagName('video'); var videoTags = document.getElementsByTagName('video');
forEach.call(videoTags, function(video) { forEach.call(videoTags, function(video) {
new tc.videoController(video); new tc.videoController(video);