Mturk Expanded Header (Cached)

Gives you an expanded header on Mturk (Mechanical Turk) with transfer balance and Worker ID without polling on every page load. This will reduce maximum request rate errors for people that use expanded header scripts. This also works on the latest Firefox (the other scripts will break soon).

  1. // ==UserScript==
  2. // @name Mturk Expanded Header (Cached)
  3. // @namespace DonovanM
  4. // @author DonovanM (dnast)
  5. // @description Gives you an expanded header on Mturk (Mechanical Turk) with transfer balance and Worker ID without polling on every page load. This will reduce maximum request rate errors for people that use expanded header scripts. This also works on the latest Firefox (the other scripts will break soon).
  6. // @include https://www.mturk.com/mturk/*
  7. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
  8. // @version 0.9.3
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. /** Users can change the values below to their liking. The cache_time is the
  14. number of minutes before the balance is checked again. For the colors
  15. you can use hex i.e. "#ABC123" or RBG(A) i.e. "rgba(100,255,220, .8)".
  16. Just make sure the color values are in quotes (but not the cache time).
  17. **/
  18. var UserSettings = { };
  19. UserSettings.cache_time = 5; // Minutes to wait between updating
  20. UserSettings.idColor = "#c60"; // Color of the WorkerId highlight
  21. UserSettings.balanceColor = "#03B603"; // Color of the balance amount. Use "#000" to go back to black
  22. /** End of user settings **/
  23.  
  24.  
  25. var LOCAL_STORAGE = "expanded_header_data";
  26.  
  27. $(document).ready(function() {
  28. var header = new Header();
  29.  
  30. // Update the values in the header when another page does an update
  31. window.addEventListener('storage', function(e) {
  32. if (e.key === LOCAL_STORAGE)
  33. header.setValues(JSON.parse(e.newValue));
  34. }, false);
  35. });
  36.  
  37. function Header() {
  38. this.DASHBOARD = "https://www.mturk.com/mturk/dashboard";
  39. this.CACHE_TIME = UserSettings.cache_time * 60000; // Convert mins to millis
  40. this.isSignedIn = false;
  41. this.init();
  42. }
  43.  
  44. Header.prototype.init = function() {
  45. this.addStyle();
  46. this.addDiv();
  47. this.getData();
  48. }
  49.  
  50. Header.prototype.addDiv = function() {
  51. // Get the place in the document to add the header
  52. var container = $("#user_name_field").parent();
  53.  
  54. if (container.length === 0)
  55. container = $("#lnkWorkerSignin").parent();
  56. else
  57. this.isSignedIn = true;
  58.  
  59. // Create the div
  60. var div = $("<div>").attr('id', "expandedHeader")
  61. .append(
  62. "Transfer Balance: ",
  63. $("<span>").addClass("balance"),
  64. " | Worker ID: ",
  65. $("<input>").addClass("workerId").prop('readonly', true)
  66. .hover(function() { $(this).select(); }, function() { $(this).focus(); $(this).blur(); })
  67. );
  68.  
  69. // Add the div to the page
  70. container.append(div);
  71. }
  72.  
  73. Header.prototype.getData = function() {
  74. // Get cached data from local storage and get the current time
  75. var data = JSON.parse(localStorage.getItem(LOCAL_STORAGE));
  76. var currentTime = new Date().getTime();
  77.  
  78. if (document.URL === this.DASHBOARD) {
  79. // If we're already on the dashboard then just get the info from this page
  80. var values = this.getValues($("body").html());
  81. this.setValues(values);
  82. this.storeData(values);
  83.  
  84. } else if ((data !== null) && (!this.isSignedIn || (currentTime - data.timestamp < this.CACHE_TIME))) {
  85. // If the info is cached but the cache time hasn't been exceeded, load the cached values.
  86. // Or if user isn't logged in, use cached values no matter how old.
  87. this.setValues(data);
  88.  
  89. } else if (this.isSignedIn) {
  90. // Otherwise load the data from mturk.com (if signed in)
  91. var self = this;
  92.  
  93. this.loadData(function(results) {
  94. self.setValues(results);
  95. self.storeData(results);
  96. });
  97. } else {
  98. console.log("Not logged in and no cached data");
  99. }
  100. }
  101.  
  102. Header.prototype.getValues = function(data) {
  103. var balance = $("#transfer_earnings .reward", data).text();
  104. var workerId = data.match(/Your Worker ID: ([0-9A-Z]+)/)[1];
  105.  
  106. return { balance: balance, workerId: workerId };
  107. }
  108.  
  109. Header.prototype.setValues = function(values) {
  110. $("#expandedHeader .balance").text(values.balance);
  111. $("#expandedHeader .workerId").attr('value', values.workerId);
  112. }
  113.  
  114. Header.prototype.storeData = function(values) {
  115. localStorage.setItem(
  116. LOCAL_STORAGE,
  117. JSON.stringify({
  118. balance: values.balance,
  119. workerId: values.workerId,
  120. timestamp: new Date().getTime()
  121. })
  122. );
  123. }
  124.  
  125. Header.prototype.loadData = function(callback) {
  126. var self = this;
  127.  
  128. $.get(this.DASHBOARD, function(data) {
  129. var values = self.getValues(data);
  130. callback({ workerId: values.workerId, balance: values.balance});
  131. })
  132. }
  133.  
  134. Header.prototype.addStyle = function() {
  135. $("head").append("\
  136. <style type=\"text/css\">\
  137. #expandedHeader { margin: 1px }\
  138. #expandedHeader .balance { font-weight: bold; color: " + UserSettings.balanceColor + " }\
  139. #expandedHeader .workerId { font-weight: bold; border: none; width: 115px; color: " + UserSettings.idColor + " }\
  140. </style>\
  141. ");
  142. }