Use MutationObserver

This commit is contained in:
Alexander Weidinger
2018-10-06 18:43:19 +02:00
parent 267102fa46
commit 8088d4a7a6

View File

@@ -1,9 +1,30 @@
// ==UserScript==
// @name netflix-speed
// @version 0.1
// @include https://www.netflix.com/watch/*
// @include https://www.netflix.com/*
// ==/UserScript==
function find_controls() {
return new Promise(function(resolve, reject) {
var observer = new MutationObserver(function(mutationsList) {
mutationsList.forEach(function(mutation) {
var nodes = Array.from(mutation.addedNodes);
for (var node of nodes) {
if (node.parentNode && node.parentNode.matches && node.parentNode.matches('.PlayerControlsNeo__controls-group')) {
observer.disconnect();
resolve(node.parentNode);
return;
}
}
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
});
}
function inject_controls() {
find_controls().then(function(controls) {
var speed_control = document.createElement('select');
speed_control.style.cssText = `
border:none;
@@ -14,7 +35,7 @@ cursor: pointer;
background: none;
`;
speed_control.onchange = function() {
document.getElementsByClassName('VideoContainer')[0].getElementsByTagName('video')[0].playbackRate = this.value;
document.querySelector('.VideoContainer video').playbackRate = this.value;
};
speed_control.innerHTML = `<option value=0.5>0.5</option>
<option value=0.75>0.75</option>
@@ -23,12 +44,11 @@ speed_control.innerHTML = `<option value=0.5>0.5</option>
<option value=1.5>1.5</option>
<option value=1.75>1.75</option>
<option value=2.0>2.0</option>`;
function append(){
if(!document.getElementsByClassName('PlayerControlsNeo__controls-group').length) {
setTimeout(append, 200);
} else {
console.log(document.getElementsByClassName('PlayerControlsNeo__controls-group')[0]);
document.getElementsByClassName('PlayerControlsNeo__controls-group')[0].appendChild(speed_control);
}
}
append();
controls.appendChild(speed_control);
inject_controls();
});
};
inject_controls();