dc.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. * dc.js 2.0
  3. * */
  4. var _DC_Constant = {
  5. isFlashLoaded:false,
  6. rurl:'http://dc.100bt.com/dc?',
  7. startTime : new Date().getTime(),
  8. btdomain:'100bt.com',
  9. flashName:'_dcFlash',
  10. uidKey:'DC_UUID',
  11. imgckey:'DCIMGKEY_',
  12. isOpenSpeedStatic:true,
  13. timeInterval:25*60*1000,
  14. isTestMode:false,
  15. testInterval:2*1000,
  16. testImgNum:2,
  17. imgPram:[
  18. {"name":"aobi40k","url":"http://aobi.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  19. {"name":"aola40k","url":"http://aola.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  20. {"name":"aoya40k","url":"http://aoya.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  21. {"name":"zj40k", "url":"http://zj.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  22. {"name":"lds40k", "url":"http://lds.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  23. {"name":"aoqi40k","url":"http://aoqi.100bt.com/do_not_delete/bt40k.jpg", "size":40},
  24. {"name":"aola_login_40k", "url":"http://login.9aola.com/bt40k.jpg", "size":40}
  25. ],
  26. isCdnOpen:true,
  27. cdnInterval : 10*60*1000,
  28. cdnNum:3,
  29. cdnImg:[
  30. {"url":"http://zj.100bt.com/do_not_delete/cdn.gif"},
  31. {"url":"http://aobi.100bt.com/do_not_delete/cdn.gif"},
  32. {"url":"http://lds.100bt.com/do_not_delete/cdn.gif"}
  33. ]
  34. };
  35. var _DC_Util = {
  36. fixUrl : function(url){
  37. var index = url.indexOf("?");
  38. if(index>0){
  39. url = url.substring(0,index);
  40. }
  41. index = url.indexOf("#");
  42. if(index>0){
  43. url = url.substring(0,index);
  44. }
  45. return url;
  46. },
  47. coordToString : function (coord){
  48. var result = "";
  49. var flag =1; //实现格式:coordinate=(x,y)(x,y)...(x,y)
  50. var j = 0 ;
  51. for(i in coord){
  52. if(typeof(coord[i])=="object"){
  53. result += "("+coord[i].x+","+coord[i].y + ")" ;
  54. j=j+1;
  55. if(j < coord.length){
  56. result+=",";
  57. }
  58. }
  59. }
  60. return result;
  61. },
  62. objToString : function(obj){
  63. var result = "",kvpairSplit = "$;$",arrSplit = "$,$",kvSplit = "$=$";
  64. for(var i in obj){
  65. //如果对象类型为object,则进行递归
  66. if(obj.hasOwnProperty(i)){
  67. if(i=="coordinate"|i=="ncoordinate"){
  68. result += i;
  69. result += kvSplit;
  70. result += _DC_Util.coordToString(obj[i]) + kvpairSplit;
  71. }else{
  72. result += i;
  73. result += kvSplit;
  74. result += obj[i] + kvpairSplit;
  75. }
  76. }
  77. }
  78. return result;
  79. },
  80. postData : function(result){
  81. var submitURL = new Image();
  82. var submitData = encodeURI(_DC_Util.objToString(result))
  83. submitURL.src = _DC_Constant.rurl + submitData;
  84. },
  85. jsGetCookie : function(cName){
  86. var sRE = "(?:;)?" + cName + "=([^;]*);?" , oRE = new RegExp(sRE);
  87. if(oRE.test(document.cookie)){
  88. return decodeURIComponent(RegExp["$1"]);
  89. }else{
  90. return null;
  91. }
  92. },
  93. jsSetCookie : function (key,value,domain,expires){
  94. document.cookie = key+"="+value+";domain="+domain+";path=/;expires="+expires;
  95. },
  96. getFlashMovieObject : function (){
  97. var movieName = _DC_Constant.flashName;
  98. if(navigator.appName.indexOf("Microsoft")!=-1){
  99. return window[movieName];
  100. }else{
  101. return document[movieName]
  102. }
  103. },
  104. //[0,max]
  105. rand : function(max) {
  106. return Math.floor(Math.random() * (max + 1));
  107. } ,
  108. returnBase : function(number, base) {
  109. return (number).toString(base).toUpperCase();
  110. },
  111. getIntegerBits : function(val, start, end) {
  112. var base16 = _DC_Util.returnBase(val, 16);
  113. var quadArray = new Array();
  114. var quadString = '';
  115. var i = 0;
  116. for (i = 0; i < base16.length; i++) {
  117. quadArray.push(base16.substring(i, i + 1));
  118. }
  119. for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
  120. if (!quadArray[i] || quadArray[i] == '')
  121. quadString += '0';
  122. else
  123. quadString += quadArray[i];
  124. }
  125. return quadString;
  126. },
  127. modelNameIsUsed : function (userModelName){
  128. var usedModelNames=[
  129. "coordinate",
  130. "Url",
  131. "RefUrl",
  132. "UserId",
  133. "StayTime",
  134. "BrowserInfo",
  135. "TimeStamp",
  136. "PageOnloadSpeed",
  137. "GameSiteName"
  138. ];
  139. for(var i=0;i<usedModelNames.length;i++){
  140. if(usedModelNames[i] == userModelName){
  141. return true;
  142. }
  143. }
  144. return false;
  145. },
  146. createUUID : function() {
  147. var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
  148. var dc = new Date();
  149. var t = dc.getTime() - dg.getTime();
  150. var tl = _DC_Util.getIntegerBits(t, 0, 31);
  151. var tm = _DC_Util.getIntegerBits(t, 32, 47);
  152. var thv = _DC_Util.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2
  153. var csar = _DC_Util.getIntegerBits(_DC_Util.rand(4095), 0, 7);
  154. var csl = _DC_Util.getIntegerBits(_DC_Util.rand(4095), 0, 7);
  155. var n = _DC_Util.getIntegerBits(_DC_Util.rand(8191), 0, 7)
  156. + _DC_Util.getIntegerBits(_DC_Util.rand(8191), 8, 15)
  157. + _DC_Util.getIntegerBits(_DC_Util.rand(8191), 0, 7)
  158. + _DC_Util.getIntegerBits(_DC_Util.rand(8191), 8, 15)
  159. + _DC_Util.getIntegerBits(_DC_Util.rand(8191), 0, 15); // this last number is two octets long
  160. return tl + tm + thv + csar + csl + n;
  161. },
  162. addUnbeforeLoadEvent : function(func){
  163. var beforeloadFunc = window.onbeforeunload;
  164. if(typeof beforeloadFunc != 'function'){
  165. window.onbeforeunload = func;
  166. }else{
  167. window.onbeforeunload = function(){
  168. beforeloadFunc();
  169. func();
  170. }
  171. }
  172. },
  173. addUnloadEvent : function(func){
  174. var unloadFunc = window.onunload;
  175. if(typeof unloadFunc != 'function'){
  176. window.onunload = func;
  177. }else{
  178. window.onunload = function(){
  179. unloadFunc();
  180. func();
  181. }
  182. }
  183. },
  184. bindEvent:function(element, event, handle) {
  185. element.attachEvent ? element.attachEvent("on" + event, function(event) {
  186. handle.call(element, event)
  187. }) : element.addEventListener && element.addEventListener(event, handle, false)
  188. },
  189. addLoadEvent : function(func){
  190. var onloadFunc = window.onload;
  191. if(typeof onloadFunc != 'function'){
  192. window.onload = func;
  193. }else{
  194. window.onload = function(){
  195. onloadFunc();
  196. func();
  197. }
  198. }
  199. }
  200. };
  201. var user_result={};
  202. function setUserModelValue(name,value){
  203. if(!_DC_Util.modelNameIsUsed()){
  204. user_result[name]=value;
  205. }
  206. }
  207. function DC_SendUserModelData(){
  208. _DC_Util.postData(user_result);
  209. }
  210. function Dc_SetButtonClickData(gameName,buttonName){
  211. var result = {};
  212. var url = document.location.href;
  213. url = _DC_Util.fixUrl(url);
  214. result.Url = url+"@gameName="+gameName+"&buttonName="+buttonName;
  215. _DC_Util.postData(result);
  216. }
  217. function Dc_SetUserCanyuData(gameName,dc_userId){
  218. var result = {};
  219. var url = document.location.href;
  220. url = _DC_Util.fixUrl(url);
  221. result.Url = url+"@gameName="+gameName+"&DC_UserId="+dc_userId;
  222. _DC_Util.postData(result);
  223. }
  224. (function(){
  225. function jsLoadFlash (callback){
  226. if(!_DC_Constant.isFlashLoaded) {
  227. var div = document.createElement("div");
  228. div.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="" id="'+_DC_Constant.flashName+'" width=1 height=1><param name=movie value="http://dc.100bt.com/flash/cookie.swf"><param name="allowScriptAccess" value="always"><embed play=false swliveconnect="true" name="'+_DC_Constant.flashName+'" src="http://dc.100bt.com/flash/cookie.swf" quality=high bgcolor=#FFFFFF width=1 height=1 type="application/x-shockwave-flash"></embed ></object >';
  229. document.body.appendChild(div);
  230. //每隔5ms判断一下flash是否加载完全,让flash加载完后再调用callback函数
  231. var t = setInterval(function(){
  232. if(_DC_Util.getFlashMovieObject().isFlashLoaded){
  233. clearInterval(t);
  234. if(callback){
  235. callback.call(new DC());
  236. }
  237. _DC_Constant.isFlashLoaded = true;
  238. }
  239. },5);
  240. }
  241. }
  242. function _DC_Result(){
  243. this.result = {};
  244. };
  245. _DC_Result.prototype = {
  246. getUrl : function(){
  247. this.result.Url = decodeURI(window.location.href).replace(/#/g, '@');
  248. },
  249. getRefUrl : function(){
  250. this.result.RefUrl = decodeURI(document.referrer);
  251. },
  252. getUserId : function(){
  253. if(_DC_Util.jsGetCookie("userId")){
  254. this.result.UserId = _DC_Util.jsGetCookie("userId");
  255. }
  256. },
  257. getTtqId : function(){
  258. if(window.ttqId){
  259. this.result.ttqId = window.ttqId;
  260. }
  261. },
  262. getBrowserInfo:function(){
  263. this.result.BrowserInfo = navigator.userAgent;
  264. },
  265. getTimeStamp:function(){
  266. this.result.TimeStamp = new Date().getTime();
  267. },
  268. getScreenInfo:function(){
  269. this.result.ScreenWidth = screen.width;
  270. this.result.ScreenHeight = screen.height;
  271. },
  272. getStayTime : function(){
  273. var et = new Date();
  274. this.result.StayTime = et - _DC_Constant.startTime;
  275. },
  276. getUuid : function(){
  277. var cookieKey = _DC_Constant.uidKey;
  278. var cookieValue ;
  279. var cookieExpire = new Date();
  280. cookieExpire.setFullYear(2050, 12, 1);
  281. //如果flash不能被加载,则使用老的一套流程
  282. if(!_DC_Constant.isFlashLoaded){
  283. cookieValue = _DC_Util.jsGetCookie(cookieKey); //存在则直接在HTTPCookie中取值
  284. if(!cookieValue){
  285. cookieValue = _DC_Util.createUUID();
  286. _DC_Util.jsSetCookie(cookieKey, cookieValue, _DC_Constant.btdomain, cookieExpire.toGMTString());
  287. this.result.FDC_UUID = cookieValue
  288. this.result.DC_UUID = cookieValue;
  289. }else{
  290. this.result.DC_UUID = cookieValue;
  291. }
  292. return;
  293. }
  294. var flashObj = _DC_Util.getFlashMovieObject() ;
  295. //如果HTTPCookie为空,则检查flashCookie是否存在
  296. if(document.cookie.indexOf(cookieKey)<0){
  297. //如果flashCookie存在,则直接把flashCookie的值设置到HTTPCookie中去
  298. if(flashObj.isFCookieExist(cookieKey)){
  299. cookieValue = flashObj.getFCookies(cookieKey);
  300. _DC_Util.jsSetCookie(cookieKey, cookieValue, _DC_Constant.btdomain, cookieExpire.toGMTString());
  301. }
  302. // 如果flashCookie也不存在,则由js直接产生一个新的值,分别同时设置到flashCookie和HTTPCookie中
  303. else{
  304. cookieValue = _DC_Util.createUUID();
  305. _DC_Util.jsSetCookie(cookieKey, cookieValue, _DC_Constant.btdomain, cookieExpire.toGMTString());
  306. flashObj.saveFCookies(cookieKey,cookieValue);
  307. this.result.FDC_UUID = cookieValue
  308. }
  309. }else{
  310. cookieValue = _DC_Util.jsGetCookie(cookieKey); //存在则直接在HTTPCookie中取值
  311. }
  312. this.result.DC_UUID = cookieValue;
  313. },
  314. clearResult : function(){
  315. this.result = {};
  316. },
  317. initLoadData : function(){
  318. this.getUrl();
  319. this.getRefUrl();
  320. this.getBrowserInfo();
  321. this.getScreenInfo();
  322. this.getUserId();
  323. this.getTimeStamp();
  324. this.getUuid();
  325. this.getTtqId();
  326. this.result.us = _DC_Util.rand(Math.pow(2,32)); //为了计算pv,而单独区分url
  327. },
  328. initUnloadData : function(){
  329. this.getBrowserInfo();
  330. this.getScreenInfo();
  331. this.getRefUrl();
  332. this.getStayTime();
  333. this.result.uns = _DC_Util.rand(Math.pow(2,32)); //为了区分us
  334. },
  335. postData : function(){
  336. _DC_Util.postData(this.result);
  337. }
  338. }
  339. _DC_Result.testCdnMonitor = function(){
  340. if(_DC_Constant.isCdnOpen){
  341. DC_CdnMonitor(_DC_Constant.cdnInterval);
  342. }
  343. function DC_CdnMonitor(interval){
  344. setTimeout(function(){
  345. var imgs = _DC_Constant.cdnImg;
  346. var sendImgs = [];
  347. var n = 0;
  348. while(n<_DC_Constant.cdnNum){
  349. var i = _DC_Util.rand(imgs.length-1);
  350. var imgObj = imgs[i];
  351. if(imgObj!=undefined){
  352. delete(imgs[i]);
  353. n++;
  354. sendImgs.push(imgObj);
  355. }
  356. }
  357. var t = setInterval(function(){
  358. var img = sendImgs.shift();
  359. DC_CreateImg(img);
  360. if(sendImgs.length==0){
  361. clearInterval(t);
  362. }
  363. },2000);
  364. },interval);
  365. }
  366. function DC_CreateImg(imgObj){
  367. var imgSrc = imgObj.url
  368. //每十分钟一个版本号,不带日期,如 当前时间为 13:12,则版本号为 131
  369. var day = new Date();
  370. var hour = day.getHours();
  371. var min = (day.getMinutes() - day.getMinutes()%10)/10;
  372. var version = hour+""+min;
  373. var result = {}
  374. var img = document.createElement("img");
  375. //为请求资源加上一个随机数,避免因为浏览器缓存而请求不到最新的资源
  376. img.setAttribute("src",imgSrc+"?"+ version);
  377. img.setAttribute("width","0");
  378. img.setAttribute("heigth","0");
  379. img.setAttribute("style","display:none;visibility:hidden;width:0;height:0;overflow:hidden;");
  380. img.style.display = "none";
  381. document.body.appendChild(img);
  382. }
  383. }
  384. _DC_Result.getPageLoadSpeed = function(){
  385. if(_DC_Constant.isOpenSpeedStatic){
  386. if(_DC_Constant.isTestMode){
  387. //测试模式,用于测试数据是否出来
  388. DC_TestSpeed(_DC_Constant.testInterval);
  389. }else{
  390. DC_TestSpeed(_DC_Constant.timeInterval);
  391. }
  392. }
  393. function DC_TestSpeed(interval){
  394. setTimeout(function(){
  395. var imgs = _DC_Constant.imgPram;
  396. var sendImgs = [];
  397. var n = 0;
  398. while(n<_DC_Constant.testImgNum){
  399. var i = _DC_Util.rand(imgs.length-1);
  400. var imgObj = imgs[i];
  401. if(imgObj!=undefined){
  402. delete(imgs[i]);
  403. n++;
  404. sendImgs.push(imgObj);
  405. }
  406. }
  407. var t = setInterval(function(){
  408. var img = sendImgs.shift();
  409. if(_DC_Util.jsGetCookie(_DC_Constant.imgckey+img.name)==null){
  410. DC_CreateImg(img);
  411. setImgExpireCookie(img);
  412. }
  413. if(sendImgs.length==0){
  414. clearInterval(t);
  415. }
  416. },2000);
  417. },interval);
  418. }
  419. function DC_CreateImg(imgObj){
  420. var imgSrc = imgObj.url
  421. var startTime = new Date();
  422. //每十分钟一个版本号,不带日期,如 当前时间为 13:12,则版本号为 131
  423. var day = new Date();
  424. var hour = day.getHours();
  425. var min = (day.getMinutes() - day.getMinutes()%10)/10;
  426. var version = hour+""+min;
  427. var result = {}
  428. var img = document.createElement("img");
  429. //为请求资源加上一个随机数,避免因为浏览器缓存而请求不到最新的资源
  430. img.setAttribute("src",imgSrc+"?"+ version);
  431. img.setAttribute("width","0");
  432. img.setAttribute("heigth","0");
  433. img.setAttribute("style","display:none;visibility:hidden;width:0;height:0;overflow:hidden;");
  434. img.style.display = "none";
  435. img.onload = function(){
  436. var imgSize = imgObj.size;
  437. var endTime = new Date();
  438. var loadTime = endTime - startTime;
  439. var speed = imgSize*1000/loadTime;
  440. //以下操作是令结果更加精确,先乘10在取整,再除10得到结果
  441. var Lnum = Math.pow(10,1);
  442. result.PageOnloadSpeed = Math.round(speed*Lnum)/Lnum+"kb/s";//这就是结果,多少kb/s,记录这个数据
  443. result.GameSiteName = imgObj.name ;
  444. result.loadTime = loadTime;
  445. result.imgSize = imgSize;
  446. _DC_Util.postData(result);
  447. };
  448. document.body.appendChild(img);
  449. }
  450. //
  451. function setImgExpireCookie(imgObj){
  452. var name = imgObj.name;
  453. var cookieExpire = new Date();
  454. cookieExpire.setHours(cookieExpire.getHours()+1);
  455. _DC_Util.jsSetCookie(_DC_Constant.imgckey+name,name, _DC_Constant.btdomain, cookieExpire)
  456. }
  457. };
  458. _DC_Result.getCoorClick = function(){
  459. var px=0, py=0;
  460. var obj = document.documentElement;
  461. var temp_result = {}
  462. temp_result.coordinate = [] ;
  463. temp_result.ncoordinate = [] ;
  464. obj.onclick = function(event)
  465. {
  466. var Coordinate = new Object();
  467. var NewCoordinate = new Object();
  468. var width = window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth;
  469. if(temp_result.coordinate.length==10){
  470. _DC_Util.postData(temp_result);
  471. temp_result.coordinate = [] ;
  472. temp_result.ncoordinate = [] ;
  473. }
  474. //判断ie的兼容性
  475. if(!event) {
  476. event = window.event;
  477. }
  478. if(document.all) { // is ie
  479. px = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
  480. py = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  481. px += event.clientX||0;
  482. py += event.clientY||0;
  483. } else {
  484. px = event.pageX||0; // ||0 是为了防止pageX没初始化就赋值给px,出现NaN问题
  485. py = event.pageY||0;
  486. }
  487. Coordinate.x = px;
  488. Coordinate.y = py;
  489. temp_result.coordinate.push(Coordinate);
  490. /*新坐标点击*/
  491. NewCoordinate.x = px - width / 2; //以中线为标准
  492. NewCoordinate.y = py;
  493. temp_result.ncoordinate.push(NewCoordinate);
  494. }
  495. temp_result.ScreenWidth = screen.width;;
  496. temp_result.ScreenHeight = screen.height;
  497. _DC_Util.addUnbeforeLoadEvent(function(){
  498. if(temp_result.coordinate.length>0)
  499. _DC_Util.postData(temp_result);
  500. });
  501. _DC_Util.addUnloadEvent(function(){
  502. if(temp_result.coordinate.length>0)
  503. _DC_Util.postData(temp_result);
  504. });
  505. };
  506. _DC_Util.addLoadEvent(function(){
  507. _DC_Result.getPageLoadSpeed();
  508. _DC_Result.getCoorClick();
  509. _DC_Result.testCdnMonitor();
  510. // jsLoadFlash();
  511. var dc = new _DC_Result();
  512. dc.initLoadData();
  513. dc.postData();
  514. });
  515. _DC_Util.addUnbeforeLoadEvent(function(){
  516. var dc = new _DC_Result();
  517. dc.initUnloadData();
  518. dc.postData();
  519. setTimeout(function(){},1000);
  520. });
  521. //用于解决Firefox,360浏览器发送请求丢失问题
  522. _DC_Util.addUnloadEvent(function(){
  523. var isFirefox = navigator.userAgent.indexOf("Firefox") ;
  524. var is360se = navigator.userAgent.toLowerCase().indexOf("360se") ;
  525. var sendClosed = true;
  526. if(isFirefox >0 || is360se>-1){
  527. sendClosed = false ;
  528. }
  529. //若既不是Firefox也不是360,则直接返回
  530. if(sendClosed){return ;}
  531. var dc = new _DC_Result();
  532. dc.InitUnloadData();
  533. dc.postData();
  534. setTimeout(function(){},1000);
  535. });
  536. })();