文本存储

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
Content-Disposition: form-data; name="file"; filename="1.gif"
Content-Type: image/gif

'+"GIF89a"+data+"
------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)

	})

})



原文地址:https://www.cnblogs.com/mldonkey/p/10743651.html