1. Mathias Panzenböck
  2. Block YouTube HTML5 Videos

Commits

Mathias Panzenböck  committed af1dc35

initial commit

  • Participants
  • Branches default

Comments (0)

Files changed (4)

File src/content.js

View file
+"use strict";
+
+function play_hook (src) {
+	return function () {
+		// unbreak player:
+		this.autoplay = true;
+		this.preload = "auto";
+		this.src = src;
+		delete this.play;
+		this.play();
+
+		// don't ask me why this is needed,
+		// it just won't work without it:
+		try {
+			// but sometimes there is no Mg(!?!)
+			// and then it works without it (whaaa?)
+			yt.player.Mg.stopVideo();
+			yt.player.Mg.playVideo();
+		}
+		catch (e) {
+			console.error(e);
+		}
+	};
+}
+
+function videoblock () {
+	var videos = document.querySelectorAll('video');
+	// don't do anything unless HTML5 video is used
+	if (videos.length === 0) return;
+	if (window.gYouTubePlayerReady) {
+		try {
+			yt.player.Mg.stopVideo();
+		}
+		catch (e) {
+			console.error(e);
+		}
+	}
+	else {
+		var f = window.onYouTubePlayerReady;
+		window.onYouTubePlayerReady = function () {
+			window.gYouTubePlayerReady = true;
+			try {
+				yt.player.Mg.stopVideo();
+			}
+			finally {
+				if (f) f();
+			}
+		};
+	}
+	var player = document.getElementById("watch-player");
+	if (player) {
+		var handler = function () {
+			try {
+				var videos = this.querySelectorAll('video');
+				for (var i = 0; i < videos.length; ++ i) {
+					videos[i].play();
+				}
+			}
+			finally {
+				this.removeEventListener("click", handler, false);
+			}
+		};
+		player.addEventListener("click", handler, false);
+	}
+	for (var i = 0; i < videos.length; ++ i) {
+		var video = videos[i];
+		var src = video.src;
+		// "break" video:
+		video.autoplay = false;
+		video.preload = "none";
+		if (!video.paused) {
+			video.pause();
+		}
+		video.src = "about:blank";
+		video.play = play_hook(src);
+	}
+}
+
+videoblock();

File src/manifest.json

View file
+{
+	"name": "Block HTML5 YouTube Videos",
+	"description": "Stops loading HTML5 videos so you can open multiple tabs without killing your network bandwith.",
+	"version": "1.0.1",
+	"content_scripts": [{
+		"matches": [
+			"http://*.youtube.com/*",
+			"https://*.youtube.com/*"
+		],
+		"js": ["content.js"],
+		"all_frames": true
+	}]
+}

File videoblock.png

Added
New image

File videoblock.svg

View file
Added
New image