Instagram Save Media Bookmarklet v1.1.0

Code for Browser Bookmarklet used to quickly save post media (photo or video). Since instagram started using blobs for video this code doesn't work for major part of video in posts.

by AntowaKartowa

JavaScript

/* 
	Opens currently viewed (single) image. Currently no solution to track down blob link
	to real video link.

	IIFE prevents Instagram page to become blank in Firefox after invoking bookmarklet.
*/
(function() {
	/*
		Create object with temporary data that could be helpful between bookmarklet invokations.
	*/
	window._im ??= {};
	var im = window._im;
	
	/*
		Checks is there prevLocation already set up and is it the same or changed from
		last time it was called.
	*/
	var same = !!im?.prevLocation && im?.prevLocation === window.location.pathname; 

	/* Variables that should be recalculated only if we moved to new post */
	if (!same) {
		im.prevLocation = window.location.pathname;

		/* Narrow scope for further DOM elements search. */
		im.article = 
			document.body.querySelector('div[role=dialog] article') ||
			document.body.querySelector('article');

		/* Array of photo slider indicators visible as dots in the bottom of view box */
		im.dots = [...im.article.querySelectorAll('._acnb')];

		/* If there are many photos it holds last dot index */
		im.maxIndx = Math.max(0, im.dots.length - 1);

		/*
			Viewbox is element that holds all slides inside, but cut visible part with 
			`overflow: hidden` and other styles. Viewbox left position related to window
			needed to find correct slide if it's not first or last photo in a photo set.
			Previously when there was multiple (> 2) photos there were three slides. First
			and last slide became actual (visible) only when shows first or last photo in
			photo set. All others fall into second. But now there are four slides and
			sometimes current photo fall into second, sometimes fall into third slide and I
			couldn't get the logic behind that. So I've come up with this solution.
		*/
		im.viewBoxLeft = im.dots.length && Math.round(im.article.querySelector('div div[role=presentation]').getBoundingClientRect().left);
	}

	/*
		If there are many photos it holds the list of all slides elements. Slides elements
		are...