Instagram Video Fixer

Fixes Instagram video behavior

This script is marked as "external"
This script was commisioned by someone else, and I will not check if it works. If it breaks, contact me
Click here to install Browse More Scripts
// ==UserScript==
// @name         Instagram Video Fixer
// @namespace    ef780323b1b0ddb8d0377b67d7a5f78ebf65b83f
// @version      0.2
// @description  Fixes Instagram video behavior
// @author       /u/AyrA_ch
// @include      https://*.instagram.com/*
// @include      https://instagram.com/*
// @grant        none
// @external     true
// ==/UserScript==

(function ($$) {
	//List of items to remove
	var removals = ["a.QvAa1", "div.oujXn", "img._8jZFn", "div.PyenC", "div.fXIG0"];

	var log = function () {
		args = ['Instagram Fixer'].concat(Array.prototype.slice.call(arguments));
		console.debug.apply(console, args);
	};

	//Cancels a user event
	var cancel = function (e) {
		//e.preventDefault();
		e.stopPropagation();
	};

	//Event that updates the volume across all videos
	var volumeChange = function (e) {
		var vol = (e.target.volume * 100 | 0) / 100;
		$$("video").forEach(function (v) {
			if (v !== e.target) {
				v.volume = vol;
			}
		});
		localStorage.setItem("video_volume", vol);
	};

	//Get volume from storage
	var getVolume = function () {
		var v = +localStorage.getItem("video_volume");
		return v == v ? v : 1;
	};

	//Fix page in response to changes
	var fixPage = function () {
		removals.forEach(function (v) {
			$$(v).forEach(function (v) {
				log("Removed element", v);
				v.remove();
			});
		});
		Array.prototype.filter.call($$("video"), function (v) {
			return !v.controls;
		}).forEach(function (v) {
			//Clone the video element to kill all associated events
			var n = v.cloneNode(false);
			v.parentNode.appendChild(n);
			v.parentNode.removeChild(v);
			n.controls = true;
			n.preload = "meta";
			n.volume = getVolume();
			n.addEventListener("volumechange", volumeChange);
			n.addEventListener("play", function (e) {
				var current = e.target;
				$$("video").forEach(function (v) {
					if (v !== e.target && !v.paused) {
						log("Pausing", v);
						v.pause();
					}
				});
			});
			log("Attached volume event to", v);
		});
	};

	//Trigger initial fix
	fixPage();

	var mo = new MutationObserver(fixPage);
	mo.observe(document.body, {
		subtree: true,
		attributes: true,
		childList: true
	});
})(document.querySelectorAll.bind(document));

/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.

DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/

Copyright © 2018 by Kevin Gut 📧 | More services | Generated for 13.58.39.23