Skip to content

Instantly share code, notes, and snippets.

@simshaun
Last active December 10, 2017 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simshaun/8ad43a9e87a2883986bf36555b02c37b to your computer and use it in GitHub Desktop.
Save simshaun/8ad43a9e87a2883986bf36555b02c37b to your computer and use it in GitHub Desktop.
Greasemonkey / Violentmonkey userscript to stop Netflix from loading video in the hero area of the browse page.
// ==UserScript==
// @name Netflix /browse hero video preventer
// @author Shaun
// @namespace Violentmonkey Scripts
// @match *://*.netflix.com/browse
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
let observers = [];
const origXhrOpen = XMLHttpRequest.prototype.open;
function init() {
XMLHttpRequest.prototype.open = function() {
this.addEventListener("progress", function() {
if (this.responseURL.indexOf("range") !== -1) {
console.log("Shaun's Netflix script aborted loading a video.");
this.abort();
}
});
origXhrOpen.apply(this, arguments);
};
watchForVideo();
}
function teardown() {
observers.forEach(observer => observer.disconnect());
observers = [];
XMLHttpRequest.prototype.open = origXhrOpen;
}
history.onpushstate = function(state, title, url) {
if (url.indexOf("browse") === -1) {
teardown();
console.log("Shaun's Netflix script destroyed itself.");
} else {
init();
console.log("Shaun's Netflix script started running.");
}
};
window.onpopstate = function() {
if (document.location.href.indexOf("browse") !== -1) {
init();
}
};
init();
/**
* Waits for an element satisfying selector to exist, then resolves promise with the element.
* Useful for resolving race conditions.
*
* @param selector
* @returns {Promise}
*/
function elementReady(selector) {
return new Promise((resolve, reject) => {
let el = document.querySelector(selector);
if (el) {
resolve(el);
}
const observer = new MutationObserver((mutationRecords, observer) => {
// Query for elements matching the specified selector
Array.from(document.querySelectorAll(selector)).forEach(element => {
resolve(element);
observer.disconnect();
});
});
observers.push(observer);
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
});
}
function watchForVideo() {
elementReady("video").then(video => {
console.info("Shaun's Netflix script removed a video element.", video);
video.remove();
watchForVideo();
});
}
(function(history) {
var pushState = history.pushState;
history.pushState = function() {
if (typeof history.onpushstate === "function") {
history.onpushstate.apply(this, arguments);
}
return pushState.apply(history, arguments);
};
})(window.history);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment