[java]代码库
小米的javascript除了数据部分,下面就只剩 871 行的javascript了。 |
下面分模块进行分析。首先从简单的工具类入手。 |
从 616 行到最后。一共 253 行。 |
小米由于采用了一个数组,存储所有的静态字符串。所以需要手动对代码进行翻译 |
var Util = { |
time: function() { |
var a = m.$( "reback" ); |
if (count === 0x0 ) { //count应该是一个计数器,是倒计时使用的 |
m.$( "box-close" ).innerHTML = "X" ; //一个关闭的X |
a.innerHTML = "进入活动" ; |
a.className = "reback_btn_next" ; |
this .start(); |
return false |
}; |
count = count - 0x1 ; //倒计时减一 |
a.innerHTML = ”重新进入(<span id= 'initCount' >“ + count + ”</span>)“ |
|
}, |
//以上的time函数,是显示您正在排队倒计时的那个窗口,count这个值,咱们先记住,看看是从何而来 |
start: function() { |
var b = this , |
a = m.$(”reback“); //这里的m.$应该类似于jquery的选择符,选择的是对象的id |
a.onclick = function() { |
getStatus.jsonInter(CONFIG.srcs.hdget, ”hdget“, true ); //是抢购的关键函数,在这里触发抢购,如果抢购成功,页面会自动跳转,这里需要注意下:之前的抢购这个按钮是存在的,所以我点一下reback,就是稍后那个10秒到5秒的倒计时,请求真的会重新提交给服务器,而不用等30秒。18号的抢购中,却删除了这个按钮,导致我抢购失败。也让郁闷的我决定写这篇文章。 |
b.retime(); //下一个函数 |
_gaq.push([”_trackEvent“, ”活动“, ”抢购“,”进入活动“); |
a.onclick = null ; |
return false |
} |
}, |
//以上,点击开始抢购后的执行的代码,注意这里return false,你懂得。 |
retime: function() { |
m.$(”box-close“).innerHTML =” “; |
m.$(”reback“).className = ”reback_btn“; |
count = CONFIG.count //count是等待的秒数 |
}, |
//以上,进行重试的倒计时 |
changebg: function() { |
stop = false ; |
setInterval(function() { |
if (!stop) { |
m.$( "top" ).className = "topmitv" ; |
stop = true |
} else { |
m.$( "top" ).className = "topmi3" ; |
stop = false |
} |
}, |
0x1388 ) //5秒钟后自动换背景哦。 |
}, |
//以上,自动背景替换,5秒换一下 |
showVideo: function(g) { |
if (typeof document.body.style.maxHeight !== ”undefined“) { //只显示一个等待框。 |
var a = document.body.scrollWidth; |
var e = document.body.scrollHeight; |
var b = m.$(”videoBoxMask“); |
b.style.width = a + ”px“; |
b.style.height = e + ”px“; |
b.style.display = ”block“; |
b.onclick = function() { |
Util.closeBox() |
}; |
var c = ”<span class = "close" onclick= "Util.closeBox()" title= "关闭" >X“; |
var f = document.getElementById(”videoBox“); |
var d = g.getAttribute(”data-url“); |
f.innerHTML = c.replace(”{{videoUrl}}“, d); |
f.style.display =”block“; |
return false |
} |
}, |
//以上,显示风起云涌的人山人海的对话框,等吧,兄弟们。 |
closeBox: function() { |
m.$(”videoBox“).style.display = ”none“; |
m.$(”videoBox“).innerHTML =”“; |
m.$(”videoBoxMask“).style.display =”none“; |
}, |
//以上,关闭抢购排队对话框 |
getBookInfo: function(c) { //传入一个c,c是什么呢? |
var a = ”“, |
e = ”“, |
d = ”“, |
b = function(g, f, h) { |
return ”<h3>很遗憾,您没有预约“ + g + ”喔</h3>参与开放购买需要提前预约,别灰心,你可返回首页尝试购买“ + f + ”, 也可立即预约 11 月 26 日星期二开放购买</p>“ + h + ”返回活动首页“ |
}; |
switch (c) { //原来c是抢购的种类 |
case ”phone“: |
a = ”小米手机“; |
e = ”小米电视及小米盒子“; |
d = CONFIG.urls.bookPhone; |
break ; |
case ”tv“: |
a = ”小米电视“; |
e = ”小米手机及小米盒子“; |
d = CONFIG.urls.bookTv; |
break ; |
case ”box“: |
a = ”小米盒子“; |
e = ”小米手机及小米电视“; |
d = CONFIG.urls.bookBox; |
break |
}; |
m.$(”box-reg-wrap“).innerHTML = b(a, e, d); |
m.$(”box-reg-wrap“).style.background =”url(http: //p.www.xiaomi.com/open/131101/images/mitu-1.png) no-repeat 5px 0“ //来张漂亮背景 |
}, |
//以上,只是用来通知哥们没有预约。。不能抢购。 |
pushGA: function(b) { |
var a = ”“; |
switch (b) { |
case ”phone“: |
a = ”购买手机“; |
break ; |
case ”tv“: |
a = ”购买电视“; |
break ; |
case ”box“: |
a = ”购买盒子“; |
break |
}; |
_gaq.push([”_trackEvent“, ”活动“, ”抢购“, a]) |
}, |
//以上,是用来借用google的分析引擎,记录用户行为和流量 |
bookedPop: function(c, d) { //d很关键,d里面有预定的url |
var b = CONFIG.isBook, |
a = d.getAttribute(”href“); |
if (m.cookie(CONFIG.cookies.userid)) { //采用cookie进行权限验证,降低服务器负载 |
if ((c === ”phone“ && b.phone === false ) || (c === ”tv“ && b.tv === false ) || (c === ”box“ && b.box === false )) { //没预定的判断 |
this .getBookInfo(c); //提示哥们没有预定 |
getStatus.boxy( true , ”-reg“) //去预定吧 |
} else { |
window.open(a) //打开支付的页面, |
} |
} else { |
location.href = CONFIG.urls.login //悲催的没登陆。。。 |
}; |
return false |
}, |
//以上,是支付的代码 |
showBox: function(b) { |
var a = CONFIG.isBook; |
this .pushGA(b); |
if (m.cookie(CONFIG.cookies.userid)) { |
CONFIG.proType = b; |
if ((b === ”phone“ && a.phone === false ) || (b === ”tv“ && a.tv === false ) || (b === ”box“ && a.box === false )) { |
this .getBookInfo(b); |
getStatus.boxy( true , ” -reg“); |
return false |
}; |
getStatus.jsonInter(CONFIG.srcs.hdget,”hdget“, true ) |
} else { |
location.href = CONFIG.urls.login |
} |
}, |
animate: function(e, b) { |
var c = document.getElementById(e); |
var d = b; |
var a = function() { |
var i = d / 0xa ; |
var g = 0x0 ; |
var f = function() { |
g++; |
if (g == 0xa ) { |
clearInterval(h) |
}; |
var j = 0x0 - 0x5a * g; |
c.style.backgroundPosition = j +”px 0 “ |
}; |
f(); |
var h = setInterval(f, i) |
}; |
a(); |
window.loadingAnimate = setInterval(a, d) |
}, |
resetServertime: function() { |
var a = 0x1e * 0xea60 ; |
window.resetTime = setInterval(function() { |
getStatus.jsonInter(CONFIG.srcs.hdinfo, ”hdinfo“, false ) |
}, |
a) |
} |
}; |
//以上,是显示用的动画 |
var loginInfo = { |
data: { |
userId: 0x0 , |
userName: ”“ //用cookie存用户信息 |
}, |
init: function() { |
this .data.userId = m.cookie(CONFIG.cookies.userid); //取出用户名 |
if (! this .data.userId) { |
return false |
}; |
this .data.userName = ( this .data.userId) ? m.cookie(”XM_“ + this .data.userId + ”_UN“) : ”“; //这里要hack的人要注意了,是cookie的格式,应该可以伪造 |
if ( this .data.userName == null || this .data.userName == ”“) { |
var a = document.createElement(”script“); |
a.src = ”https: //account.xiaomi.com/pass/userInfoJsonP?userId=“ + this.data.userId + _$[277]; |
a.type = ”&callback=loginInfo.getAccountInfo“; |
a.async = true ; |
document.getElementsByTagName(”text/javascript“)[ 0x0 ].appendChild(a) |
} else { |
this .upUserInfo() |
} |
}, |
//以上,是右上角登陆状态的显示 |
upUserInfo: function() { |
var a = this .data.userName; |
if ( !! m.$(”LoginArea“)) { |
m.$(”LoginArea“).innerHTML = ”欢迎您 “ + a + ”!<a href= 'http://order.xiaomi.com/site/logout' >退出“; |
m.$(”LoginArea“).style.paddingLeft = ”12px“ |
} |
}, |
//以上,同上,写的好业余 |
getAccountInfo: function(b) { |
if (b.userId) { |
this .data.userName = (b.uniqName) ? b.uniqName: b.userId; |
var a = { |
path: ”/“, |
domain:”.xiaomi.com“ |
}; |
m.cookie(”XM_“+ this .data.userId + ”_UN“, this .data.userName, a); //这算掩耳盗铃吗? |
this .upUserInfo() |
} |
} |
}; |
//以上,用户信息 |
//下面是,网页进入完毕后自动执行的。参考 jquery的ready |
m.ready(function() { //越来越像改写的jquery了,为了防止冲突,加了个m |
m.phone(CONFIG.mIndex); |
getStatus.init(); |
loginInfo.init(); |
Util.changebg(); |
m.$(”box-cache-btn“).onclick = function() { |
getStatus.jsonInter(CONFIG.srcs.hdget, ”hdget“, true ) //按钮,触发抢购,然后开始等待30秒。。 |
}; |
//以上是注册点击抢购按钮的事件,点击抢购按钮,就执行getStatus.jsonInter方法 |
m.addEvent(m.$(”linksCon“), ”mouseover“, |
function() { |
m.$(”hdLnks“).style.display = ”block“; |
m.$(”linksCon-span“).style.cssText = ”background-color:#fff;color:# 333 “ |
}); |
m.addEvent(m.$(“linksCon”), "mouseout" , |
function() { |
m.$( "hdLnks" ).style.display = "none" ; |
m.$( "linksCon-span" ).style.cssText = "background:none;border:0;" |
}) |
}); |
//以上 |
//以下是自动执行代码,是给google用来做分析的 |
var _gaq = _gaq || []; |
_gaq.push([“_setAccount”, “UA- 24946561 - 1 ”]); |
_gaq.push([“_addOrganic”,“baidu”, “word”]); |
_gaq.push([“_addOrganic”, “soso”, “w”]); |
_gaq.push([“_addOrganic”, “vnet”, “kw”]); |
_gaq.push([“_addOrganic”, “sogou”, “query”]); |
_gaq.push([“_addOrganic”, “youdao”, “q”]); |
_gaq.push([“_addOrganic”, “so”, “q”]); |
_gaq.push([ "_setDomainName" , "xiaomi.com" ]); |
_gaq.push([ "_setAllowLinker" , true ]); |
_gaq.push([ "_trackPageview" ]); (function() { |
var b = document.createElement( "script" ); |
b.type = "text/javascript" ; |
b.async = true ; |
b.src = ( "https:" == document.location.protocol ? " https://ssl" : "http://www" ) + ".google-analytics.com/ga.js" ; |
var a = document.getElementsByTagName( "script" )[ 0x0 ]; |
a.parentNode.insertBefore(b, a) |
})(); |
var _msq = _msq || []; |
_msq.push([ "setDomainId" , 0x64 ]); |
_msq.push([ "trackPageView" ]); (function() { |
var a = document.createElement( "script" ); |
a.type = "text/javascript" ; |
a.async = true ; |
a.src = " http://p.www.xiaomi.com/js/xmst.js" ; |
var b = document.getElementsByTagName( "script" )[ 0x0 ]; |
b.parentNode.insertBefore(a, b) |
})(); |