popup.js 1.7 KB

var i = 0;
var start = false;
function imgDown() { 
	if(!start)
		return;
	var base64 = $('.rg_ic.rg_i').eq(i).attr( 'src' );
	if(typeof  base64 === "undefined" ){
		alert('마지막 결과입니다.');
		clearInterval(b);
		start = false;
		return;
	}
	if(base64.indexOf("base64") != -1)
		downBase64(i+".png",base64);
	else 
		downUrl(i+".png",base64);
	i++;
}
function scrolldn() { 
	if(!start){
		clearInterval(a);
		return;
	}
	document.documentElement.scrollTop = document.body.scrollHeight;
	$('html, body').scrollTop(document.body.scrollHeight);
}
function downBase64(filename, base64) {
	var pom = document.createElement('a');
	pom.setAttribute('href', base64)
	pom.setAttribute('download', filename);
	if (document.createEvent) {
		var event = document.createEvent('MouseEvents');
		event.initEvent('click', true, true);
		pom.dispatchEvent(event);
	}
	else {
		pom.click();
	}
}

function downUrl(filename, url) {
    var img = new Image();
    img.setAttribute('crossOrigin', 'anonymous');
    img.src = url;
	var dataURL = url;
    img.onload = function () {
        var canvas = document.createElement("canvas");
        canvas.width =this.width;
        canvas.height =this.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL("image/png");
		downBase64(filename, dataURL);
    };
}

var a;
var b;
window.onkeyup = function(e) {
   var key = e.keyCode ? e.keyCode : e.which;
   if (key == 49) {
		start = true;
		a = setInterval(scrolldn, 1000);
		b = setInterval(imgDown, 100);
   }
   if (key == 50) {
		start = true;
		alert('일시정지! 다운받던이미지부터진행함 -> 다시1누르면됨');
		clearInterval(a);
		clearInterval(b);
   }
}