Created
September 8, 2014 06:48
-
-
Save pdasika82/7b4f3a8ab1f238a8b5e1 to your computer and use it in GitHub Desktop.
Bookmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SITE_CSS = "/css/marklet.css"; | |
var SITE_URL = "http://site.com"; | |
var SITE_REDIRECT_PATH = "/mark/?img="; | |
if (typeof jQuery == 'undefined') { | |
var jQ = document.createElement('script'); | |
jQ.type = 'text/javascript'; | |
jQ.onload = runthis; | |
jQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; | |
document.body.appendChild(jQ); | |
} | |
else { | |
runthis(); | |
} | |
function runthis() { | |
jQuery(document).ready(function() { | |
jQuery('html, body').animate({ | |
scrollTop: 0 | |
}, | |
'fast'); | |
/* Check whether Bookmarklet is already visible */ | |
if (jQuery('#image-grabber-container').length == 0) { | |
var numberOfImages = 0; | |
var cssUrl = SITE_URL+SITE_CSS; | |
/*Add css - Must change this if you want to use your own CSS*/ | |
jQuery('<style type="text/css">@import url("'+cssUrl+'");</style>').appendTo("head"); | |
/*Add toggle*/ | |
jQuery('body').append('<div id="background-blocker"></div><div id="image-grabber-container"><div id="button-toggle"><span id="close" style="float:right">Close</span><span id="count"></span></div><ul id="list-of-images"></ul></div>'); | |
/*Make toggle work*/ | |
jQuery("#button-toggle").click(function() { | |
jQuery("#QuteMarklet,#image-grabber-container, #background-blocker").remove(); | |
}); | |
/*Find images and add to list*/ | |
jQuery('img').each(function() { | |
var _sudoThing = jQuery(this); | |
addImage(_sudoThing); | |
}); | |
jQuery('#count').text(numberOfImages); | |
} | |
function addImage(imageToAdd) { | |
var imageURL = imageToAdd.attr('src'); | |
//console.log(imageURL); | |
if (imageURL === undefined) { | |
return; | |
} | |
var beginLiTag = "<li><a href='"; | |
var endATag = "'>"; | |
var beginImageTag = "<img src='"; | |
var middleImageTag = "' style='margin-top:"; | |
var endImageTag = "px'>"; | |
var endLiTag = "</a></li>"; | |
var imageWidth = imageToAdd.width(); | |
var imageHeight = imageToAdd.height(); | |
var containData = imageURL.indexOf('data:'); | |
var absoluteUrl = imageURL; | |
if(!(imageURL.substring(0,7) == 'http://' || imageURL.substring(0,2) == '//')){ | |
absoluteUrl = document.location.protocol+'//'+document.location.hostname + imageURL; | |
} | |
var refererUrl = window.location; | |
/*Check whether image big enough*/ | |
if (imageWidth > 120 && imageHeight > 120 && containData === - 1) { | |
/*Calculate margin to vertically center*/ | |
if (imageWidth > imageHeight) { | |
var calculatedMargin = (200 - (200 * (imageHeight / imageWidth))) * 0.5; | |
} | |
else { | |
calculatedMargin = 0; | |
} | |
var redirectUrl = SITE_URL+SITE_REDIRECT_PATH+encodeURIComponent(absoluteUrl) | |
+'&url='+encodeURIComponent(refererUrl)+'&wd='+imageWidth+'&ht='+imageHeight; | |
var finalLink = beginLiTag + redirectUrl + endATag + beginImageTag + imageURL + middleImageTag + calculatedMargin + endImageTag + "<span>" + imageWidth + " x " + imageHeight + "</span>" + endLiTag; | |
jQuery('#list-of-images').append(finalLink); | |
numberOfImages++; | |
} | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment