cuzi / flickr Download Link

// ==UserScript==
// @name             flickr Download Link
// @version          3.4
// @description      Adds a download link next to the Create link and enables right click on photos.
// @homepageURL      https://openuserjs.org/scripts/cuzi/flickr_Download_Link
// @updateURL        https://openuserjs.org/meta/cuzi/flickr_Download_Link.meta.js
// @downloadURL      https://openuserjs.org/install/cuzi/flickr_Download_Link.user.js
// @contributionURL  https://buymeacoff.ee/cuzi
// @contributionURL  https://ko-fi.com/cuzicvzi
// @copyright        2014, cuzi (https://openuserjs.org/users/cuzi)
// @namespace        cuzi
// @license          MIT
// @grant            none
// @include          /^https?:\/\/www\.flickr\.com\/.*$/
// ==/UserScript==

(function() {
"use strict";

function page_photo() {

  var main_photo = document.getElementsByClassName('main-photo')[0];

  // Make the right click context menu available
  document.getElementsByClassName('main-photo')[0].style.zIndex = 10000;
  document.getElementsByClassName('main-photo')[0].parentNode.parentNode.appendChild( document.getElementsByClassName('main-photo')[0].parentNode); // Move photo a level up

  if(document.getElementsByClassName('facade-of-protection-neue').length > 0) { // Remove protection layer
    document.getElementsByClassName('facade-of-protection-neue')[0].setAttribute("style","display:inline; position:relative;");
  }


  if(document.getElementsByClassName('facade-of-protection-zoom').length > 0) { // Remove protection layer
    document.getElementsByClassName('facade-of-protection-zoom')[0].setAttribute("style","display:inline; position:relative;");
  }

  var url = main_photo.src; // URL of currently shown image
  var li, a;

  if(document.getElementsByClassName("all-sizes server-only-link").length > 0) {
    // Download link in ballon
    var balloon = document.getElementsByClassName("all-sizes server-only-link")[0].parentNode.parentNode;
    if(balloon.getElementsByTagName("li").length == 1) {
      // Download is disabled -> add download link for current resolution
      var orga = document.getElementsByClassName("all-sizes server-only-link")[0];
      a = orga.cloneNode();
      li = document.createElement("li");
      a.setAttribute("id","idllusgm2");
      a.setAttribute("href",url);
      a.setAttribute("class","server-only-link");
      a.appendChild(document.createTextNode("Download current size"));
      a.dataset.track = "";
      a.dataset.rapid_p = "";
      li.appendChild(a);
      balloon.appendChild(li);
    }
  }

  if(!document.getElementById("idllusgm")) {
    // Download link in nav bar
    li = document.createElement('li');
    li.setAttribute('id','iddllusgm3')
    li.setAttribute("title","Download current size")
    li.setAttribute("role","menuitem")
    a = document.createElement('a');
    li.appendChild(a);
    document.getElementsByClassName('nav-menu')[0].appendChild(li);
    a.setAttribute('class','gn-title');
    a.setAttribute('id','idllusgm');
    a.setAttribute('href',url);
    a.appendChild(document.createTextNode('Download'));
  } else {
    document.getElementById("idllusgm").setAttribute('href',url);
  }
}


function page_size_overview() {
  var allsizes, allsizesheader;
  if((allsizes = document.getElementById("allsizes-photo")) && allsizes.getElementsByClassName("spaceball").length > 0) {
    // Size overview page
    allsizes.removeChild(allsizes.getElementsByClassName("spaceball")[0]); // Remove protection layer -> make the right click context menu available

    if((allsizesheader = document.getElementById("all-sizes-header")) && allsizes.querySelector('img') && !document.querySelector('#all-sizes-header dl:nth-child(2) a')) {
      // Add download link to message
      var url = allsizes.querySelector('img').src
      var text = document.querySelector('#all-sizes-header dl:nth-child(2) dd');
      if(text) {
        var a = document.createElement('a');
        text.appendChild(document.createTextNode('. '))
        text.appendChild(a)
        a.setAttribute('href', url);
        a.setAttribute('download', url.split('/').pop());
        a.appendChild(document.createTextNode('Download current size'));
      }
    }
  }
}

function page_video() {
  if(document.getElementById("iddllusgm3")) { // Remove photo specific link from nav bar
    document.getElementById("iddllusgm3").parentNode.removeChild(document.getElementById("iddllusgm3"));
  }
}


function main() {

  if(document.getElementsByClassName('yui3-videoplayer-video').length > 0) {
    page_video();
  } else if(document.getElementsByClassName('main-photo').length > 0) {
    page_photo();
  } else if(document.getElementById("allsizes-photo")) {
    page_size_overview();
  }

}

window.setInterval(main, 1000);


})();