下面來分析一下page_speed_v2.js文件的內容。對這個腳本進行重新格式化,以便更加方便閱讀。
/**
* 綁定createScript方法
*
* @param String a script標簽的src屬性的值
* @param String b script標簽的id屬性的值
*/
PageSpeed.createScript = function(a, b) {
var c = null;
if (document.getElementById(b)) { /* script標簽已經存在 */
c = document.getElementById(b)
} else { /* 不存在, 創建script標簽 */
c = document.createElement('script');
var d = null;
if (document.getElementsByTagName) {
d = document.getElementsByTagName('head')[0] || document.documentElement;
} else {
d = document.documentElement;
}
d.insertBefore(c, d.firstChild); /* 調用insertBefore方法, 將新創建的script標簽插入為第一個子元素 */
}
/* 這里的花括號起到代碼片段分組功能 */
{
c.setAttribute('type', 'text/html');
c.setAttribute('style', 'display:none;');
c.setAttribute('charset', 'gb2312');
c.setAttribute('id', b);
c.setAttribute('src', a);
}
return c; /* 返回創建成功的script標簽元素 */
};
/**
* 綁定 submitDataForPageSpeed 方法
*
* @param Object a Map對象
*/
PageSpeed.submitDataForPageSpeed = function(a) {
var b = {
'name': '',
'rate': 0
};
b.name = a.name;
b.rate = a.rate;
/* a.PAGE_SPEED_TIME數組的最后一個元素減去第一個元素 */
b['1'] = a.PAGE_SPEED_TIME[a.PAGE_SPEED_TIME.length - 1] - a.PAGE_SPEED_TIME[0];
for (var i = 1; i < a.PAGE_SPEED_TIME.length - 1; i++) {
b[(i + 1) + ''] = a.PAGE_SPEED_TIME[i] - a.PAGE_SPEED_TIME[0]
}
/* 提交速度測試結果的目的腳本 */
var c = 'http://pagespeed-ied.qq.com/r.cgi';
/* 遍歷 b 數組的元素, 構建 query string */
var d = [];
for (var p in b) {
d.push(p + '=' + b[p])
}
c += '?' + d.join('&');
/* 創建script標簽提交測速結果 */
PageSpeed.createScript(c, 'submitForPageSpeed')
};
/**
* 綁定 defaultSubmit 方法
*
*/
PageSpeed.defaultSubmit = function() {
var a = '';
try {
a = location.host
} catch(e) {}
var b = {
'name': a,
'rate': 1,
'PAGE_SPEED_TIME': []
};
if (!b.name) {
return
}
if (! (typeof(PAGE_SPEED_TIME) == 'object' && PAGE_SPEED_TIME instanceof Array)) {
return
}
/* 獲取新的時間戳 */
PAGE_SPEED_TIME.push(new Date().getTime());
b.PAGE_SPEED_TIME = PAGE_SPEED_TIME;
/*
* 沒看懂這里為啥需要一個新的變量 aaa, 直接使用 a 不可以么?
* try ... catch 結構中使用的卻是一個新的變量 aaaa 但后面卻從未使用, 為何? 是否應該是 aaa?
*/
var aaa = '';
try {
aaaa = location.host
} catch(e) {}
var c = Math.floor(Math.random() * 10000);
if (aaa == "ktv.qq.com" || aaa == "ttd.qq.com" || aaa == "tian.qq.com" || aaa == "sura.qq.com" || aaa == "gw.tnt.qq.com" || aaa == "007.qq.com") {
c = Math.floor(Math.random() * 1000);
}
/* 隨機概率提交數據 */
if (c <= b.rate * 1) {
PageSpeed.submitDataForPageSpeed(b);
}
};
/**
* 綁定submit方法(用于CDN測速?)
*
* @param String a 名稱
*/
PageSpeed.submit = function(a) {
var b = PageSpeed.cdn_page_speed_submitData;
var c = {};
for (var p in b) {
c[p] = b[p]
}
c.name = a || c.name;
PageSpeed.submitDataForPageSpeed(c);
}
})();
try {
/* 1秒后嘗試提交數據 */
setTimeout(function() {
PageSpeed.defaultSubmit()
},
1000);
} catch(e) {}
/* 最后這段注釋, 第二段是32位16進制數字, 應該是類似于 ETag 用于標記內容版本的 */
/* |xGv00|ca82276cd78ac911d3d4310ba1408236 */
|
新聞熱點
疑難解答