Skip to content

Instantly share code, notes, and snippets.

@tpatel
Created October 26, 2013 02:12
Show Gist options
  • Select an option

  • Save tpatel/7164530 to your computer and use it in GitHub Desktop.

Select an option

Save tpatel/7164530 to your computer and use it in GitHub Desktop.
Run this in http://chat.meatspac.es to be able to favorite the best images. The images are stored on your browser. Click on one image to add it to your favorites, click on "X" next to the image to remove it, click on the image to have a bigger view.
(function() {
/*
Run this in http://chat.meatspac.es to be able to favorite the best images. The images are stored on your browser.
Click on one image to add it to your favorites, click on "X" next to the image to remove it, click on the image to have a bigger view.
Made by @thibpat (github.com/tpatel)
*/
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
var images = [];
var lsSupported = supports_html5_storage();
var disp = $('<div style="position:fixed;top:50px;right:50px;width:200px;">').appendTo('body');
//Listen the click at the body level not to have to add a listener to all images
$('body').click(function(e) {
var t = $(e.target);
if(t.parents('ul').length > 0 && t.prop('tagName') == 'IMG') {
addImage(t.attr('src'));
}
});
function template(id, content) {
$('<div style="float:left;margin-right:10px">')
.append(
$('<a target="_blank">')
.attr('href', content)
.append(
$('<img style="width:50px;vertical-align:top;">')
.attr('src', content)
)
).append(
$('<a href="#" style="text-decoration:none;color:black;font-family:sans-serif">')
.data('id', id)
.click(function(e) {
var id = $(this).data('id');
removeImage(id);
})
.text('x')
).appendTo(disp);
}
function display() {
disp.html('');
for(var i in images) {
template(i, images[i]);
}
}
function udpate() {
if(lsSupported) localStorage["favorites"] = JSON.stringify(images);
display();
}
function addImage(img) {
images.push(img);
udpate();
}
function removeImage(id) {
images.splice(id, 1);
udpate();
}
//Get data from the localStorage
if(lsSupported && localStorage["favorites"]) {
images = JSON.parse(localStorage["favorites"]);
display();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment