Skip to content

Instantly share code, notes, and snippets.

@codeachange
Last active November 15, 2015 13:45
Show Gist options
  • Save codeachange/0d525ee41ebe1196ef02 to your computer and use it in GitHub Desktop.
Save codeachange/0d525ee41ebe1196ef02 to your computer and use it in GitHub Desktop.
把img替换为iframe,破解防盗链
function iframeImg(imgSelector){
var frameStr = '<iframe style="display:inline-block;width:100%;height:100%;border:0 none;"></iframe>';
/*jslint scripturl: true*/
var frameSrc = 'javascript:\'<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /><style>html,body{width:100%;height:100%;padding:0;margin:0;line-height:0;}</style></head><body></body></html>\';';
$(imgSelector).each(function (){
var imgSrc = this.src;
var iframe = $(frameStr).load(function (){
$('<img style="max-width:100%;">').load(function (){
iframe.css({width: this.width, height: this.height});
}).appendTo(iframe[0].contentWindow.document.body).attr('src', imgSrc+'?_t='+Math.random());
}).attr('src', frameSrc);
$(this).replaceWith(iframe);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment