Flickr Original Link

Show direct links to download biggest Flickr image available and some other sizes.

As of 2014-07-09. See the latest version.

// ==UserScript==
// @name        Flickr Original Link
// @namespace   https://greasyfork.org/scripts/1190-flickr-original-link
// @include     /^https?:\/\/.*\.?flickr\.com\/photos\/.*/
// @version		4.2
// @grant       GM_getValue
// @grant       GM_setValue
// @require 	http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @description 	Show direct links to download biggest Flickr image available and some other sizes. 
// ==/UserScript==
var source = "";
var postfix = "_d.jpg";
var isChecked = "";
var key = "flickr_openLink";
var currentSetting = GM_getValue(key, false);
if (currentSetting) {
    postfix = ".jpg";
    isChecked = ' checked="checked" ';
}
else {
    postfix = "_d.jpg";
    isChecked = "";
}
var temp = '<li><div title="If this option is checked, you will OPEN the image, instead of downloading it. Nếu bạn đánh dấu tùy chọn này, khi nhấn vào link, bạn sẽ MỞ ảnh ra chứ không phải tải tự động về máy." id="optionBoxContainer" style="color:pink"><input id="optionBox" type="checkbox"'
+ isChecked + 'style="margin:5px"/>Open image link in new window</div></li>';
$('#subnav-refresh .subnav-holder ul:first').append(temp);
var action_singlephoto = function() {
    var size = document.documentElement.innerHTML.match(/"sizes":{(.+?})}/i);
    var mWidth, mHeight, mLink, mSize, length;
    var strCss = ".bigButton {display : inline-block; cursor : pointer; border-style : solid; border-width : 2px; border-radius : 50px; padding : 15px 15px; font-size : 10pt; font-weight : bold;} .smallButton { display:	 inline-block; padding: 0.6em; margin: 0.4em; background-color: pink; border-radius:1.5em;font-size:10pt}";
    mSize = size[0].match(/"width":"?\d+"?,"height":"?\d+"?,/ig);
    mLink = size[0].match(/"url":"[^"]+"/ig);
    length = mLink.length;
    for (var k = 0; k < length; k++) {
        mSize[k] = mSize[k].replace(/"width":(\d+),"height":(\d+),/i, "$1 x $2");
        mLink[k] = "http:" + mLink[k].replace(/"url":"([^"]+)"/i, "$1").replace(/\\/g, "").replace(/\.jpg/i, postfix);
    }
    $('head').append('<style>' + strCss + '</style>');
    var insertLocation = $('.sub-photo-right-row1');
    if (insertLocation.length > 0) {
        insertLocation.append('<a class="bigButton" href="' + mLink[length - 1] + '">DOWNLOAD ' + mSize[length - 1] + ' px</a>');
        for (var k = 0; k < 7; k++) {
            if (length - k - 2 > 0) insertLocation.append('<a class="smallButton" href="' + mLink[length - 2 - k] + '">' + mSize[length - 2 - k] + ' px</a>');
        }
    }
}
var pageType = function() {
    var title = $('head title').text();
    var type = "none";
    if (title.match(/flickr.+photostream/i) != null) type = 'photostream';
    else if (title.match(/an album on flickr/i) != null) type = 'photostream';
        else if (title.match(/flickr - photo sharing/i) != null) type = 'singlephoto';
        console.log("Type = " + type);
    return type;
}
// add link to photo stream view
var flickr_mouseenter = function() {
    if ($(this).find('.myFuckingLink').length > 0) return;
    var photoId = $(this).attr('data-photo-id');
    for (var i = 0; i < source.length; i++) {
        if (source[i].indexOf(photoId) == 8) {
            var link = source[i].match(/https?[^"]+/)[0].replace(/\\/g, "").replace(/\.jpg/, postfix).replace(/https/, "http");
            var size = source[i].replace(/.+"width":"?(\d+)"?,"height":"?(\d+)"?.*/, "$1 x $2 px");
            $(this).find('.attribution-block').append('<div class="myFuckingLink"><a style="display:inline-block;" href="' + link + '">GET ' + size + '</a>');
            break;
        }
    }
}
var action_photostream = function() {
    source = document.documentElement.innerHTML.match(/"file":"[^}]+}}/g);
    if (source == null) return;
    var t1 = $('a[data-track="prev"]').attr('href');
    if (t1 != null) {
        var t2 = $('span.this-page').text();
        t1 = t1.replace(/\/page\d+\//i, "/page" + t2 + "/");
        var $div = $('<div>');
        $div.load(t1, function() {
            var source2 = $(this).text().match(/"file":"[^}]+}}/g);
            source = source.concat(source2);
        });
    }
    var previous = 0;
    $('body').bind('DOMNodeInserted', function(e) {
        var photoDisplayItem = $('#photo-display-container .photo-display-item');
        if (photoDisplayItem.length > previous) {
            previous = photoDisplayItem.length;
            // console.log("Length = "+photoDisplayItem.length);
            photoDisplayItem.each(flickr_mouseenter);
        }
    });
}
$('#optionBox').change(function() {
    currentSetting = $(this).prop('checked');
    GM_setValue(key, currentSetting);
    if (currentSetting == true) {
        postfix = ".jpg";
        isChecked = ' checked="checked" ';
    }
    else {
        postfix = "_d.jpg";
        isChecked = "";
    }
    $('.myFuckingLink').remove();
    $('#photo-display-container .photo-display-item').each(flickr_mouseenter);
});
var type = pageType();
if (type == 'photostream') action_photostream();
else if (type == 'singlephoto') action_singlephoto();