TW Fred Timer

Fred's Timer View

当前为 2015-04-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TW Fred Timer
  3. // @name:ru TW Fred Timer
  4. // @namespace TW Fred Timer
  5. // @description Fred's Timer View
  6. // @description:ru Счет времени бандита Фреда
  7. // @include http://*.the-west.*/game.php*
  8. // @include https://*.the-west.*/game.php*
  9.  
  10. // @version 0.02 alfa
  11. // @grant none
  12.  
  13. // ==/UserScript==
  14.  
  15.  
  16. function TWFredScript(fn) {
  17. var script = document.createElement('script');
  18. script.setAttribute("type", "application/javascript");
  19. script.textContent = '(' + fn + ')();';
  20. document.body.appendChild(script);
  21. document.body.removeChild(script);
  22. }
  23.  
  24. TWFredScript(function() {
  25. var VERSION = "0.01 alfa";
  26. var NAME = "TW Fred Timer";
  27. var installURL = '';
  28. fcContainer = $("<div />",
  29. {
  30. id: "twfred_container",
  31. style: "position: absolute; top: 64px; right: 50%; margin-right: 140px; z-index: 10; width: 142px; height: 16px; background: url('/images/interface/character/character.png') no-repeat scroll left -160px transparent;"
  32. }
  33. );
  34. fcContainer.appendTo("#user-interface");
  35. fcWrap = $('<div />',
  36. {
  37. id: "twfred_wrap",
  38. style:"padding-top:1px; font-size:10px; color:#FFF; text-align:center;",
  39. }
  40. );
  41. fcContainer.append(fcWrap);
  42. TWFred = {
  43. wofid:2, // don't know where find it
  44. diff: 0,
  45. timer:2, // seconds
  46. execCount:0,
  47. reInitVal:60, // execs to reinit data - on start
  48. reInitValN:60, // execs to reinit data - on normal work
  49. reInitValI:7, // execs to reinit data - on zero Timer
  50. clickObj: null,
  51. timerId: 0,
  52. };
  53. fcWrap.click(function(){TWFred.click()});
  54. TWFred.init = function()
  55. {
  56. TWFred.execCount = 0;
  57. TWFred.reInitVal = TWFred.reInitValN;
  58. TWFred.getClicker();
  59. $.post("/game.php?window=wheeloffortune&mode=init", {wofid:TWFred.wofid},
  60. function(data)
  61. {
  62. //console.log(data);
  63. var diff = 0;
  64. var cd = data.mode.cooldowns;
  65. if (cd.length) // cuz no data at end of Event
  66. {
  67. cd = cd[0].cdstamp
  68. var stime = Game.getServerTime();
  69. diff = cd-stime;
  70. }
  71. TWFred.diff = diff;
  72. //TWFred.showTimer();
  73. TWFredTimer();
  74. }
  75. ); //post
  76. };
  77. TWFred.showTimer = function()
  78. {
  79. var str = TWFred.diff.formatDuration();
  80. fcWrap.html(str);
  81. };
  82. TWFred.doTimer = function()
  83. {
  84. //console.log('doTimer:' + TWFred.execCount + ',' + TWFred.reInitVal);
  85. TWFred.timerId = 0;
  86. if (undefined === TWFred) return;
  87. if (!TWFred.clickObj) TWFred.getClicker(); // init clicker
  88. var diff = TWFred.diff - TWFred.timer;
  89. if (diff < 0) diff = 0;
  90. if (!diff) TWFred.reInitVal = TWFred.reInitValI;
  91. TWFred.diff = diff;
  92. TWFred.showTimer();
  93. TWFred.execCount++;
  94. if (TWFred.execCount > TWFred.reInitVal)
  95. {
  96. TWFred.init();
  97. }
  98. else
  99. {
  100. TWFredTimer();
  101. }
  102. };
  103. TWFred.getClicker = function()
  104. {
  105. if (TWFred.clickObj) return;
  106. var bar = WestUi.NotiBar.getBar();
  107. if (!bar) return;
  108. bar = bar.list;
  109. TWFred.reInitVal = TWFred.reInitValN;
  110. $.each(bar, function(key,val)
  111. {
  112. //console.log(val);
  113. if (val.uid == 'easter2015')
  114. {
  115. TWFred.clickObj = val.element;
  116. }
  117. });
  118. fcWrap.css("cursor","pointer");
  119. if (!TWFred.clickObj) TWFred.destroy();
  120. };
  121. TWFred.click = function()
  122. {
  123. if (!TWFred.clickObj) return;
  124. TWFred.init();
  125. TWFred.clickObj.click();
  126. };
  127. TWFred.destroy = function()
  128. {
  129. fcContainer.remove();
  130. delete TWFred;
  131. };
  132. function TWFredTimer()
  133. {
  134. if (undefined === TWFred) return;
  135. if (!TWFred.timerId) TWFred.timerId = setTimeout(TWFred.doTimer,TWFred.timer * 1000);
  136. };
  137. TWFred.init();
  138. }
  139. );