Wednesday, December 19, 2018

Instagram Download Links

Hmmm..... this no longer appears to work. Needs reworking, and check here.

To view a full size Instagram picture, append

media/?size=l
, e.g.

https://www.instagram.com/p/B3hoo93BLA1UtIa4QJvMu1iyBPvX1PGbeH5XfI0/

should be updated to:

https://www.instagram.com/p/B3hoo93BLA1UtIa4QJvMu1iyBPvX1PGbeH5XfI0/media/?size=l

A simple Tampermonkey script to add a link to an Instagram page to allow the image to be downloaded.

// ==UserScript==
// @name Instagram Download Link
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add a link to download photos....
// @author Me
// @match https://www.instagram.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

var currentPage;
var domChangeTimer = '';
var listenerInstalled = false;

$(window).bind('load', MainAction);

function MainAction() {

  // add a listener to the page so this function is run each time the page changes...
  if (!listenerInstalled) {
    document.addEventListener("DOMSubtreeModified", HandleDomChange, false);
    listenerInstalled = true;
  }

  // check the current URL. This will change as AJAX changes the page...
  if (currentPage != window.location.href) {
    currentPage = window.location.href;

    // is the link to the image available...?
    var link = $('meta[property="og:image"]').attr('content');

    // if not, and the page is just of a single image
    // e.g. https://www.instagram.com/p/BuRxiC5Hr4i/
    // then reload the page so the link is available...
    // this is clumsy, but can't find another way to get at the DOM as updated by AJAX
    if (window.location.href.match(/\/p\//) && link == undefined) {
      window.location.reload();
    } else if (!window.location.href.match(/\/p\//) && link != undefined) {

      // and if we've just mmoved off a single image page
      // reload again to get rid of the image metadata from our copy of the DOM
      // that has since been updated by AJAX
      window.location.reload();
    }

    console.log("Download link : " + link);

    // if the link is available, add it to the page...
    if (link != undefined) {
      $("article").append("Download Link");
    }
  }
}

function HandleDomChange(zEvent) {

  // why do we do this here...?
  if (typeof domChangeTimer == "number") {
    clearTimeout(domChangeTimer);
    domChangeTimer = '';
  }

  // DOM has changed, so set a timer...
  // Note that this may be updated numerous times, but the function
  // will only be called when the timer hasn't been set for x ms
  domChangeTimer = setTimeout (function() {
    MainAction();
  }, 500); // in ms
}