AlkanFan

Untitled

May 8th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. // ==UserScript==
  2. // @name MTurk - Expanded header info.
  3. // @version 1.4
  4. // @namespace shiny9k-1
  5. // @author Shiny9k
  6. // @description This script adds some extra features under the account header links on MTurk's site. New features include displaying of your current balance along with Worker ID.
  7. // @include https://www.mturk.com/mturk/*
  8. // @downloadURL https://userscripts.org/scripts/source/126924.user.js
  9. // @updateURL http://userscripts.org/scripts/source/126924.meta.js
  10. // @run-at document-end
  11. // ==/UserScript==
  12. /*
  13. Changes:
  14. 1.4 - I did a bit of overhauling the code. The script now has a cacheWorkerID option. If this is set to 1 (default) it will use the WorkerID stored by Greasemonkey rather accessing the Dashboard for it. Overall this should speed the script up a little bit. I could do something similar too with the balance but I would honestly rather not do so as I like my balance to be up to date. But this could change if people keep having the "too many connections" problem. The script also has gotten a few more checks to prevent weird errors (like WorkerID being shown before your balance) from happening. If you have any problems please feel free to post them on the discussion section. Thanks.
  15. 1.3 - Added easy copying. What this does is it makes you not even have to double click the WorkerID to select it. Just put your mouse cursor over it and CTRL+C.
  16. 1.2 - Several changes. Code has been restructured drastically. Script should automatically update itself now when a new version is released. Overall speed of data collection should be improved and several safeguards implemented to try to ensure data is collected.
  17. 1.1 - Minor changes. Made it so balance values up to 1 million can be fetched. I doubt anyone has anything near that so the current regex should be good for quite some time.
  18. 1.0 - Initial Release
  19. */
  20.  
  21. // Setting this to 1 (default) makes the script only check for a WorkerID once.
  22. // If you are using MTurk from an Internet Cafe or shared computer where others access MTurk from you may want to set this to 0.
  23. var cacheWorkerID = '1';
  24.  
  25. // Set this higher if your WorkerID isn't displaying. NOTE: Amazon does check for page flooding so I would not recommend setting this value too high.
  26. var retryAttempts = '10';
  27.  
  28. // Like the above variable, set this to higher if your WorkerID isn't displaying. Lowering this value is not recommended.
  29. var retryTime = '250';
  30.  
  31. function fetchData(urlNum) {
  32. var timer;
  33.  
  34. var pageURL = new Array("https://www.mturk.com/mturk/youraccount", "https://www.mturk.com/mturk/dashboard");
  35.  
  36.  
  37.  
  38. if (typeof numCon === 'undefined') {
  39. var numCon = 0;
  40. } else if (numCon > retryAttempts) {
  41. throw new Error("Could not get data.");
  42. }
  43.  
  44.  
  45. GM_xmlhttpRequest({
  46. method: "GET",
  47. url: pageURL[urlNum],
  48. onload: function (response) {
  49. // Get balance
  50. if (urlNum == '0') {
  51. var reBal = /(\$\d+,?.?\d+,?.?\d+,?.?\d?\d?)/;
  52. var balance = response.responseText.match(reBal);
  53. if (balance['0']) {
  54. GM_setValue('AMZ_Balance', balance['0']);
  55. addToPage(balance['0'], 0, numCon);
  56. }
  57.  
  58. // Get Worker
  59. } else if (urlNum == '1' && document.getElementById('eh_balance') != null) {
  60. var reWID = /ID:\s(\w+)/;
  61. var workerID = response.responseText.match(reWID);
  62. if (workerID['1']) {
  63. GM_setValue('AMZ_WorkerID', workerID['1']);
  64. addToPage(workerID['1'], 1, numCon);
  65. }
  66.  
  67. }
  68. }
  69. });
  70. numCon++;
  71. }
  72.  
  73. function addToPage(item, type, numCon) {
  74. // This adds the balance
  75. if (type == '0') {
  76. newSpan = document.createElement('span');
  77. newSpan.id = 'eh_balance';
  78. newSpan.style.cssText = "font-weight: bold;";
  79. newDiv.innerHTML += 'Current Balance: ';
  80. newSpan.innerHTML = item;
  81. newDiv.appendChild(newSpan);
  82. newDiv.innerHTML += ' | ';
  83.  
  84. if (cacheWorkerID == '1' && typeof GM_getValue('AMZ_WorkerID') != 'undefined' && document.getElementById('eh_balance') != null) {
  85. addToPage(GM_getValue('AMZ_WorkerID'), 1);
  86. } else if (document.getElementById('eh_balance') != null && numCon < retryAttempts) {
  87. // I don't want the script flooding MTurk with too many page view attempts. Going to user the defined delays to limit it
  88. var timer = setTimeout(function () {
  89. fetchData(1, " ");
  90. }, retryTime);
  91. numCon++;
  92. }
  93.  
  94. // This is for adding the Worker ID
  95. } else if (type == '1') {
  96. newDiv.innerHTML += "Worker ID: <input type='text' onmouseover='javascript:this.focus();this.select();' onmouseout='javascript:this.blur();' value='" + item + "' style='color:#c60; font-weight: bold; border: none; width: 115px;' readonly/>";
  97.  
  98. // Not logged in. Uses previously stored values (If available) to output the header info. with
  99. } else if (type == '2') {
  100. var balance = GM_getValue('AMZ_Balance');
  101. var workerID = GM_getValue('AMZ_WorkerID');
  102. if (balance && workerID) {
  103. topMenu.appendChild(newDiv);
  104. spanGen(balance, '0');
  105. spanGen(workerID, '1');
  106. }
  107. }
  108. }
  109.  
  110. // Let's start things by creating the new div. This will contain the new header items.
  111. var newDiv = document.createElement('div');
  112. newDiv.style.paddingTop = "1%";
  113. newDiv.id = 'expHdr';
  114.  
  115. // Quick check to see if you're logged in. If not, we'll try writing in some previously stored values.
  116. if (document.getElementById('user_name_field')) {
  117. topMenu = document.getElementById('user_name_field').parentNode;
  118. } else {
  119. topMenu = document.getElementById('lnkWorkerSignin').parentNode;
  120. }
  121. topMenu.appendChild(newDiv);
  122.  
  123. // Let's add the new header.
  124. if (document.getElementById('lnkWorkerSignin')) {
  125. addToPage(0, 2);
  126. // Get Cash balance and then Worker ID
  127. } else {
  128. fetchData(0);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment