change recursive function and change variables that use querySelector.

This commit is contained in:
Jonathan Dawson
2019-11-21 16:54:09 -06:00
parent ad86b01cc6
commit 20a15f8da5

View File

@@ -180,7 +180,9 @@
var observer=new MutationObserver((mutations) => { var observer=new MutationObserver((mutations) => {
mutations.forEach((mutation) => { mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && (mutation.attributeName === 'src' || mutation.attributeName === 'currentSrc')){ if (mutation.type === 'attributes' && (mutation.attributeName === 'src' || mutation.attributeName === 'currentSrc')){
var controller = document.querySelector(`div[data-vscid="${this.id}"]`); var controller = getShadow(document.body).filter(x => {
return x.attributes['data-vscid'] && x.tagName == 'DIV' && x.attributes['data-vscid'].value==`${id}`
})[0]
if(!controller){ if(!controller){
return; return;
} }
@@ -269,7 +271,7 @@
this.parent.parentElement.insertBefore(fragment, this.parent); this.parent.parentElement.insertBefore(fragment, this.parent);
break; break;
case (location.hostname == 'tv.apple.com'): case (location.hostname == 'tv.apple.com'):
// insert after parent for correct stacking context // insert after parent for correct stacking context
this.parent.getRootNode().host.prepend(fragment); this.parent.getRootNode().host.prepend(fragment);
default: default:
@@ -327,33 +329,23 @@
return true; return true;
} }
} }
function queryShadowVideo(element) { function getShadow(parent) {
const walker = document.createTreeWalker( let result = []
element, function getChild(parent) {
NodeFilter.SHOW_ELEMENT, if (parent.firstElementChild) {
{ acceptNode: function(node) { var child = parent.firstElementChild
if (node.shadowRoot) { do {
return NodeFilter.FILTER_ACCEPT; result = result.concat(child)
} getChild(child)
return NodeFilter.FILTER_SKIP; if (child.shadowRoot) {
}} result = result.concat(getShadow(child.shadowRoot))
); }
child = child.nextElementSibling
let list = []; } while (child)
}
if (element.shadowRoot) { }
list = list.concat(queryShadowVideo(element.shadowRoot)) getChild(parent)
} return result
while (walker.nextNode()) {
let video = walker.currentNode.shadowRoot.querySelector('video')
if (video) {
list.push(video);
}
list = list.concat(queryShadowVideo(walker.currentNode.shadowRoot))
}
return list;
} }
function initializeNow(document) { function initializeNow(document) {
@@ -403,11 +395,7 @@
} }
// Ignore keydown event if typing in a page without vsc // Ignore keydown event if typing in a page without vsc
if (document.querySelector('apple-tv-plus-player')) { if (!getShadow(document.body).filter(x => x.tagName == 'vsc-controller')) {
if (queryShadowVideo(document.querySelector('apple-tv-plus-player')).length == 0) {
return false;
}
} else if (!document.querySelector(".vsc-controller")) {
return false; return false;
} }
@@ -466,11 +454,10 @@
break; break;
case 'attributes': case 'attributes':
if (mutation.attributeName == 'aria-hidden' && (mutation.target.tagName == 'APPLE-TV-PLUS-PLAYER') && (mutation.target.attributes['aria-hidden'].value == "false")) { if (mutation.attributeName == 'aria-hidden' && (mutation.target.tagName == 'APPLE-TV-PLUS-PLAYER') && (mutation.target.attributes['aria-hidden'].value == "false")) {
var node = queryShadowVideo(document.querySelector('apple-tv-plus-player'))[0] var flattenedNodes = getShadow(document.body)
if (!node.previousElementSibling) { var node = flattenedNodes.filter(x => x.tagName == 'VIDEO')[0]
if (!flattenedNodes.filter(x => x.className == 'vsc-controller')[0]) {
checkForVideo(node, node.parentNode || mutation.target, true); checkForVideo(node, node.parentNode || mutation.target, true);
} else {
checkForVideo(node, node.parentNode || mutation.target, false);
} }
} }
break; break;
@@ -504,12 +491,12 @@
} }
function runAction(action, document, value, e) { function runAction(action, document, value, e) {
if (document.querySelector('apple-tv-plus-player')) { if (tc.settings.audioBoolean) {
var mediaTags = queryShadowVideo(document.querySelector('apple-tv-plus-player')) var mediaTags = getShadow(document.body).filter(x => {
} else if (tc.settings.audioBoolean) { return x.tagName == 'AUDIO' || x.tagName == 'VIDEO'
var mediaTags = document.querySelectorAll('video,audio'); });
} else { } else {
var mediaTags = document.querySelectorAll('video'); var mediaTags = getShadow(document.body).filter(x => x.tagName == 'VIDEO');;
} }
mediaTags.forEach = Array.prototype.forEach; mediaTags.forEach = Array.prototype.forEach;
@@ -521,8 +508,9 @@
mediaTags.forEach(function(v) { mediaTags.forEach(function(v) {
var id = v.dataset['vscid']; var id = v.dataset['vscid'];
var controller = document.querySelector(`div[data-vscid="${id}"]`); var controller = getShadow(document.body).filter(x => {
return x.attributes['data-vscid'] && x.tagName == 'DIV' && x.attributes['data-vscid'].value==`${id}`
})[0]
// Don't change video speed if the video has a different controller // Don't change video speed if the video has a different controller
if (e && !(targetController == controller)) { if (e && !(targetController == controller)) {
return; return;
@@ -554,7 +542,6 @@
controller.classList.add('vsc-manual'); controller.classList.add('vsc-manual');
controller.classList.toggle('vsc-hidden'); controller.classList.toggle('vsc-hidden');
} else if (action === 'blink') { } else if (action === 'blink') {
console.log(controller)
// if vsc is hidden, show it briefly to give the use visual feedback that the action is excuted. // if vsc is hidden, show it briefly to give the use visual feedback that the action is excuted.
if(controller.classList.contains('vsc-hidden') || controller.blinkTimeOut !== undefined){ if(controller.classList.contains('vsc-hidden') || controller.blinkTimeOut !== undefined){
clearTimeout(controller.blinkTimeOut); clearTimeout(controller.blinkTimeOut);