Instagram Save Media Bookmarklet v1.0.1

Code for Chrome 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

/*
Checks is there prevLocation already set up and is it the same or changed from
last time it was called. Used explicit referrence from window object to avoid
Referrence error on the first run.
*/
let same = !!window.prevLocation && window.prevLocation === location.pathname;
let prevLocation = same ? window.prevLocation : location.pathname;

/* Narrow scope for further DOM elements search. */
let article = same ? window.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 */
let dots = same ? window.dots : [...article.querySelectorAll('._acnb')];

/*
	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.
*/
let viewBoxLeft = same
  ? window.viewBoxLeft
  : dots.length && Math.floor(article.querySelector('div div[role=presentation]').getBoundingClientRect().left);
	
/* If there are many photos it holds the list of all slides elements */
let slides = same && !dots.length
	? window.slides
	: [...article.querySelectorAll('div[role=presentation] li[tabindex="-1"]')];
	
/* If there are many photos it holds last dot index */
let maxIndx = same
  ? window.maxIndx
  : Math.max(0, dots.length - 1);
	
/* If there are many photos it gets index of dot marked as active */
let dotIndx = dots.length && dots.findIndex(item => item.classList.contains('_acnf'));

/*
	If active dot index...