window.alert = function (obj) {
	var iframe = document.createElement('iframe');
	iframe.src = 'javascript:void(0);'
	document.body.appendChild(iframe)
	iframe.contentWindow.alert(obj);
	iframe.parentNode.removeChild(iframe);
}

window.hugo={}
window.hugo.iframes=[]
let iframes=window.hugo.iframes;
var openIframe=function(html,body){
	// 1. 创建<iframe>元素
	var ifr = document.createElement('iframe');
//	// 2. 将CSS,HTML字符串转换为Blob对象
//	var blob = new Blob([html], {
//	  'type': 'text/html'
//	});
//	// 3. 使用URL.createObjectURL()方法将...
//	iframe.src = URL.createObjectURL(blob);
	body.innerHTML = "";
	body.appendChild(ifr);
	var ifrw = (ifr.contentWindow) ? ifr.contentWindow: (ifr.contentDocument.document) ? ifr.contentDocument.document: ifr.contentDocument;
	ifrw.document.open();
	ifrw.document.write(html);
	ifrw.document.close();
	//console.log(x)
}
//开启页面html
{

    let xx = $("#cnblogs_post_body,.cnblogs-post-body").find("[data-type=html]");
    let xxx = [];
    for (let x of xx) {
        xxx.push(x.innerText)
    };
    xx.remove();
    let URL = window.URL || window.webkitURL;
    for (let x of xxx) {
		//openIframe(x,document.body)
		iframes.push(x);
    };
}

文本存储

let commentid=4242119;
var HtmlUtil = {
    /*1.用浏览器内部转换器实现html转码*/
    htmlEncode: function (html) {
            //1.首先动态创建一个容器标签元素,如DIV
            var temp = document.createElement("div");
            //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(旧版火狐,google支持)
            (temp.textContent != undefined) ? (temp.textContent = html) : (temp.innerText = html);
            //3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了
            var output = temp.innerHTML;
            temp = null;
            return output;
        },
        /*2.用浏览器内部转换器实现html解码*/
        htmlDecode: function (text) {
            //1.首先动态创建一个容器标签元素,如DIV
            var temp = document.createElement("div");
            //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
            temp.innerHTML = text;
            //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        }
}
let sohu_upload=function(data,callback){
	data=HtmlUtil.htmlEncode(data)
	let xhr = new XMLHttpRequest();
	if(callback){

		xhr.addEventListener("load", function (e) {
			callback('ok', e);
		}, false);
		xhr.addEventListener("error", function (e) {
			callback('error', e);
		}, false);
		xhr.addEventListener("abort", function (e) {
			callback('abort', e);
		}, false);
	}
	xhr.withCredentials= true;
	xhr.open("POST", "https://changyan.sohu.com/api/2/comment/attachment");
	xhr.setRequestHeader("Content-Type","multipart/form-data; boundary=----WebKitFormBoundaryGee2QOPAgWrB0OAp");
	data='------WebKitFormBoundaryGee2QOPAgWrB0OAp\r\nContent-Disposition: form-data; name="file"; filename="1.gif"\r\nContent-Type: image/gif\r\n\r\n'+"GIF89a"+data+"\r\n------WebKitFormBoundaryGee2QOPAgWrB0OAp--"
	xhr.send(data);	
}

let comment_get= function(callback){
	 $.ajax({
		 type: "post",
		 url: "https://www.cnblogs.com/mvc/comment/GetCommentBody.aspx",
		 data: JSON.stringify({
			 commentId: commentid
		 }),
		 dataType: "text",
		 success: function (data) {
				
				 console.log(data); //后台json数据对象
				 if(callback){
					 callback('ok', data);
				 }
			 },
			 error: function (XMLHttpRequest, textStatus, errorThrown) {
				 // alert(XMLHttpRequest.status);
				 // alert(XMLHttpRequest.readyState);
				 // alert(textStatus + "ajax出错了");
				 callback('error', textStatus);
			 }

	 });
}
let comment_update= function(data,callback){
	 $.ajax({
		 type: "post",
		 url: "https://www.cnblogs.com/mvc/PostComment/Update.aspx",
		 data: JSON.stringify({
			 commentId: commentid,
			 body:data
		 }),
		 dataType: "text",
		 success: function (data) {
				 console.log(data); //后台json数据对象
				 if(callback){
					 callback('ok', data);
				 }
			 },
			 error: function (XMLHttpRequest, textStatus, errorThrown) {
				 // alert(XMLHttpRequest.status);
				 // alert(XMLHttpRequest.readyState);
				 // alert(textStatus + "ajax出错了");
				 callback('error', textStatus);
			 }

	 });
}
let sohu_download=function(jsname,callback){
	comment_get(function(state,data){
		console.log(data)
		let obj=eval("("+data+")");
		if(obj.js!=undefined && obj.js[jsname]!=undefined && obj.js[jsname].sohu!=undefined && obj.js[jsname].sohu.url!=undefined){
console.log("OK")
			$.ajax({
				 type: "get",
				 url: obj.js[jsname].sohu.url.replace("http://","https://"),
				 dataType: "text",
				 success: function (data) {
						
						 console.log(data); //后台json数据对象
						 let tag=data.substring(0,6)
						 if(tag=="GIF89a"){
							 data=HtmlUtil.htmlDecode(data.substring(6))
							 console.log(data)
							 if(callback){
								 callback('ok', data);
							 }
						 }
						 
						 
					 }

			 });
		}
		
	})	
}
let getJs=function(jsname,callback){
	sohu_download(jsname,function(state,data){
		let blob=new Blob([data]);
		let url=window.URL.createObjectURL(blob);
		if(callback){
			callback(url,data);
		}		
	});
}
let uploadJs=function(jsname,data,callback){
	sohu_upload(data,function(state,e){
		if(state=="ok"){
			console.log("ok:")
			console.log(e)
			let result=eval("("+e.target.response+")")
			if(typeof result=="string")
				result=eval("("+result+")")
			if(result.url!=undefined){
				console.log(result.url)
				comment_get(function(state,data){
					console.log(data)
					let obj=eval("("+data+")");
					console.log(obj);
					if(obj.js==undefined){
						obj.js={}
					}
					if(obj.js[jsname]==undefined){
						obj.js[jsname]={}

					}
					if(obj.js[jsname].sohu==undefined){
						obj.js[jsname].sohu={}
					}
					obj.js[jsname].sohu.url=result.url;
					data=JSON.stringify(obj)
					comment_update(data);
				})
			}
		}else if(state=="error"){
			console.log("error:")
			console.log(e)
		}else if(state=="abort"){
			console.log("abort:")
			console.log(e)
		}
	});	
}


$(function(){
	$('#get').click(function(){
		let name=$('#filename').val()
		$('#data').val("")
		getJs(name,function(url,data){
			$('#data').val(data)
		})
	})

	$('#set').click(function(){
		let name=$('#filename').val()
		let data=$('#data').val()
		uploadJs(name,data)

	})

})



posted @ 2019-04-21 01:52  离线云  阅读(357)  评论(1编辑  收藏  举报