Mturk Expanded Header (Cached)

Gives you an expanded header with transfer balance and Worker ID without polling on every page load. This also works on the latest Firefox (the other scripts will break soon).

当前为 2014-07-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mturk Expanded Header (Cached)
  3. // @namespace DonovanM
  4. // @author DonovanM (dnast)
  5. // @description Gives you an expanded header with transfer balance and Worker ID without polling on every page load. 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
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. /** Users can change the values below to thier 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.  
  31. function Header() {
  32. this.URL = "https://www.mturk.com/mturk/dashboard";
  33. this.CACHE_TIME = UserSettings.cache_time * 60000; // Convert mins to millis
  34. this.isSignedIn = false;
  35. this.init();
  36. }
  37.  
  38. Header.prototype.init = function() {
  39. this.addStyle();
  40. this.addDiv();
  41. this.getData();
  42. }
  43.  
  44. Header.prototype.addDiv = function() {
  45. // Get the place in the document to add the header
  46. var container = $("#user_name_field").parent();
  47.  
  48. if (container.length === 0)
  49. container = $("#lnkWorkerSignin").parent();
  50. else
  51. this.isSignedIn = true;
  52.  
  53. // Create the div
  54. var div = $("<div>").attr('id', "expandedHeader")
  55. .append(
  56. "Transfer Balance: ",
  57. $("<span>").addClass("balance"),
  58. " | Worker ID: ",
  59. $("<input>").addClass("workerId").prop('readonly', true)
  60. .hover(function() { $(this).select(); }, function() { $(this).focus(); $(this).blur(); })
  61. );
  62.  
  63. // Add the div to the page
  64. container.append(div);
  65. }
  66.  
  67. Header.prototype.getData = function() {
  68. // Get cached data from local storage and get the current time
  69. var data = JSON.parse(localStorage.getItem(LOCAL_STORAGE));
  70. var currentTime = new Date().getTime();
  71.  
  72. // If the info is cached but the cache time hasn't been exceeded, load the cached values.
  73. // If user isn't logged in, use cached values no matter how old.
  74. if ((data !== null) && (!this.isSignedIn || (currentTime - data.timestamp < this.CACHE_TIME))) {
  75. this.setValues(data.balance, data.workerId);
  76. } else if (this.isSignedIn) {
  77. // Otherwise load the data from mturk.com (if signed in)
  78. var self = this;
  79.  
  80. this.loadData(function(results) {
  81. self.setValues(results.balance, results.workerId);
  82. localStorage.setItem(LOCAL_STORAGE,
  83. JSON.stringify({
  84. balance: results.balance,
  85. workerId: results.workerId,
  86. timestamp: new Date().getTime()
  87. })
  88. );
  89. });
  90. } else {
  91. console.log("Not logged in and no cached data");
  92. }
  93. }
  94.  
  95. Header.prototype.setValues = function(balance, workerId) {
  96. $("#expandedHeader .balance").text(balance);
  97. $("#expandedHeader .workerId").attr('value', workerId);
  98. }
  99.  
  100. Header.prototype.loadData = function(callback) {
  101. $.get(this.URL, function(data) {
  102. var balance = $("#transfer_earnings .reward", data).text();
  103. var workerId = data.match(/Your Worker ID: ([0-9A-Z]+)/)[1];
  104.  
  105. callback({ workerId: workerId, balance: balance});
  106. })
  107. }
  108.  
  109. Header.prototype.addStyle = function() {
  110. $("head").append("\
  111. <style type=\"text/css\">\
  112. #expandedHeader { margin: 1px }\
  113. #expandedHeader .balance { font-weight: bold; color: " + UserSettings.balanceColor + " }\
  114. #expandedHeader .workerId { font-weight: bold; border: none; width: 115px; color: " + UserSettings.idColor + " }\
  115. </style>\
  116. ");
  117. }