mmmturkeybacon Floating Timers

Adds total time elapsed, time remaining, and task time elapsed to a floating display. The task timer keeps track of page load times to more accurately reflect the amount of required to finish a HIT.

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

  1. // ==UserScript==
  2. // @name mmmturkeybacon Floating Timers
  3. // @version 1.10
  4. // @description Adds total time elapsed, time remaining, and task time elapsed to a floating display. The task timer keeps track of page load times to more accurately reflect the amount of required to finish a HIT.
  5. // @author mmmturkeybacon
  6. // @namespace http://userscripts.org/users/523367
  7. // @match https://*.mturk.com/mturk/preview*
  8. // @match https://*.mturk.com/mturk/accept*
  9. // @match https://*.mturk.com/mturk/continue*
  10. // @match https://*.mturk.com/mturk/submit*
  11. // @match https://*.mturk.com/mturk/return*
  12. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  13. // @grant unsafeWindow
  14. // ==/UserScript==
  15.  
  16. // Given:
  17. // serverTimestamp (i.e. date of now)
  18. // totalSeconds (i.e. time alloted)
  19. // endTime (i.e. expiration time)
  20. // theTime (i.e. time elapsed)
  21. // Calculate:
  22. // start_time = endTime - totalSeconds
  23. // seconds_remaining = totalSeconds - theTime;
  24.  
  25. /* Set task_start_time as soon as possible, because page load time affects how long it takes to complete
  26. * a task. */
  27. var task_start_time = localStorage.getItem('mtb_page_unload_time');
  28. if (task_start_time == null)
  29. {
  30. task_start_time = (new Date()).getTime();
  31. }
  32.  
  33.  
  34. $(document).ready(function()
  35. //$(window).load(function()
  36. {
  37.  
  38. var isAccepted = document.evaluate("//input[@type='hidden' and @name='isAccepted' and @value='true']", document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
  39.  
  40. if (isAccepted == true)
  41. {
  42. var totalSeconds = unsafeWindow.totalSeconds;
  43.  
  44. var timer_holder = document.createElement("DIV");
  45. timer_holder.style.cssText = "position: fixed; top: 0px; left: 0px; z-index: 20; background-color: black;";
  46. timer_holder.align = "right";
  47.  
  48. var timer_holder_innerHTML = '<div><span id="elapsed_timer" title="Time elapsed since HIT accepted." style="font-size: 14px; color: white; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  49. timer_holder_innerHTML += '<div><span id="remaining_timer" title="Time remaining." style="font-size: 14px; color: white; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  50. timer_holder_innerHTML += '<div><span id="task_timer" title="Time elapsed since last HIT completed. Click to reset." style="font-size: 14px; color: white; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  51. timer_holder.innerHTML = timer_holder_innerHTML;
  52. document.body.insertBefore(timer_holder, document.body.firstChild);
  53. var theTime = document.getElementById("theTime");
  54. var elapsed_timer = document.getElementById("elapsed_timer");
  55. var remaining_timer = document.getElementById("remaining_timer");
  56. var task_timer = document.getElementById("task_timer");
  57. //var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  58. var observer = new MutationObserver(function(mutations)
  59. {
  60. elapsed_timer.innerHTML = theTime.innerHTML;
  61. var task_seconds_elapsed = Math.floor(((new Date()).getTime() - task_start_time)/1000);
  62. task_timer.innerHTML = format_time(task_seconds_elapsed);
  63. if (totalSeconds)
  64. {
  65. var seconds_remaining = totalSeconds - parse_theTime(theTime.innerHTML);
  66. remaining_timer.innerHTML = format_time(seconds_remaining);
  67. }
  68. else
  69. {
  70. remaining_timer.innerHTML = "error";
  71. }
  72. });
  73. var options = {
  74. attributes: false,
  75. characterData: true,
  76. childList: true,
  77. subtree: true
  78. };
  79. observer.observe(theTime, options);
  80.  
  81. task_timer.onclick = function(){task_start_time = (new Date()).getTime(); if(typeof(Storage)!=="undefined"){localStorage.setItem('mtb_page_unload_time', task_start_time);}};
  82. window.addEventListener('beforeunload', function(){if(typeof(Storage)!=="undefined"){localStorage.setItem('mtb_page_unload_time', (new Date()).getTime());}});
  83. }
  84.  
  85. });
  86.  
  87. function parse_theTime(theTime)
  88. {
  89. var dd = 0;
  90. var hh = 0;
  91. var mm = 0;
  92. var ss = 0;
  93. var seconds = 0;
  94.  
  95. var time_split = theTime.split(/:| /);
  96.  
  97. if (time_split.length == 4)
  98. {
  99. hh = parseInt(time_split[1], 10);
  100. mm = parseInt(time_split[2], 10);
  101. ss = parseInt(time_split[3], 10);
  102. }
  103. else if (time_split.length == 6)
  104. {
  105. dd = parseInt(time_split[1], 10);
  106. hh = parseInt(time_split[3], 10);
  107. mm = parseInt(time_split[4], 10);
  108. ss = parseInt(time_split[5], 10);
  109. }
  110.  
  111. seconds = (dd*24*60*60) + (hh*60*60) + (mm*60) + ss;
  112. return seconds;
  113. }
  114.  
  115.  
  116. function format_time(time_in_seconds)
  117. {
  118. var time_str = "error";
  119.  
  120. if (time_in_seconds >= 0)
  121. {
  122. // time formatting code modified from http://userscripts.org/scripts/show/169154
  123. var days = Math.floor((time_in_seconds/(60*60*24)));
  124. var hours = Math.floor((time_in_seconds/(60*60)) % 24);
  125. var minutes = Math.floor((time_in_seconds/60) % 60);
  126. var seconds = time_in_seconds % 60;
  127.  
  128. time_str = (days == 0 ? '' : days + (days > 1 ? ' days ' : ' day '));
  129.  
  130. if (hours < 10) {hours = "0"+hours;}
  131. if (minutes < 10) {minutes = "0"+minutes;}
  132. if (seconds < 10) {seconds = "0"+seconds;}
  133.  
  134. time_str += hours + ':' +minutes + ':' + seconds;
  135. }
  136.  
  137. return time_str;
  138. }