tools.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var testingElement = {}, te = testingElement;
  2. te.log = function( url ) {
  3. var img = new Image(),
  4. key = 'tangram_sio_log_' + Math.floor( Math.random() * 2147483648 ).toString( 36 );
  5. // 这里一定要挂在window下
  6. // 在IE中,如果没挂在window下,这个img变量又正好被GC的话,img的请求会abort
  7. // 导致服务器收不到日志
  8. window[key] = img;
  9. img.onload = img.onerror = img.onabort = function() {
  10. // 下面这句非常重要
  11. // 如果这个img很不幸正好加载了一个存在的资源,又是个gif动画
  12. // 则在gif动画播放过程中,img会多次触发onload
  13. // 因此一定要清空
  14. img.onload = img.onerror = img.onabort = null;
  15. window[key] = null;
  16. // 下面这句非常重要
  17. // new Image创建的是DOM,DOM的事件中形成闭包环引用DOM是典型的内存泄露
  18. // 因此这里一定要置为null
  19. img = null;
  20. };
  21. // 一定要在注册了事件之后再设置src
  22. // 不然如果图片是读缓存的话,会错过事件处理
  23. // 最后,对于url最好是添加客户端时间来防止缓存
  24. // 同时服务器也配合一下传递Cache-Control: no-cache;
  25. img.src = url;
  26. };
  27. (function() {
  28. function mySetup() {
  29. /*使用注意,由于实现机理的原因,SendKeyboard方法不能在调试状态下使用
  30. (应该会导致寻找句柄和编辑器区域错误),关掉调试页面即可正常使用*/
  31. te.presskey = function( funkey, charkey ) {
  32. /*必须取到最顶层的窗口的名称,要不然取不到句柄*/
  33. var title = top.document.getElementsByTagName( 'title' )[0].innerHTML;
  34. var plugin = document.getElementById( 'plugin' );
  35. var browser = ua.getBrowser();
  36. // if(browser=='maxIE'){
  37. // title+=' - 傲游浏览器 3.1.5.1000';
  38. // }
  39. /*ie需要先触发一次空的*/
  40. if ( browser != "ie9" )
  41. plugin.sendKeyborad( browser, title, "null", "" );
  42. plugin.sendKeyborad( browser, title, funkey, charkey );
  43. };
  44. te.setClipData = function( pasteData ) {
  45. /*必须取到最顶层的窗口的名称,要不然取不到句柄*/
  46. var title = top.document.getElementsByTagName( 'title' )[0].innerHTML;
  47. var plugin = document.getElementById( 'plugin' );
  48. var browser = ua.getBrowser();
  49. plugin.setClipboard( browser, title, pasteData );
  50. };
  51. te.dom = [];
  52. te.obj = [];
  53. }
  54. function myTeardown() {
  55. if ( te ) {
  56. if ( te.dom && te.dom.length ) {
  57. for ( var i = 0; i < te.dom.length; i++ )
  58. if ( te.dom[i] && te.dom[i].parentNode )
  59. te.dom[i].parentNode.removeChild( te.dom[i] );
  60. }
  61. }
  62. }
  63. var s = QUnit.testStart, e = QUnit.testDone, ms = QUnit.moduleStart, me = QUnit.moduleEnd, d = QUnit.done;
  64. QUnit.testStart = function() {
  65. mySetup();
  66. s.apply( this, arguments );
  67. ;
  68. };
  69. QUnit.testDone = function() {
  70. e.call( this, arguments );
  71. myTeardown();
  72. };
  73. // QUnit.moduleStart = function() {
  74. // var h = setInterval(function() {
  75. // if (window && window['baidu'] && window.document && window.document.body)
  76. // {
  77. // clearInterval(h);
  78. // start();
  79. // }
  80. // }, 20);
  81. // stop();
  82. // ms.apply(this, arguments);;
  83. // };
  84. // QUnit.moduleEnd = function() {
  85. // me.call(this, arguments);
  86. // };
  87. // QUnit.done = function(fail,total) {
  88. // // d.call(this, arguments);
  89. // d(fail,total);
  90. // };
  91. })();
  92. // function Include(src) {
  93. // var url = "http://"
  94. // + location.host
  95. // + location.pathname.substring(0, location.pathname.substring(1)
  96. // .indexOf('/') + 1);
  97. // document.write("<script type='text/javascript' src='" + url
  98. // + "/src/Import.php?f=" + src + "'></script>");
  99. // }