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-03-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name mmmturkeybacon Floating Timers
  3. // @author mmmturkeybacon
  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. // @namespace http://userscripts.org/users/523367
  6. // @match https://*.mturk.com/mturk/preview*
  7. // @match https://*.mturk.com/mturk/accept*
  8. // @match https://*.mturk.com/mturk/continue*
  9. // @match https://*.mturk.com/mturk/submit*
  10. // @match https://*.mturk.com/mturk/return*
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  12. // @version 1.09
  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.addEventListener('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 = "position: fixed; top: 0px; left: 0px; z-index: 20; background-color: black;";
  46. var timer_holder_innerHTML = '<div><span id="elapsed_timer" title="Time elapsed since HIT accepted." style="position: fixed; top: 0px; left: 0px; z-index: 20; font-size: 14px; color: white; background-color: black; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  47. timer_holder_innerHTML += '<div><span id="remaining_timer" title="Time remaining." style="position: fixed; top: 14px; left: 0px; z-index: 20; font-size: 14px; color: white; background-color: black; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  48. timer_holder_innerHTML += '<div><span id="task_timer" title="Time elapsed since last HIT completed. Click to reset." style="position: fixed; top: 28px; left: 0px; z-index: 20; font-size: 14px; color: white; background-color: black; font-family: verdana,arial,sans-serif; text-align: right;"></span></div>';
  49. timer_holder.innerHTML = timer_holder_innerHTML;
  50. document.body.insertBefore(timer_holder, document.body.firstChild);
  51. var theTime = document.getElementById("theTime");
  52. var elapsed_timer = document.getElementById("elapsed_timer");
  53. var remaining_timer = document.getElementById("remaining_timer");
  54. var task_timer = document.getElementById("task_timer");
  55. //var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  56. var observer = new MutationObserver(function(mutations)
  57. {
  58. elapsed_timer.innerHTML = theTime.innerHTML;
  59. var task_seconds_elapsed = Math.floor(((new Date()).getTime() - task_start_time)/1000);
  60. task_timer.innerHTML = format_time(task_seconds_elapsed);
  61. if (totalSeconds)
  62. {
  63. var seconds_remaining = totalSeconds - parse_theTime(theTime.innerHTML);
  64. remaining_timer.innerHTML = format_time(seconds_remaining);
  65. }
  66. else
  67. {
  68. remaining_timer.innerHTML = "error";
  69. }
  70. });
  71. var options = {
  72. attributes: false,
  73. characterData: true,
  74. childList: true,
  75. subtree: true
  76. };
  77. observer.observe(theTime, options);
  78.  
  79. task_timer.onclick = function(){task_start_time = (new Date()).getTime(); if(typeof(Storage)!=="undefined"){localStorage.setItem('mtb_page_unload_time', task_start_time);}};
  80. window.addEventListener('beforeunload', function(){if(typeof(Storage)!=="undefined"){localStorage.setItem('mtb_page_unload_time', (new Date()).getTime());}});
  81. }
  82.  
  83. });
  84.  
  85. function parse_theTime(theTime)
  86. {
  87. var dd = 0;
  88. var hh = 0;
  89. var mm = 0;
  90. var ss = 0;
  91. var seconds = 0;
  92.  
  93. var time_split = theTime.split(/:| /);
  94.  
  95. if (time_split.length == 4)
  96. {
  97. hh = parseInt(time_split[1], 10);
  98. mm = parseInt(time_split[2], 10);
  99. ss = parseInt(time_split[3], 10);
  100. }
  101. else if (time_split.length == 6)
  102. {
  103. dd = parseInt(time_split[1], 10);
  104. hh = parseInt(time_split[3], 10);
  105. mm = parseInt(time_split[4], 10);
  106. ss = parseInt(time_split[5], 10);
  107. }
  108.  
  109. seconds = (dd*24*60*60) + (hh*60*60) + (mm*60) + ss;
  110. return seconds;
  111. }
  112.  
  113.  
  114. function format_time(time_in_seconds)
  115. {
  116. var time_str = "error";
  117.  
  118. if (time_in_seconds >= 0)
  119. {
  120. // time formatting code modified from http://userscripts.org/scripts/show/169154
  121. var days = Math.floor((time_in_seconds/(60*60*24)));
  122. var hours = Math.floor((time_in_seconds/(60*60)) % 24);
  123. var minutes = Math.floor((time_in_seconds/60) % 60);
  124. var seconds = time_in_seconds % 60;
  125.  
  126. time_str = (days == 0 ? '' : days + (days > 1 ? ' days ' : ' day '));
  127.  
  128. if (hours < 10) {hours = "0"+hours;}
  129. if (minutes < 10) {minutes = "0"+minutes;}
  130. if (seconds < 10) {seconds = "0"+seconds;}
  131.  
  132. time_str += hours + ':' +minutes + ':' + seconds;
  133. }
  134.  
  135. return time_str;
  136. }