Skip to content

Instantly share code, notes, and snippets.

@leegao
Created December 10, 2010 00:52
Show Gist options
  • Save leegao/735598 to your computer and use it in GitHub Desktop.
Save leegao/735598 to your computer and use it in GitHub Desktop.
!(???) && Profit!!!
/*
* 1. Find a picture, open it, and disable your popup blocker
* 2. Enter the following into your address bar
* 3. ???
* 4. Profit
*/
javascript:function dr(e){h=e.height;w=e.width;var d=document.createElement("canvas");d.width=w;d.height=h;ct=d.getContext("2d");ct.drawImage(e,0,0);im=ct.getImageData(0,0,w,h);dt=im.data;a=(c=Math.ceil)(w/(z=30));a=2;b=[];str='<p style = "line-height:6px;font-size:6px;padding:0;">';for(j=0;j<c(h/a);j++){for(i=0;i<c(w/a);i++){t=[[],[],[],[]];for(y=j*a;y<(j+1)*a;y++){for(x=i*a;x<(i+1)*a;x++){t[0].push(dt[(pt=y*w*4+x*4)]);t[1].push(dt[pt+1]);t[2].push(dt[pt+2]);t[3].push(dt[pt+3])}}b.push(ls=[(sm=function(f){sg=0;for(id=0;id<f.length;id++){sg+=f[id]==undefined?0:f[id]}sg=c(sg/f.length);return sg>255?255:sg})(t[0]),sm(t[1]),sm(t[2]),sm(t[3])/256]);str+="<span style='background-color:rgba("+ls[0]+","+ls[1]+","+ls[2]+","+ls[3]+")'>&nbsp;&nbsp;&nbsp;</span>"}str+="<br>"}str+="</p>";return str}function dp(f){try{nw=window.open();nw.document.writeln(f)}catch(d){alert(d)}}images=document.images;s="";for(var abc=0;abc<images.length;abc++){if((w=(image=images[abc]).width)>=30&&(h=image.height)>=30){try{s+=dr(image)}catch(err){}}}dp(s);
// Compress and add javascript: prefix, then save as a bookmarklet
function process(image){
h = image.height; w = image.width;
var buffer = document.createElement("canvas"); // Anonymous canvas
buffer.width = w;buffer.height=h;
var ct = buffer.getContext('2d');
ct.drawImage(image,0,0);
var data = ct.getImageData(0,0,w,h).data; // This may raise an exception for external images.
var pxs=2; // Height-Width of the pixel block
var str='<p style = "line-height:6px;font-size:6px;padding:0;">';
for (j=0;j<(c=Math.ceil)(h/pxs);j++){
for (i = 0; i < c(w/pxs); i++){
t = [[],[],[],[]];
// Processes a 4x4 px block and averages the color+alpha
for (y=j*pxs;y<(j+1)*pxs;y++){
for (x=i*pxs;x<(i+1)*pxs;x++){
t[0].push(data[(ptr=y*w*4+x*4)]);
t[1].push(data[ptr+1]);
t[2].push(data[ptr+2]);
t[3].push(data[ptr+3]);
}
}
var color=[(sm = function(l){sg=0;for(id=0;id<l.length;id++)sg+=l[id]==undefined?0:l[id];sg=c(sg/l.length);return sg>255?255:sg;})(t[0]),sm(t[1]),sm(t[2]),sm(t[3])/256];
str += "<span style='background-color:rgba("+color[0]+','+color[1]+','+color[2]+','+color[3]+")'>&nbsp;&nbsp;&nbsp;</span>";
}
str += "<br>";
}
str += "</p>";
return str
}
function out(dt){
try{
nw = window.open()
nw.document.writeln(dt);
}catch(err){
alert(err);
}
}
images = document.images;s="";
for(var abc = 0; abc < images.length; abc++){
if ((w=(image = images[abc]).width) >= 30 && (h=image.height) >= 30){
try{
s += dr(image);
}catch(err){
// Image is on a different subdomain
// For now, do not do anything.
}
}
}
out(s);
@tkalve
Copy link

tkalve commented Dec 10, 2010

Woah, this is awesome! Having a hard time finding anything I can use it for, though. ;)

@cheluis
Copy link

cheluis commented Dec 10, 2010

Sorry about my ignorance, but, which one is the url that I have to type?
thanks

@leegao
Copy link
Author

leegao commented Dec 10, 2010

@tkalve: haha, it's just something that I thought would be cool to do. Most of the things that I write will never be useful :P

@cheluis: Sorry about the confusion, enter the entirety of line 8 on bookmark.js in your address bar. If no images show up, then go to the address of one of the images and try again. (the HTML5 specification disallows getImageData on remote images :/)

@cheluis
Copy link

cheluis commented Dec 10, 2010

Oh this is pretty cool. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment