MTurk HIT DataBase

Extended ability to search HITs you have worked on and other useful tools (CSV export/import, requester notes, requester block, pending/projected earnings)

当前为 2015-06-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MTurk HIT DataBase
  3. // @namespace https://greasyfork.org/users/710
  4. // @description Extended ability to search HITs you have worked on and other useful tools (CSV export/import, requester notes, requester block, pending/projected earnings)
  5. // @include https://www.mturk.com/mturk/searchbar*
  6. // @include https://www.mturk.com/mturk/findhits*
  7. // @include https://www.mturk.com/mturk/viewhits*
  8. // @include https://www.mturk.com/mturk/viewsearchbar*
  9. // @include https://www.mturk.com/mturk/sortsearchbar*
  10. // @include https://www.mturk.com/mturk/sorthits*
  11. // @include https://www.mturk.com/mturk/dashboard
  12. // @include https://www.mturk.com/mturk/preview?*
  13. // @version 1.9.4
  14. // @grant none
  15. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
  16. // @require http://code.highcharts.com/highcharts.js
  17. // @require https://greasyfork.org/scripts/2351-jsdiff/code/jsdiff.js?version=6256
  18. // @require https://greasyfork.org/scripts/2350-filesaver-js/code/filesaverjs.js?version=6255
  19. // ==/UserScript==
  20.  
  21. //
  22. // 2012-10-03 0.9.7: This is rewrite of MTurk Extended HIT Search (http://userscripts.org/scripts/show/146277)
  23. // with some extra features (and some missing for now: search by date).
  24. // It now uses IndexedDB (http://en.wikipedia.org/wiki/Indexed_Database_API)
  25. //
  26. // 2012-10-04 0.9.8: Improved use of indexes, check Pending Payment HITs
  27. // 0.9.9: Minor improvements
  28. //
  29. // 2012-10-04 0.10: Added date options
  30. //
  31. // 2012-10-07 0.11: Requester notes, bug fixes
  32. // 0.12: CSV export
  33. //
  34. // 2012-10-09 0.13: "Block" requesters or specific HITs
  35. //
  36. // 2012-10-10 0.14: Requester Overview, shows summary of all requesters in DB
  37. //
  38. // 2012-10-11 0.15: Blocked HITs are always on bottom of the page
  39. //
  40. // 2012-10-14 0.16: Requester Overview improvements
  41. //
  42. // 2012-10-17 0.17: Bug fixes and error checks
  43. //
  44. // 2012-10-18 0.18: Import HIT data from MTurk Extended HIT Search script
  45. //
  46. // 2012-10-21 0.19: Moved main interface to dashboard, show pending earnings on dashboard,
  47. // summary of all requesters with pending HITs.
  48. //
  49. // 2012-10-23 0.20: Added Turkopticon (http://turkopticon.differenceengines.com/) links to overview pages
  50. // 0.21: Fixed overview pages reward to include only 'Paid' and 'Approved - Pending Payment' HITs.
  51. //
  52. // 2012-10-28 0.22: Limited Auto Update.
  53. // 0.23: Minor improvements
  54. //
  55. // 2012-10-30 0.24: Projected earnings for today
  56. //
  57. // 2012-11-02 0.25: Smarter Auto Update
  58. //
  59. // 2012-11-03 0.26: GUI update
  60. //
  61. // 2012-11-05 0.30: Extra non-amazonian script monkeys
  62. //
  63. // 2012-11-06 0.31: Projected earnings progress bar
  64. //
  65. // 2012-11-08 0.32: Minor GUI fixes to look better on Chrome. Looks like it now works on stable Chrome!
  66. //
  67. // 2012-11-13 0.33: Time limits now work with Requester Overview
  68. //
  69. // 2012-11-15 0.34: Bug/compatibility fixes
  70. //
  71. // 2012-11-18 0.40: Daily Overview, update database to use YYYY-MM-DD date format.
  72. //
  73. // 2012-11-22 0.41: R and T button on HIT preview page. Auto-Approval time.
  74. //
  75. // 2012-11-30 0.42: Changes on MTurk pages. Status page in now on one page!
  76. //
  77. // 2012-12-02 1.0: Added @downloadURL and @updateURL
  78. //
  79. // 2012-12-06 1.1: Requester details.
  80. // Try to fetch few extra days at first update (not showing on status page).
  81. //
  82. // 2012-12-11 1.2: Import HITs from previously exported CSV-files.
  83. // Removed Extended HIT Search import.
  84. //
  85. // 2012-12-13 1.3: Fix CSV-import to put empty string instead if undefined if feedback is empty.
  86. //
  87. // 2012-12-14 1.4: Rewritten database update more properly.
  88. //
  89. // 2012-12-16 1.5: Fixed broken Auto Update (forgot to check that on pervious update).
  90. //
  91. // 2013-02-26 1.6: Fixed IDBTransactionModes for Chrome (note this breaks it for Firefox)
  92. //
  93. // 2013-02-27 1.7: Changed UI bars back to what they used to be.
  94. //
  95.  
  96. var DAYS_TO_FETCH = [];
  97. var DAYS_TO_FETCH_CHECK;
  98.  
  99. var HITStorage = {};
  100. var indexedDB = window.indexedDB || window.webkitIndexedDB ||
  101. window.mozIndexedDB;
  102. window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction;
  103. window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange;
  104. HITStorage.IDBTransactionModes = { "READ_ONLY": "readonly", "READ_WRITE": "readwrite", "VERSION_CHANGE": "versionchange" };
  105. var IDBKeyRange = window.IDBKeyRange;
  106.  
  107. HITStorage.indexedDB = {};
  108. HITStorage.indexedDB = {};
  109. HITStorage.indexedDB.db = null;
  110.  
  111. HITStorage.indexedDB.onerror = function(e) {
  112. console.log(e);
  113. };
  114. var v = 4;
  115.  
  116. if (!localStorage["20150602fix"])
  117. {
  118. alert("ATTENTION!!!! Your hit database may not be functioning correctly. You will need to convert it, due to issues introduced by some code changes by mturk staff. This will involve a few steps. Please visit http://tinyurl.com/q58mxr6 and follow the instructions to convert. This message will disappear once the hitDB has been properly converted.");
  119. if (!localStorage["20150602alert"])
  120. localStorage["20150602alert"] = 1;
  121. else
  122. localStorage["20150602alert"] += 1;
  123. if (localStorage["20150602alert"] == 3)
  124. {
  125. alert("Alert has displayed 3 times, alert will not display anymore. Please convert your database if you have not done so already.");
  126. localStorage["20150602alert"] = 1;
  127. }
  128. }
  129.  
  130. HITStorage.indexedDB.create = function() {
  131.  
  132. var request = indexedDB.open("HITDB", v);
  133.  
  134. request.onupgradeneeded = function (e) {
  135. HITStorage.indexedDB.db = e.target.result;
  136. var db = HITStorage.indexedDB.db;
  137. var new_empty_db = false;
  138.  
  139. if(!db.objectStoreNames.contains("HIT")) {
  140. var store = db.createObjectStore("HIT", { keyPath: "hitId" });
  141.  
  142. store.createIndex("date", "date", { unique: false });
  143. store.createIndex("requesterName", "requesterName", { unique: false });
  144. store.createIndex("title", "title", { unique: false });
  145. store.createIndex("reward", "reward", { unique: false });
  146. store.createIndex("status", "status", { unique: false });
  147. store.createIndex("requesterId", "requesterId", { unique: false });
  148.  
  149. new_empty_db = true;
  150.  
  151. // At first update try to get few extra days that do not show on status page
  152. localStorage['HITDB TRY_EXTRA_DAYS'] = 'YES';
  153. }
  154. if(!db.objectStoreNames.contains("STATS")) {
  155. var store = db.createObjectStore("STATS", { keyPath: "date" });
  156. }
  157. if(!db.objectStoreNames.contains("NOTES")) {
  158. var store = db.createObjectStore("NOTES", { keyPath: "requesterId" });
  159. }
  160. if(!db.objectStoreNames.contains("BLOCKS")) {
  161. var store = db.createObjectStore("BLOCKS", { keyPath: "id", autoIncrement: true });
  162.  
  163. store.createIndex("requesterId", "requesterId", { unique: false });
  164. }
  165.  
  166. if (new_empty_db == false)
  167. {
  168. alert("HIT DataBase date format must be upgraded (MMDDYYYY => YYYY-MM-DD)\n" +
  169. "Please don't close or reload this page until it's done.\n" +
  170. "Press OK to start. This shouldn't take long. (few minutes max)" +
  171. "Sorry for the inconvenience.");
  172. HITStorage.update_date_format(true);
  173. }
  174. db.close();
  175. //alert("DataBase upgraded to version " + v + '!');
  176. }
  177.  
  178. request.onsuccess = function(e) {
  179. HITStorage.indexedDB.db = e.target.result;
  180. var db = HITStorage.indexedDB.db;
  181. db.close();
  182. };
  183.  
  184. request.onerror = HITStorage.indexedDB.onerror;
  185. }
  186.  
  187. HITStorage.indexedDB.addHIT = function(hitData) {
  188. // Temporary extra check
  189. if (hitData.date.indexOf('-') < 0)
  190. {
  191. alert('Wrong date format in addHIT()!');
  192. return;
  193. }
  194.  
  195. var request = indexedDB.open("HITDB", v);
  196. request.onsuccess = function(e) {
  197. HITStorage.indexedDB.db = e.target.result;
  198. var db = HITStorage.indexedDB.db;
  199. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
  200. var store = trans.objectStore("HIT");
  201.  
  202. var request = store.put(hitData);
  203.  
  204. request.onsuccess = function(e) {
  205. db.close();
  206. };
  207.  
  208. request.onerror = function(e) {
  209. console.log("Error Adding: ", e);
  210. };
  211. };
  212. request.onerror = HITStorage.indexedDB.onerror;
  213. };
  214.  
  215. HITStorage.indexedDB.importHITs = function(hitData) {
  216. var hits = hitData.length;
  217. var label = document.getElementById('status_label');
  218.  
  219. var request = indexedDB.open("HITDB", v);
  220. request.onsuccess = function(e) {
  221. HITStorage.indexedDB.db = e.target.result;
  222. var db = HITStorage.indexedDB.db;
  223. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
  224. var store = trans.objectStore("HIT");
  225.  
  226. putNextHIT();
  227.  
  228. function putNextHIT()
  229. {
  230. if (hitData.length > 0)
  231. {
  232. store.put(hitData.pop()).onsuccess = putNextHIT;
  233. label.innerHTML = progress_bar(((hits-hitData.length)/hits*50), 50, '█', '█', '#7fb448', 'grey') + ' (' + hitData.length + ')';
  234. }
  235. else
  236. {
  237. HITStorage.enable_inputs();
  238. HITStorage.update_status_label('Import done', 'green');
  239. db.close();
  240. }
  241. }
  242. };
  243. request.onerror = HITStorage.indexedDB.onerror;
  244. };
  245.  
  246. HITStorage.indexedDB.addHITs = function(hitData, day_to_fetch, days_to_update) {
  247. var hits = hitData.length;
  248. if (day_to_fetch)
  249. var label = document.getElementById('status_label');
  250.  
  251. var request = indexedDB.open("HITDB", v);
  252. request.onsuccess = function(e) {
  253. HITStorage.indexedDB.db = e.target.result;
  254. var db = HITStorage.indexedDB.db;
  255. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
  256. var store = trans.objectStore("HIT");
  257.  
  258. putNextHIT();
  259.  
  260. function putNextHIT()
  261. {
  262. if (hitData.length > 0)
  263. {
  264. store.put(hitData.pop()).onsuccess = putNextHIT;
  265. if (day_to_fetch)
  266. label.innerHTML = 'Saving ' + day_to_fetch.date + ': ' + progress_bar(((hits-hitData.length)/hits*40), 40, '█', '█', '#7fb448', 'grey');
  267. }
  268. else
  269. {
  270. // move to next day
  271. if (day_to_fetch)
  272. {
  273. HITStorage.indexedDB.updateHITstats(day_to_fetch);
  274. setTimeout(function() { HITStorage.do_update(days_to_update); }, 2000);
  275. HITStorage.update_status_label('Please wait: script monkeys are taking naps ?', 'red');
  276. }
  277. db.close();
  278. }
  279. }
  280. };
  281. request.onerror = HITStorage.indexedDB.onerror;
  282. };
  283.  
  284.  
  285. HITStorage.indexedDB.updateHITstats = function(date)
  286. {
  287. // Temporary extra check
  288. if (date.date.indexOf('-') < 0)
  289. {
  290. alert('Wrong date format in updateHITstats()!');
  291. return;
  292. }
  293.  
  294. var request = indexedDB.open("HITDB", v);
  295. request.onsuccess = function(e) {
  296. HITStorage.indexedDB.db = e.target.result;
  297. var db = HITStorage.indexedDB.db;
  298. var trans = db.transaction(["STATS"], HITStorage.IDBTransactionModes.READ_WRITE);
  299. var store = trans.objectStore("STATS");
  300.  
  301. var request = store.put(date);
  302.  
  303. request.onsuccess = function(e) {
  304. db.close();
  305. };
  306.  
  307. request.onerror = function(e) {
  308. console.log("Error Adding: ", e);
  309. };
  310. };
  311. request.onerror = HITStorage.indexedDB.onerror;
  312. };
  313.  
  314. HITStorage.prepare_update_and_check_pending_payments = function()
  315. {
  316. var request = indexedDB.open("HITDB", v);
  317. request.onsuccess = function(e) {
  318. HITStorage.indexedDB.db = e.target.result;
  319. var db = HITStorage.indexedDB.db;
  320. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  321. var store = trans.objectStore("HIT");
  322. var index = store.index('status');
  323. var range = IDBKeyRange.only('Approved&nbsp;- Pending&nbsp;Payment');
  324.  
  325. index.openCursor(range).onsuccess = function(event) {
  326. var cursor = event.target.result;
  327. if (cursor && DAYS_TO_FETCH.length > 0)
  328. {
  329. for (var i=0; i<DAYS_TO_FETCH.length; i++)
  330. {
  331. if ( cursor.value.date == DAYS_TO_FETCH[i].date && cursor.value.reward>0 )
  332. {
  333. DAYS_TO_FETCH[i].pending_payments = true;
  334. }
  335. }
  336. cursor.continue();
  337. }
  338. else
  339. {
  340. if (DAYS_TO_FETCH.length>0) {
  341. db.close();
  342. HITStorage.update_status_label('Please wait: script monkeys are planning to fetch relevant status pages', 'red');
  343. setTimeout(function() { HITStorage.prepare_update(); }, 100);
  344. }
  345. else
  346. {
  347. db.close();
  348. HITStorage.update_done();
  349. }
  350. }
  351. };
  352. }
  353. };
  354.  
  355. // check that number of hits in DB matches what is available
  356. HITStorage.check_update = function()
  357. {
  358. var request = indexedDB.open("HITDB", v);
  359. request.onsuccess = function(e) {
  360. HITStorage.update_status_label('Please wait: checking database', 'red');
  361. HITStorage.indexedDB.db = e.target.result;
  362. var db = HITStorage.indexedDB.db;
  363. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  364. var store = trans.objectStore("HIT");
  365. var index = store.index('date');
  366. var range = IDBKeyRange.bound(DAYS_TO_FETCH_CHECK[DAYS_TO_FETCH_CHECK.length-1].date, DAYS_TO_FETCH_CHECK[0].date, false, false);
  367.  
  368. index.count(range).onsuccess = function(event) {
  369. var count = event.target.result;
  370. var submitted_hits = 0;
  371.  
  372. for (var i=0; i<DAYS_TO_FETCH_CHECK.length; i++)
  373. {
  374. submitted_hits += DAYS_TO_FETCH_CHECK[i].submitted;
  375. }
  376.  
  377. if (submitted_hits == count)
  378. {
  379. db.close();
  380. HITStorage.update_done();
  381. }
  382. else
  383. {
  384. if (confirm("? ERROR! Number of HITs in DataBase does not match number of HITs available! (" + count + " != " + submitted_hits + ")\n"
  385. + "Would you like to refetch all status pages now?"))
  386. {
  387. db.close();
  388. DAYS_TO_FETCH = DAYS_TO_FETCH_CHECK.slice(0);
  389. HITStorage.update_status_label('Please wait: new script monkeys are fetching relevant status pages', 'red');
  390. setTimeout(function() { HITStorage.do_update(DAYS_TO_FETCH.length); }, 100);
  391. }
  392. else
  393. {
  394. db.close();
  395. HITStorage.update_done();
  396. }
  397. }
  398. };
  399. }
  400. };
  401.  
  402. HITStorage.prepare_update = function()
  403. {
  404. var request = indexedDB.open("HITDB", v);
  405. request.onsuccess = function(e) {
  406. HITStorage.indexedDB.db = e.target.result;
  407. var db = HITStorage.indexedDB.db;
  408. var trans = db.transaction(["STATS"], HITStorage.IDBTransactionModes.READ_ONLY);
  409. var store = trans.objectStore("STATS");
  410. var range = IDBKeyRange.bound(DAYS_TO_FETCH[DAYS_TO_FETCH.length-1].date, DAYS_TO_FETCH[0].date, false, false);
  411.  
  412. store.openCursor(range).onsuccess = function(event) {
  413. var cursor = event.target.result;
  414. if (cursor && DAYS_TO_FETCH.length > 0)
  415. {
  416. for (var i=0; i<DAYS_TO_FETCH.length; i++)
  417. {
  418. if ( cursor.value.date == DAYS_TO_FETCH[i].date
  419. && cursor.value.submitted == DAYS_TO_FETCH[i].submitted
  420. && cursor.value.approved == DAYS_TO_FETCH[i].approved
  421. && cursor.value.rejected == DAYS_TO_FETCH[i].rejected
  422. && cursor.value.pending == DAYS_TO_FETCH[i].pending)
  423. {
  424. // This day is already in DB and stats match => no need to fetch
  425. // unless there are 'Approved - Pending Payment' HITs
  426. if (DAYS_TO_FETCH[i].pending_payments === undefined || DAYS_TO_FETCH[i].pending_payments == false)
  427. DAYS_TO_FETCH.splice(i,1);
  428. }
  429. }
  430. cursor.continue();
  431. }
  432. else
  433. {
  434. if (DAYS_TO_FETCH.length>0) {
  435. db.close();
  436. setTimeout(function() { HITStorage.do_update(DAYS_TO_FETCH.length); }, 100);
  437. }
  438. else
  439. {
  440. db.close();
  441. HITStorage.update_done();
  442. }
  443. }
  444. };
  445. }
  446. };
  447.  
  448. HITStorage.indexedDB.term_matches_HIT = function(term, hit)
  449. {
  450. var keys = ['date', 'requesterName', 'title', 'feedback', 'hitId', 'requesterId'];
  451. var re = new RegExp(escapeRegExp(term),"ig");
  452. for (var k in keys)
  453. {
  454. //for testing
  455. if (hit[keys[k]] != null && re.test(hit[keys[k]].trim()))
  456. {
  457. return true;
  458. }
  459. }
  460. return false;
  461. }
  462.  
  463. function escapeRegExp(str) {
  464. return str.trim().replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  465. }
  466.  
  467. HITStorage.indexedDB.matchHIT = function(hit, options)
  468. {
  469. if (options.status == '---' || hit.status.match(options.status))
  470. {
  471. if (options.search_term == '' || HITStorage.indexedDB.term_matches_HIT(options.term, hit))
  472. {
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478.  
  479. function hit_sort_func()
  480. {
  481. return function(a,b) {
  482. if (a.date == b.date) {
  483. if (a.requesterName < b.requesterName)
  484. return -1;
  485. if (a.requesterName > b.requesterName)
  486. return 1;
  487. if (a.title < b.title)
  488. return -1;
  489. if (a.title > b.title)
  490. return 1;
  491. if (a.status < b.status)
  492. return -1;
  493. if (a.status > b.status)
  494. return 1;
  495. }
  496. if (a.date > b.date)
  497. return 1;
  498. if (a.date < b.date)
  499. return -1;
  500. };
  501. }
  502.  
  503. HITStorage.indexedDB.getHITs = function(options) {
  504. var request = indexedDB.open("HITDB", v);
  505. request.onsuccess = function(e) {
  506. HITStorage.indexedDB.db = e.target.result;
  507. var db = HITStorage.indexedDB.db;
  508. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  509. var store = trans.objectStore("HIT");
  510.  
  511. var req;
  512. var results = [];
  513. var index;
  514. var range;
  515.  
  516. if (options.from_date || options.to_date)
  517. {
  518. if (options.from_date != '' || options.to_date != '')
  519. {
  520. index = store.index('date');
  521. if (options.from_date == options.to_date)
  522. {
  523. range = IDBKeyRange.only(options.from_date);
  524. }
  525. else if (options.from_date != '' && options.to_date != '')
  526. {
  527. range = IDBKeyRange.bound(options.from_date, options.to_date, false, false);
  528. }
  529. else if (options.from_date == '' && options.to_date != '')
  530. {
  531. range = IDBKeyRange.upperBound(options.to_date, false);
  532. }
  533. else
  534. {
  535. range = IDBKeyRange.lowerBound(options.from_date, false);
  536. }
  537. req = index.openCursor(range);
  538. }
  539. }
  540. else if (options.index && options.index != '')
  541. {
  542. index = store.index(options.index);
  543. range = IDBKeyRange.only(options.term);
  544. req = index.openCursor(range);
  545. }
  546. else if (options.status == 'Rejected' || options.status == 'Pending Approval'
  547. || options.status == 'Approved' || options.status == 'Paid')
  548. {
  549. var s = (options.status == 'Approved')? 'Approved&nbsp;- Pending&nbsp;Payment' : options.status;
  550. options.index = 'status';
  551. index = store.index(options.index);
  552. range = IDBKeyRange.only(s);
  553. req = index.openCursor(range);
  554. }
  555. else
  556. {
  557. req = store.openCursor();
  558. }
  559.  
  560. req.onsuccess = function(event) {
  561. var cursor = event.target.result;
  562. if (cursor) {
  563. if (HITStorage.indexedDB.matchHIT(cursor.value, options))
  564. results.push(cursor.value);
  565.  
  566. cursor.continue();
  567. }
  568. else {
  569. results.sort(hit_sort_func());
  570.  
  571. if (options.export_csv && options.export_csv == true)
  572. HITStorage.export_csv(results);
  573. else
  574. HITStorage.show_results(results);
  575.  
  576. if (options.donut == '---')
  577. document.getElementById('container').style.display = 'none';
  578. else if (options.donut != '')
  579. HITStorage.prepare_donut(results, options.donut);
  580. }
  581. db.close();
  582. };
  583. };
  584. request.onerror = HITStorage.indexedDB.onerror;
  585. };
  586.  
  587. //
  588. // Show summary of all requesters
  589. //
  590. HITStorage.indexedDB.requesterOverview = function(options) {
  591. var request = indexedDB.open("HITDB", v);
  592. request.onsuccess = function(e) {
  593. HITStorage.indexedDB.db = e.target.result;
  594. var db = HITStorage.indexedDB.db;
  595. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  596. var store = trans.objectStore("HIT");
  597. var index;
  598. var req;
  599.  
  600. // [ requesterId, requesterName, sum(hits), sum(rewards), rejected, pending ]
  601. var results = [];
  602. var tmp_results = {};
  603. if (options.from_date || options.to_date)
  604. {
  605. if (options.from_date != '' || options.to_date != '')
  606. {
  607. index = store.index('date');
  608. if (options.from_date == options.to_date)
  609. {
  610. range = IDBKeyRange.only(options.from_date);
  611. }
  612. else if (options.from_date != '' && options.to_date != '')
  613. {
  614. range = IDBKeyRange.bound(options.from_date, options.to_date, false, false);
  615. }
  616. else if (options.from_date == '' && options.to_date != '')
  617. {
  618. range = IDBKeyRange.upperBound(options.to_date, false);
  619. }
  620. else
  621. {
  622. range = IDBKeyRange.lowerBound(options.from_date, false);
  623. }
  624. req = index.openCursor(range);
  625. }
  626. req.onsuccess = function(event) {
  627. var cursor = event.target.result;
  628. if (cursor) {
  629. var hit = cursor.value;
  630. var rejected = (hit.status == 'Rejected') ? 1 : 0;
  631. var pending = (hit.status.match(/Approved|Paid|Rejected/) == null) ? 1 : 0;
  632. var reward = (pending>0 || rejected>0 )? 0: hit.reward;
  633.  
  634. if (tmp_results[hit.requesterId] === undefined)
  635. {
  636. tmp_results[hit.requesterId] = [];
  637. tmp_results[hit.requesterId][0] = hit.requesterId;
  638. tmp_results[hit.requesterId][1] = hit.requesterName;
  639. tmp_results[hit.requesterId][2] = 1;
  640. tmp_results[hit.requesterId][3] = reward;
  641. tmp_results[hit.requesterId][4] = rejected;
  642. tmp_results[hit.requesterId][5] = pending;
  643. }
  644. else
  645. {
  646. tmp_results[hit.requesterId][1] = hit.requesterName;
  647. tmp_results[hit.requesterId][2] += 1;
  648. tmp_results[hit.requesterId][3] += reward;
  649. tmp_results[hit.requesterId][4] += rejected;
  650. tmp_results[hit.requesterId][5] += pending;
  651. }
  652. cursor.continue();
  653. }
  654. else {
  655. for (var key in tmp_results) {
  656. results.push(tmp_results[key]);
  657. }
  658. // sort by total reward
  659. results.sort(function(a,b) { return b[3]-a[3]; });
  660. if (options.export_csv == true)
  661. HITStorage.show_requester_overview_csv(results);
  662. else
  663. HITStorage.show_requester_overview(results, '(' + options.from_date + '–' + options.to_date + ')');
  664. HITStorage.update_status_label('Script monkeys are ready', 'green');
  665. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  666. HITStorage.enable_inputs();
  667. }
  668. db.close();
  669. };
  670. }
  671. else {
  672. index = store.index('requesterId');
  673. req = index.openCursor();
  674. req.onsuccess = function(event) {
  675. var cursor = event.target.result;
  676. if (cursor) {
  677. var hit = cursor.value;
  678. var rejected = (hit.status == 'Rejected') ? 1 : 0;
  679. var pending = (hit.status.match(/Approved|Paid|Rejected/) == null) ? 1 : 0;
  680. var reward = (pending>0 || rejected>0 )? 0: hit.reward;
  681. if (results.length == 0)
  682. {
  683. results.push([hit.requesterId, hit.requesterName, 1, reward, rejected, pending]);
  684. }
  685. else if (results[0][0] == hit.requesterId)
  686. {
  687. results[0][2] += 1;
  688. results[0][3] += reward;
  689. results[0][4] += rejected;
  690. results[0][5] += pending;
  691. }
  692. else
  693. {
  694. results.unshift([hit.requesterId, hit.requesterName, 1, reward, rejected, pending]);
  695. }
  696. cursor.continue();
  697. }
  698. else {
  699. // sort by total reward
  700. results.sort(function(a,b) { return b[3]-a[3]; });
  701. if (options.export_csv == true)
  702. HITStorage.show_requester_overview_csv(results);
  703. else
  704. HITStorage.show_requester_overview(results);
  705. HITStorage.update_status_label('Script monkeys are ready', 'green');
  706. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  707. HITStorage.enable_inputs();
  708. }
  709. db.close();
  710. };
  711. }
  712. };
  713. request.onerror = HITStorage.indexedDB.onerror;
  714. };
  715.  
  716. //
  717. // Show summary of one requester
  718. //
  719. HITStorage.indexedDB.showRequester = function(requesterId) {
  720. var request = indexedDB.open("HITDB", v);
  721. request.onsuccess = function(e) {
  722. HITStorage.indexedDB.db = e.target.result;
  723. var db = HITStorage.indexedDB.db;
  724. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  725. var store = trans.objectStore("HIT");
  726. var index;
  727. var results = [];
  728.  
  729. index = store.index('requesterId');
  730. var range = IDBKeyRange.only(requesterId);
  731. index.openCursor(range).onsuccess = function(event) {
  732. var cursor = event.target.result;
  733. if (cursor) {
  734. results.push(cursor.value);
  735. cursor.continue();
  736. }
  737. else {
  738. results.sort(function(a,b)
  739. {
  740. if (a.date > b.date)
  741. return -1;
  742. if (a.date < b.date)
  743. return 1;
  744. return 0;
  745. });
  746. HITStorage.show_requester(results);
  747. HITStorage.update_status_label('Script monkeys are ready', 'green');
  748. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  749. HITStorage.enable_inputs();
  750. }
  751. db.close();
  752. };
  753. };
  754. request.onerror = HITStorage.indexedDB.onerror;
  755. };
  756.  
  757.  
  758. // Show summary of pending HITs
  759. HITStorage.indexedDB.pendingOverview = function(options) {
  760. var request = indexedDB.open("HITDB", v);
  761. request.onsuccess = function(e) {
  762. HITStorage.indexedDB.db = e.target.result;
  763. var db = HITStorage.indexedDB.db;
  764. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  765. var store = trans.objectStore("HIT");
  766. var index;
  767. var req;
  768.  
  769. // [ requesterId, requesterName, sum(pendings), sum(rewards) ]
  770. var results = [];
  771. var tmp_results = {};
  772.  
  773. index = store.index('status');
  774. range = IDBKeyRange.only('Pending Approval');
  775. index.openCursor(range).onsuccess = function(event) {
  776. var cursor = event.target.result;
  777. if (cursor) {
  778. var hit = cursor.value;
  779. console.log(hit);
  780. if (tmp_results[hit.requesterId] === undefined)
  781. {
  782. tmp_results[hit.requesterId] = [];
  783. tmp_results[hit.requesterId][0] = hit.requesterId;
  784. tmp_results[hit.requesterId][1] = hit.requesterName;
  785. tmp_results[hit.requesterId][2] = 1;
  786. tmp_results[hit.requesterId][3] = hit.reward;
  787. }
  788. else
  789. {
  790. tmp_results[hit.requesterId][1] = hit.requesterName;
  791. tmp_results[hit.requesterId][2] += 1;
  792. tmp_results[hit.requesterId][3] += hit.reward;
  793. }
  794. cursor.continue();
  795. }
  796. else {
  797. for (var key in tmp_results) {
  798. results.push(tmp_results[key]);
  799. }
  800. // sort by pending hits
  801. results.sort(function(a,b) { return b[2]-a[2]; });
  802. if (options.export_csv == true)
  803. HITStorage.show_pending_overview_csv(results);
  804. else
  805. HITStorage.show_pending_overview(results);
  806. HITStorage.update_status_label('Script monkeys are ready', 'green');
  807. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  808. HITStorage.enable_inputs();
  809. }
  810. db.close();
  811. };
  812. };
  813. request.onerror = HITStorage.indexedDB.onerror;
  814. };
  815.  
  816. // Show summary of daily stats
  817. HITStorage.indexedDB.statusOverview = function(options) {
  818. var request = indexedDB.open("HITDB", v);
  819. request.onsuccess = function(e) {
  820. HITStorage.indexedDB.db = e.target.result;
  821. var db = HITStorage.indexedDB.db;
  822. var trans = db.transaction(["STATS"], HITStorage.IDBTransactionModes.READ_ONLY);
  823. var store = trans.objectStore("STATS");
  824. var req;
  825.  
  826. var results = [];
  827.  
  828. if (options.from_date || options.to_date)
  829. {
  830. if (options.from_date != '' || options.to_date != '')
  831. {
  832. if (options.from_date == options.to_date)
  833. {
  834. range = IDBKeyRange.only(options.from_date);
  835. }
  836. else if (options.from_date != '' && options.to_date != '')
  837. {
  838. range = IDBKeyRange.bound(options.from_date, options.to_date, false, false);
  839. }
  840. else if (options.from_date == '' && options.to_date != '')
  841. {
  842. range = IDBKeyRange.upperBound(options.to_date, false);
  843. }
  844. else
  845. {
  846. range = IDBKeyRange.lowerBound(options.from_date, false);
  847. }
  848. req = store.openCursor(range);
  849. }
  850. }
  851. else
  852. {
  853. req = store.openCursor();
  854. }
  855. req.onsuccess = function(event) {
  856. var cursor = event.target.result;
  857. if (cursor) {
  858. if (cursor.value.submitted > 0)
  859. results.push(cursor.value);
  860. cursor.continue();
  861. }
  862. else {
  863. if (options.export_csv == true)
  864. HITStorage.show_status_overview_csv(results);
  865. else
  866. HITStorage.show_status_overview(results, '(' + options.from_date + '–' + options.to_date + ')');
  867. HITStorage.update_status_label('Script monkeys are ready', 'green');
  868. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  869. HITStorage.enable_inputs();
  870. }
  871. db.close();
  872. };
  873. };
  874. request.onerror = HITStorage.indexedDB.onerror;
  875. };
  876.  
  877. HITStorage.indexedDB.getHIT = function(id) {
  878. var request = indexedDB.open("HITDB", v);
  879. request.onsuccess = function(e) {
  880. HITStorage.indexedDB.db = e.target.result;
  881. var db = HITStorage.indexedDB.db;
  882. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  883. var store = trans.objectStore("HIT");
  884.  
  885. var request = store.get(id);
  886.  
  887. request.onsuccess = function(e) {
  888. db.close();
  889. showDetails(e.target.result.note);
  890. };
  891.  
  892. request.onerror = function(e) {
  893. console.log("Error Getting: ", e);
  894. };
  895. };
  896. request.onerror = HITStorage.indexedDB.onerror;
  897. };
  898.  
  899. HITStorage.indexedDB.addNote = function(id, note) {
  900. var request = indexedDB.open("HITDB", v);
  901. request.onsuccess = function(e) {
  902. HITStorage.indexedDB.db = e.target.result;
  903. var db = HITStorage.indexedDB.db;
  904. var trans = db.transaction(["NOTES"], HITStorage.IDBTransactionModes.READ_WRITE);
  905. var store = trans.objectStore("NOTES");
  906. var request;
  907.  
  908. if (note == '')
  909. request = store.delete(id);
  910. else
  911. request = store.put({requesterId: id, note: note});
  912.  
  913. request.onsuccess = function(e) {
  914. db.close();
  915. };
  916.  
  917. request.onerror = function(e) {
  918. console.log("Error Adding: ", e);
  919. };
  920. };
  921. request.onerror = HITStorage.indexedDB.onerror;
  922. };
  923.  
  924. HITStorage.indexedDB.blockHITS = function(requesterId, title, hitElement, titleElement) {
  925. var request = indexedDB.open("HITDB", v);
  926. request.onsuccess = function(e) {
  927. HITStorage.indexedDB.db = e.target.result;
  928. var db = HITStorage.indexedDB.db;
  929.  
  930. if (!db.objectStoreNames.contains("BLOCKS"))
  931. {
  932. db.close();
  933. return;
  934. }
  935. var trans = db.transaction(["BLOCKS"], HITStorage.IDBTransactionModes.READ_ONLY);
  936. var store = trans.objectStore("BLOCKS");
  937. var index = store.index("requesterId");
  938. var range = IDBKeyRange.only(requesterId);
  939.  
  940. index.openCursor(range).onsuccess = function(event) {
  941. var cursor = event.target.result;
  942. if (cursor && cursor.value.re)
  943. {
  944. if (cursor.value.re.test(title))
  945. {
  946. hitElement.style.display = 'none';
  947. titleElement.addEventListener("click", unblock_func(requesterId, title));
  948.  
  949. titleElement.style.fontSize = 'small';
  950.  
  951. // move blocked hits to the bottom
  952. var table = hitElement.parentNode.parentNode.parentNode.parentNode.parentNode;
  953. var hit = hitElement.parentNode.parentNode.parentNode.parentNode;
  954. table.removeChild(hit);
  955. table.appendChild(hit);
  956. }
  957. cursor.continue();
  958. }
  959. else
  960. {
  961. db.close();
  962. }
  963. };
  964. };
  965. request.onerror = HITStorage.indexedDB.onerror;
  966. };
  967.  
  968. HITStorage.indexedDB.addBlock = function(requesterId, re) {
  969. var request = indexedDB.open("HITDB", v);
  970. request.onsuccess = function(e) {
  971. HITStorage.indexedDB.db = e.target.result;
  972. var db = HITStorage.indexedDB.db;
  973. var trans = db.transaction(["BLOCKS"], HITStorage.IDBTransactionModes.READ_WRITE);
  974. var store = trans.objectStore("BLOCKS");
  975. var request;
  976.  
  977. request = store.put({requesterId: requesterId, re: re});
  978.  
  979. request.onsuccess = function(e) {
  980. db.close();
  981. };
  982. };
  983. request.onerror = HITStorage.indexedDB.onerror;
  984. };
  985.  
  986. // Removes all blocks for requesterId, where RE matches this HIT title
  987. HITStorage.indexedDB.removeBlocks = function(requesterId, title) {
  988. var request = indexedDB.open("HITDB", v);
  989. request.onsuccess = function(e) {
  990. HITStorage.indexedDB.db = e.target.result;
  991. var db = HITStorage.indexedDB.db;
  992. if (!db.objectStoreNames.contains("BLOCKS"))
  993. {
  994. db.close();
  995. return;
  996. }
  997. var trans = db.transaction(["BLOCKS"], HITStorage.IDBTransactionModes.READ_WRITE);
  998. var store = trans.objectStore("BLOCKS");
  999. var index = store.index("requesterId");
  1000. var range = IDBKeyRange.only(requesterId);
  1001.  
  1002. index.openCursor(range).onsuccess = function(event)
  1003. {
  1004. var cursor = event.target.result;
  1005. if (cursor)
  1006. {
  1007. if (cursor.value.re.test(title))
  1008. store.delete(cursor.value.id);
  1009. db.close();
  1010. }
  1011. };
  1012. };
  1013. request.onerror = HITStorage.indexedDB.onerror;
  1014. };
  1015.  
  1016. HITStorage.indexedDB.updateNoteButton = function(id, label) {
  1017. var request = indexedDB.open("HITDB", v);
  1018. request.onsuccess = function(e) {
  1019. HITStorage.indexedDB.db = e.target.result;
  1020. var db = HITStorage.indexedDB.db;
  1021.  
  1022. if (!db.objectStoreNames.contains("NOTES"))
  1023. {
  1024. label.title = 'Update HIT database on statusdetail page to use this feature';
  1025. db.close();
  1026. return;
  1027. }
  1028. var trans = db.transaction(["NOTES"], HITStorage.IDBTransactionModes.READ_ONLY);
  1029. var store = trans.objectStore("NOTES");
  1030.  
  1031. store.get(id).onsuccess = function(event)
  1032. {
  1033. if (event.target.result === undefined)
  1034. {
  1035. label.textContent = '';
  1036. }
  1037. else
  1038. {
  1039. var note = event.target.result.note;
  1040. label.textContent = note;
  1041. label.style.border = '1px dotted';
  1042. if (note.indexOf('!') >= 0)
  1043. label.style.color = 'red';
  1044. else
  1045. label.style.color = 'black';
  1046. }
  1047. db.close();
  1048. };
  1049. };
  1050. request.onerror = HITStorage.indexedDB.onerror;
  1051. };
  1052.  
  1053.  
  1054. HITStorage.indexedDB.colorRequesterButton = function(id, button) {
  1055. var request = indexedDB.open("HITDB", v);
  1056. request.onsuccess = function(e) {
  1057. HITStorage.indexedDB.db = e.target.result;
  1058. var db = HITStorage.indexedDB.db;
  1059. if (!db.objectStoreNames.contains("HIT"))
  1060. {
  1061. button.title = 'Update HIT database on statusdetail page to use this feature';
  1062. db.close();
  1063. return;
  1064. }
  1065. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  1066. var store = trans.objectStore("HIT");
  1067.  
  1068. var index = store.index("requesterId");
  1069. index.get(id).onsuccess = function(event)
  1070. {
  1071. if (event.target.result === undefined)
  1072. {
  1073. button.style.backgroundColor = 'pink';
  1074. }
  1075. else
  1076. {
  1077. button.style.backgroundColor = 'lightgreen';
  1078. button.style.fontWeight = 'bold';
  1079. }
  1080. db.close();
  1081. };
  1082. };
  1083. request.onerror = HITStorage.indexedDB.onerror;
  1084. };
  1085.  
  1086. HITStorage.indexedDB.colorTitleButton = function(title, button) {
  1087. var request = indexedDB.open("HITDB", v);
  1088. request.onsuccess = function(e) {
  1089. HITStorage.indexedDB.db = e.target.result;
  1090. var db = HITStorage.indexedDB.db;
  1091. if (!db.objectStoreNames.contains("HIT"))
  1092. {
  1093. button.title = 'Update HIT database on statusdetail page to use this feature';
  1094. db.close();
  1095. return;
  1096. }
  1097. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  1098. var store = trans.objectStore("HIT");
  1099.  
  1100. var index = store.index("title");
  1101. index.get(title).onsuccess = function(event)
  1102. {
  1103. if (event.target.result === undefined)
  1104. {
  1105. button.style.backgroundColor = 'pink';
  1106. }
  1107. else
  1108. {
  1109. button.style.backgroundColor = 'lightgreen';
  1110. button.style.fontWeight = 'bold';
  1111. }
  1112.  
  1113. db.close();
  1114. };
  1115. };
  1116. request.onerror = HITStorage.indexedDB.onerror;
  1117. };
  1118.  
  1119. HITStorage.indexedDB.deleteDB = function () {
  1120. var deleteRequest = indexedDB.deleteDatabase("HITDB");
  1121. deleteRequest.onsuccess = function (e)
  1122. {
  1123. alert("deleted");
  1124. }
  1125. deleteRequest.onblocked = function (e)
  1126. {
  1127. alert("blocked");
  1128. }
  1129. deleteRequest.onerror = HITStorage.indexedDB.onerror;
  1130. }
  1131.  
  1132. HITStorage.indexedDB.get_pending_approvals = function() {
  1133. var element = document.getElementById('pending_earnings_value');
  1134. var header_element = document.getElementById('pending_earnings_header');
  1135. if (element == null)
  1136. return;
  1137.  
  1138. var request = indexedDB.open("HITDB", v);
  1139. request.onsuccess = function(e) {
  1140. HITStorage.indexedDB.db = e.target.result;
  1141. var db = HITStorage.indexedDB.db;
  1142. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  1143. var store = trans.objectStore("HIT");
  1144.  
  1145. var result = 0;
  1146. var index;
  1147. var range;
  1148.  
  1149. index = store.index('status');
  1150. range = IDBKeyRange.only('Pending Approval');
  1151.  
  1152. index.openCursor(range).onsuccess = function(event) {
  1153. var cursor = event.target.result;
  1154. if (cursor) {
  1155. result += cursor.value.reward;
  1156. cursor.continue();
  1157. }
  1158. else {
  1159. element.textContent = '$' + result.toFixed(2);
  1160. if (header_element != null)
  1161. header_element.textContent = 'Pending earnings (HITDB updated: ' + localStorage['HITDB UPDATED']+ ')';
  1162. }
  1163. db.close();
  1164. };
  1165. };
  1166. request.onerror = HITStorage.indexedDB.onerror;
  1167. };
  1168.  
  1169. HITStorage.indexedDB.get_pending_payments = function() {
  1170. var element = document.getElementById('pending_earnings_value');
  1171. if (element == null)
  1172. return;
  1173.  
  1174. var request = indexedDB.open("HITDB", v);
  1175. request.onsuccess = function(e) {
  1176. HITStorage.indexedDB.db = e.target.result;
  1177. var db = HITStorage.indexedDB.db;
  1178. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  1179. var store = trans.objectStore("HIT");
  1180.  
  1181. var result = 0;
  1182. var index;
  1183. var range;
  1184.  
  1185. index = store.index('status');
  1186. range = IDBKeyRange.only('Approved&nbsp;- Pending&nbsp;Payment');
  1187.  
  1188. index.openCursor(range).onsuccess = function(event) {
  1189. var cursor = event.target.result;
  1190. if (cursor) {
  1191. result += cursor.value.reward;
  1192. cursor.continue();
  1193. }
  1194. else {
  1195. element.title = 'Approved - Pending Payment: $' + result.toFixed(2);
  1196. }
  1197. }
  1198. db.close();
  1199. };
  1200. request.onerror = HITStorage.indexedDB.onerror;
  1201. };
  1202.  
  1203. HITStorage.indexedDB.get_todays_projected_earnings = function(date) {
  1204. var element = document.getElementById('projected_earnings_value');
  1205. if (element == null)
  1206. return;
  1207.  
  1208. var request = indexedDB.open("HITDB", v);
  1209. request.onsuccess = function(e) {
  1210. HITStorage.indexedDB.db = e.target.result;
  1211. var db = HITStorage.indexedDB.db;
  1212. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  1213. var store = trans.objectStore("HIT");
  1214.  
  1215. var result = 0;
  1216. var rejected = 0;
  1217. var index;
  1218. var range;
  1219.  
  1220. index = store.index('date');
  1221. range = IDBKeyRange.only(date);
  1222.  
  1223. index.openCursor(range).onsuccess = function(event) {
  1224. var cursor = event.target.result;
  1225. if (cursor) {
  1226. if (cursor.value.status == 'Rejected')
  1227. rejected += cursor.value.reward;
  1228. else
  1229. result += cursor.value.reward;
  1230. cursor.continue();
  1231. }
  1232. else {
  1233. element.textContent = '$' + result.toFixed(2);
  1234. element.title = '$' + rejected.toFixed(2) + ' rejected';
  1235.  
  1236. if (localStorage['TODAYS TARGET'] !== undefined)
  1237. {
  1238. var target = parseFloat(localStorage['TODAYS TARGET']).toFixed(2);
  1239. var my_target = document.getElementById('my_target');
  1240.  
  1241. var progress = Math.floor(result/target*40);
  1242. if (progress > 40)
  1243. progress = 40;
  1244. my_target.innerHTML = progress_bar(progress, 40, '█', '█', '#7fb448', 'grey') + '&nbsp;' +
  1245. ((result>target)? '+' : '') + (result-target).toFixed(2);
  1246. my_target.style.fontSize = '9px';
  1247. }
  1248. }
  1249. }
  1250. db.close();
  1251. };
  1252. request.onerror = HITStorage.indexedDB.onerror;
  1253. };
  1254.  
  1255. // Update database date format from MMDDYYYY to YYYY-MM-DD
  1256. // Shouldn't break anything even if used on already updated db
  1257. HITStorage.update_date_format = function(verbose)
  1258. {
  1259. var request = indexedDB.open("HITDB", v);
  1260. request.onsuccess = function(e) {
  1261. HITStorage.indexedDB.db = e.target.result;
  1262. var db = HITStorage.indexedDB.db;
  1263. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_WRITE);
  1264. var store = trans.objectStore("HIT");
  1265.  
  1266. store.openCursor().onsuccess = function(event) {
  1267. var cursor = event.target.result;
  1268. if (cursor)
  1269. {
  1270. if (cursor.value.date.indexOf('-') < 0)
  1271. {
  1272. var i = cursor.value;
  1273. i.date = convert_date(i.date);
  1274. i.requesterName = i.requesterName.trim();
  1275. i.title = i.title.trim();
  1276. cursor.update(i);
  1277. }
  1278. cursor.continue();
  1279. }
  1280. else
  1281. {
  1282. db.close();
  1283. HITStorage.update_stats_date_format(verbose);
  1284. }
  1285. };
  1286. }
  1287. }
  1288.  
  1289. HITStorage.update_stats_date_format = function(verbose)
  1290. {
  1291. var request = indexedDB.open("HITDB", v);
  1292. request.onsuccess = function(e) {
  1293. HITStorage.indexedDB.db = e.target.result;
  1294. var db = HITStorage.indexedDB.db;
  1295. var trans = db.transaction(["STATS"], HITStorage.IDBTransactionModes.READ_WRITE);
  1296. var store = trans.objectStore("STATS");
  1297.  
  1298. store.openCursor().onsuccess = function(event) {
  1299. var cursor = event.target.result;
  1300. if (cursor)
  1301. {
  1302. if (cursor.value.date.indexOf('-') < 0)
  1303. {
  1304. var i = cursor.value;
  1305. i.date = convert_date(i.date);
  1306. cursor.delete();
  1307. store.put(i);
  1308. }
  1309. cursor.continue();
  1310. }
  1311. else
  1312. {
  1313. // DB should be fully updated
  1314. db.close();
  1315. if (verbose == true)
  1316. alert('Date conversion done.');
  1317. }
  1318. };
  1319. }
  1320. };
  1321.  
  1322. /* ------------------------------------------------------------- */
  1323.  
  1324. HITStorage.prepare_donut = function (donutData, type)
  1325. {
  1326. if (type == '---')
  1327. return;
  1328. var countHits = true;
  1329. if (type.match('REWARDS'))
  1330. countHits = false;
  1331.  
  1332. var tmpData = {};
  1333. var topRequesters = [];
  1334. var topHits = [];
  1335. var sum = 0;
  1336.  
  1337. for (var i=0; i < donutData.length; i++) {
  1338. var requesterName = donutData[i].requesterName.trim() + " (" + donutData[i].requesterId + ")";
  1339. var hitTitle = donutData[i].title;
  1340. var hitReward = donutData[i].reward;
  1341. sum += (countHits) ? 1 : hitReward;
  1342.  
  1343. if (tmpData[requesterName]) {
  1344. tmpData[requesterName]['HITS'] += (countHits) ? 1 : hitReward;
  1345. }
  1346. else {
  1347. tmpData[requesterName] = {};
  1348. tmpData[requesterName]['HITS'] = (countHits) ? 1 : hitReward;
  1349. }
  1350. if (tmpData[requesterName][hitTitle])
  1351. tmpData[requesterName][hitTitle] += (countHits) ? 1 : hitReward;
  1352. else
  1353. tmpData[requesterName][hitTitle] = (countHits) ? 1 : hitReward;
  1354.  
  1355. }
  1356.  
  1357. for (var key in tmpData) {
  1358. topRequesters.push({name: key, y: tmpData[key]['HITS']});
  1359. }
  1360. topRequesters.sort(function(a,b){return b.y-a.y});
  1361.  
  1362. var colors = Highcharts.getOptions().colors;
  1363.  
  1364. for (var i=0; i<topRequesters.length; i++) {
  1365. var tmpHits = [];
  1366. topRequesters[i].color = colors[i];
  1367. for (var key2 in tmpData[topRequesters[i].name]) {
  1368. if (key2 != 'HITS') {
  1369. tmpHits.push({name: key2, y: tmpData[topRequesters[i].name][key2], color: colors[i]});
  1370. }
  1371. }
  1372. tmpHits.sort(function(a,b){return b.y-a.y});
  1373. for (var j=0; j<tmpHits.length ; j++) {
  1374. var brightness = 0.2 - (j / tmpHits.length) / 5;
  1375. tmpHits[j].color = Highcharts.Color(colors[i]).brighten(brightness).get();
  1376. }
  1377. topHits = topHits.concat(tmpHits);
  1378. }
  1379.  
  1380. document.getElementById('container').style.display = 'block';
  1381.  
  1382.  
  1383. chart = new Highcharts.Chart({
  1384. chart: {
  1385. renderTo: 'container',
  1386. type: 'pie'
  1387. },
  1388. title: {
  1389. text: 'Requesters and HITs matching your latest search'
  1390. },
  1391. yAxis: {
  1392. title: {
  1393. text: ''
  1394. }
  1395. },
  1396. plotOptions: {
  1397. pie: {
  1398. shadow: false,
  1399. dataLabels: { enabled: true}
  1400. }
  1401. },
  1402. tooltip: {
  1403. animation: false,
  1404. valuePrefix: (countHits)? '' : '$',
  1405. valueSuffix: (countHits)? ' HITs' : '',
  1406. valueDecimals: (countHits)? 0 : 2,
  1407. pointFormat: (countHits)? '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> (of all ' + sum + ' HITs)<br/>' :
  1408. '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> (of all $' + sum.toFixed(2) + ')<br/>'
  1409. },
  1410. series: [{
  1411. name: 'Requesters',
  1412. data: topRequesters,
  1413. size: '60%',
  1414. dataLabels: {
  1415. formatter: function() {
  1416. if (countHits) {
  1417. return this.y/sum >= 0.20 ? this.point.name: null;
  1418. }
  1419. else {
  1420. return this.y/sum >= 0.20 ? this.point.name : null;
  1421. }
  1422. },
  1423. color: 'black',
  1424. distance: -10
  1425. }
  1426. }, {
  1427. name: 'HITs',
  1428. data: topHits,
  1429. innerSize: '60%',
  1430. dataLabels: {
  1431. formatter: function() {
  1432. if (countHits) {
  1433. return this.y/sum > 0.05 ? this.point.name : null;
  1434. }
  1435. else {
  1436. return this.y/sum > 0.05 ? this.point.name : null;
  1437. }
  1438. },
  1439. color: 'black',
  1440. }
  1441. }]
  1442. });
  1443. }
  1444.  
  1445. // Stolen from Today's Projected Earnings (http://userscripts.org/scripts/show/95331)
  1446. HITStorage.getHTTPObject = function()
  1447. {
  1448. if (typeof XMLHttpRequest != 'undefined')
  1449. {
  1450. return new XMLHttpRequest();
  1451. }
  1452. try
  1453. {
  1454. return new ActiveXObject("Msxml2.XMLHTTP");
  1455. }
  1456. catch (e)
  1457. {
  1458. try
  1459. {
  1460. return new ActiveXObject("Microsoft.XMLHTTP");
  1461. }
  1462. catch (e) {}
  1463. }
  1464. return false;
  1465. }
  1466.  
  1467. // Stolen from Today's Projected Earnings (http://userscripts.org/scripts/show/95331)
  1468. // date format MMDDYYYY!
  1469. HITStorage.process_page = function(link, date, hitData)
  1470. {
  1471. var page = HITStorage.getHTTPObject();
  1472. page.open("GET", link, false);
  1473. page.send(null);
  1474. return HITStorage.parse_data(page.responseText, date, hitData);
  1475. }
  1476.  
  1477. // Partly stolen from Today's Projected Earnings (http://userscripts.org/scripts/show/95331)
  1478. // date format MMDDYYYY!
  1479. HITStorage.parse_data = function(page_text, date, hitData)
  1480. {
  1481. var index = 0;
  1482. var index2 = 0;
  1483. var page_html = document.createElement('div');
  1484. page_html.innerHTML = page_text;
  1485.  
  1486. var requesters = page_html.getElementsByClassName('statusdetailRequesterColumnValue');
  1487. var titles = page_html.getElementsByClassName('statusdetailTitleColumnValue');
  1488. var amounts = page_html.getElementsByClassName('statusdetailAmountColumnValue');
  1489. var statuses = page_html.getElementsByClassName('statusdetailStatusColumnValue');
  1490. var feedbacks = page_html.getElementsByClassName('statusdetailRequesterFeedbackColumnValue');
  1491.  
  1492. var requesterName;
  1493. var hitTitle;
  1494. var hitReward;
  1495. var hitStatus;
  1496. var requesterId;
  1497. var hitId;
  1498.  
  1499. for(var k = 0; k < amounts.length; k++)
  1500. {
  1501. requesterName = requesters[k].textContent;
  1502. requesterLink = requesters[k].childNodes[1].href;
  1503. hitTitle = titles[k].textContent;
  1504. index = amounts[k].innerHTML.indexOf('$');
  1505. hitReward = parseFloat(amounts[k].innerHTML.substring(index+1));
  1506. hitStatus = statuses[k].innerHTML;
  1507. hitFeedback = feedbacks[k].textContent;
  1508.  
  1509.  
  1510. requesterId = getQueryVariable(requesterLink,"requesterId");
  1511. subject = getQueryVariable(requesterLink,"subject");
  1512. subject = subject.split("+");
  1513. hitId = subject[subject.length-1];
  1514.  
  1515. var hit = {
  1516. hitId : hitId,
  1517. date : convert_date(date),
  1518. requesterName : requesterName.trim(),
  1519. requesterLink : requesterLink.trim(),
  1520. title : hitTitle.trim(),
  1521. reward : hitReward,
  1522. status : hitStatus,
  1523. feedback : hitFeedback.trim(),
  1524. requesterId : requesterId
  1525. };
  1526.  
  1527. //HITStorage.indexedDB.addHIT(hitData);
  1528. hitData.push(hit);
  1529. }
  1530.  
  1531. return amounts.length;
  1532. }
  1533.  
  1534. //Used to simplify getting requester ID's and such
  1535. function getQueryVariable(url,variable)
  1536. {
  1537. var query = url.substring(1);
  1538. var vars = query.split("?")[1].split("&");
  1539. for (var i=0;i<vars.length;i++)
  1540. {
  1541. var pair = vars[i].split("=");
  1542. if(pair[0] == variable)
  1543. {
  1544. return pair[1];
  1545. }
  1546. }
  1547. return(false);
  1548. }
  1549.  
  1550. // Returns available days (YYYY-MM-DD)
  1551. HITStorage.getAllAvailableDays = function(try_extra_days)
  1552. {
  1553. var days = [];
  1554.  
  1555. var page = HITStorage.getHTTPObject();
  1556. page.open("GET", 'https://www.mturk.com/mturk/status', false);
  1557. page.send(null);
  1558.  
  1559. var page_html = document.createElement('div');
  1560. page_html.innerHTML = page.responseText;
  1561.  
  1562. var dateElements = page_html.getElementsByClassName('statusDateColumnValue');
  1563. var submittedElements = page_html.getElementsByClassName('statusSubmittedColumnValue');
  1564. var approvedElements = page_html.getElementsByClassName('statusApprovedColumnValue');
  1565. var rejectedElements = page_html.getElementsByClassName('statusRejectedColumnValue');
  1566. var pendingElements = page_html.getElementsByClassName('statusPendingColumnValue');
  1567. var earningsElements = page_html.getElementsByClassName('statusEarningsColumnValue');
  1568.  
  1569. for (var i=0; i<dateElements.length; i++)
  1570. {
  1571. var date = dateElements[i].childNodes[1].href.substr(53);
  1572. date = convert_date(date);
  1573.  
  1574. days.push( { date: date,
  1575. submitted: parseInt(submittedElements[i].textContent),
  1576. approved : parseInt(approvedElements[i].textContent),
  1577. rejected : parseInt(rejectedElements[i].textContent),
  1578. pending : parseInt(pendingElements[i].textContent),
  1579. earnings : parseFloat(earningsElements[i].textContent.slice(1)) });
  1580. }
  1581.  
  1582. if (try_extra_days > 0)
  1583. {
  1584. var date = days[days.length-1].date;
  1585. var d = new Date();
  1586. d.setFullYear(parseInt(date.substr(0,4)), parseInt(date.substr(5,2))-1, parseInt(date.substr(8,2)));
  1587.  
  1588. for (var i=0; i<try_extra_days; i++)
  1589. {
  1590. d.setDate(d.getDate()-1);
  1591. var month = '0' + (d.getMonth() + 1);
  1592. var day = '0' + d.getDate();
  1593. if (month.length > 2)
  1594. month = month.substr(1);
  1595. if (day.length > 2)
  1596. day = day.substr(1);
  1597. date = '' + d.getFullYear() + '-' + month + '-' + day;
  1598.  
  1599. days.push( { date: date,
  1600. submitted: -1,
  1601. approved : -1,
  1602. rejected : -1,
  1603. pending : -1,
  1604. earnings : -1 } );
  1605. }
  1606. }
  1607.  
  1608. return days;
  1609. }
  1610.  
  1611. HITStorage.getLatestHITs = function()
  1612. {
  1613. if (localStorage['HITDB AUTO UPDATE'] === undefined || localStorage['HITDB AUTO UPDATE'] == 'OFF')
  1614. return;
  1615.  
  1616. if (localStorage['HITDB TIMESTAMP'] !== undefined)
  1617. {
  1618. if (new Date().getTime() < new Date(parseInt(localStorage['HITDB TIMESTAMP'])).getTime() + 90000)
  1619. {
  1620. return;
  1621. }
  1622. }
  1623. localStorage['HITDB TIMESTAMP'] = new Date().getTime();
  1624.  
  1625. var auto_button = document.getElementById('auto_button');
  1626. var page = HITStorage.getHTTPObject();
  1627. page.open("GET", 'https://www.mturk.com/mturk/status', false);
  1628. page.send(null);
  1629. auto_button.textContent += ' +';
  1630.  
  1631. var page_html = document.createElement('div');
  1632. page_html.innerHTML = page.responseText;
  1633.  
  1634. var dateElements = page_html.getElementsByClassName('statusDateColumnValue');
  1635. var submittedElements = page_html.getElementsByClassName('statusSubmittedColumnValue');
  1636. var approvedElements = page_html.getElementsByClassName('statusApprovedColumnValue');
  1637. var rejectedElements = page_html.getElementsByClassName('statusRejectedColumnValue');
  1638. var pendingElements = page_html.getElementsByClassName('statusPendingColumnValue');
  1639. var earningsElements = page_html.getElementsByClassName('statusEarningsColumnValue');
  1640.  
  1641. if (dateElements[0].childNodes[1].textContent.trim() != 'Today')
  1642. return;
  1643.  
  1644. var url = dateElements[0].childNodes[1].href;
  1645. var date = url.substr(53); // keep MMDDYYYY
  1646. var submitted = parseInt(submittedElements[0].textContent);
  1647. //var approved = parseInt(approvedElements[0].textContent);
  1648. //var rejected = parseInt(rejectedElements[0].textContent);
  1649. //var pending = parseInt(pendingElements[0].textContent);
  1650. //var earnings = parseFloat(earningsElements[0].textContent.slice(1));
  1651. var pages_done = null;
  1652. if (localStorage['HITDB AUTOUPDATE PAGES'] !== undefined)
  1653. {
  1654. pages_done = JSON.parse(localStorage['HITDB AUTOUPDATE PAGES']);
  1655. }
  1656. if (pages_done == null || pages_done.date != date)
  1657. pages_done = {date: date};
  1658.  
  1659. var new_hits = 0;
  1660. var page = 1 + Math.floor(submitted/25);
  1661. page = (page<1) ? 1 : page;
  1662.  
  1663. var hitData = [];
  1664. if (submitted != pages_done.submitted)
  1665. {
  1666. url = "https://www.mturk.com/mturk/statusdetail?sortType=All&pageNumber=" + page + "&encodedDate=" + date;
  1667. HITStorage.process_page(url, date, hitData);
  1668. new_hits += submitted - pages_done.submitted;
  1669. pages_done.submitted = submitted;
  1670. localStorage['HITDB AUTOUPDATE PAGES'] = JSON.stringify(pages_done);
  1671. auto_button.textContent += '+';
  1672. }
  1673.  
  1674. if (page > 1)
  1675. {
  1676. extra_page = page-1;
  1677.  
  1678. while (extra_page >= 1)
  1679. {
  1680. if (pages_done[extra_page] != true)
  1681. {
  1682. url = "https://www.mturk.com/mturk/statusdetail?sortType=All&pageNumber=" + extra_page + "&encodedDate=" + date;
  1683. if (HITStorage.process_page(url, date, hitData) == 25)
  1684. {
  1685. pages_done[extra_page] = true;
  1686. localStorage['HITDB AUTOUPDATE PAGES'] = JSON.stringify(pages_done);
  1687. auto_button.textContent += '+';
  1688. }
  1689. break;
  1690. }
  1691. extra_page -= 1;
  1692. }
  1693. }
  1694. HITStorage.indexedDB.addHITs(hitData);
  1695. }
  1696.  
  1697. // Gets status details for given date (MMDDYYYY)
  1698. // Collects all HITs for given date to hitData array
  1699. HITStorage.getHITData = function(day_to_fetch, hitData, page, days_to_update)
  1700. {
  1701. var dataDate = convert_iso_date(day_to_fetch.date);
  1702. page = page || 1;
  1703. detailed_status_page_link = "https://www.mturk.com/mturk/statusdetail?sortType=All&pageNumber=" + page + "&encodedDate=" + dataDate;
  1704.  
  1705. if (HITStorage.process_page(detailed_status_page_link, dataDate, hitData) == 0)
  1706. {
  1707. if (day_to_fetch.submitted == -1 || hitData.length == day_to_fetch.submitted)
  1708. {
  1709. setTimeout(function(){ HITStorage.indexedDB.addHITs(hitData, day_to_fetch, days_to_update); }, 1000);
  1710. }
  1711. else
  1712. {
  1713. alert("There was an error while fetching HITs for date: " + day_to_fetch.date + ".\n" +
  1714. "Script monkeys expected " + day_to_fetch.submitted + " bananas, but got " + hitData.length + "! ?");
  1715. HITStorage.update_done();
  1716. }
  1717. }
  1718. else
  1719. {
  1720. HITStorage.update_status_label('Please wait: script monkeys are fetching status pages (' +
  1721. day_to_fetch.date + ', page ' + page + ')', 'red');
  1722. setTimeout(function(){ HITStorage.getHITData(day_to_fetch, hitData, page+1, days_to_update); }, 1000);
  1723. }
  1724. }
  1725.  
  1726. HITStorage.formatTime = function(msec)
  1727. {
  1728. if (isNaN(msec))
  1729. return "-";
  1730. var seconds = Math.floor(msec / 1000) % 60;
  1731. var minutes = Math.floor((msec / 1000) / 60) % 60;
  1732. var hours = Math.floor(((msec / 1000) / 60) / 60) % 24;
  1733. var days = Math.floor(((msec / 1000) / 60) / 60 / 24);
  1734.  
  1735. if (hours > 0)
  1736. seconds = "";
  1737. else
  1738. seconds = "" + seconds + "s";
  1739. minutes == 0 ? minutes = "" : minutes = "" + minutes + "m ";
  1740. hours == 0 ? hours = "" : hours = "" + hours + "h ";
  1741.  
  1742. if (days > 0)
  1743. return '' + days + ' day' + ((days>1)? 's' : ' ') + hours;
  1744. return hours + minutes + seconds;
  1745. }
  1746.  
  1747. HITStorage.update_status_label = function(new_status, color)
  1748. {
  1749. var label = document.getElementById('status_label');
  1750. label.innerHTML = new_status;
  1751. label.style.color = color || 'black';
  1752. }
  1753.  
  1754. // validate input field dates
  1755. // Accept YYYY-MM-DD
  1756. HITStorage.validate_date = function(input)
  1757. {
  1758. date = input.value;
  1759.  
  1760. if (date.match(/^[01]\d\/[0123]\d\/20\d\d$/) != null)
  1761. {
  1762. var d = date.split('\/');
  1763. date = d[2] + '-' + d[0] + '-' + d[1];
  1764. input.value = date;
  1765. }
  1766.  
  1767. if (date.match(/^$|^20\d\d\-[01]\d\-[0123]\d$/) != null)
  1768. {
  1769. input.style.backgroundColor = 'white';
  1770. return true;
  1771. }
  1772. input.style.backgroundColor = 'pink';
  1773. return false;
  1774. }
  1775.  
  1776. HITStorage.validate_dates = function()
  1777. {
  1778. from = document.getElementById('from_date');
  1779. to = document.getElementById('to_date');
  1780.  
  1781. if (HITStorage.validate_date(from) && HITStorage.validate_date(to))
  1782. {
  1783. if (from.value > to.value && to.value != '')
  1784. {
  1785. alert('Invalid date!');
  1786. return false;
  1787. }
  1788.  
  1789. return true;
  1790. }
  1791. alert('Invalid date!');
  1792. return false;
  1793. }
  1794.  
  1795. HITStorage.start_search = function()
  1796. {
  1797. if (HITStorage.validate_dates() == false)
  1798. return;
  1799.  
  1800. HITStorage.update_status_label('Using local HIT database', 'green');
  1801.  
  1802. var options = {};
  1803. options.term = document.getElementById('search_term').value;
  1804. options.status = document.getElementById('status_select').value;
  1805. options.donut = document.getElementById('donut_select').value;
  1806. options.from_date = document.getElementById('from_date').value;
  1807. options.to_date = document.getElementById('to_date').value;
  1808. options.export_csv = document.getElementById('export_csv').checked;
  1809.  
  1810. HITStorage.disable_inputs();
  1811. setTimeout(function(){ HITStorage.do_search(options); }, 500);
  1812. }
  1813.  
  1814. HITStorage.disable_inputs = function()
  1815. {
  1816. document.getElementById('delete_button').disabled = true;
  1817. document.getElementById('search_button').disabled = true;
  1818. document.getElementById('update_button').disabled = true;
  1819. document.getElementById('overview_button').disabled = true;
  1820. document.getElementById('import_button').disabled = true;
  1821. document.getElementById('pending_button').disabled = true;
  1822. document.getElementById('status_button').disabled = true;
  1823. document.getElementById('from_date').disabled = true;
  1824. document.getElementById('to_date').disabled = true;
  1825. document.getElementById('search_term').disabled = true;
  1826. document.getElementById('status_select').disabled = true;
  1827. document.getElementById('donut_select').disabled = true;
  1828. }
  1829.  
  1830. HITStorage.enable_inputs = function()
  1831. {
  1832. document.getElementById('delete_button').disabled = false;
  1833. document.getElementById('search_button').disabled = false;
  1834. document.getElementById('update_button').disabled = false;
  1835. document.getElementById('overview_button').disabled = false;
  1836. document.getElementById('import_button').disabled = false;
  1837. document.getElementById('pending_button').disabled = false;
  1838. document.getElementById('status_button').disabled = false;
  1839. document.getElementById('from_date').disabled = false;
  1840. document.getElementById('to_date').disabled = false;
  1841. document.getElementById('search_term').disabled = false;
  1842. document.getElementById('status_select').disabled = false;
  1843. document.getElementById('donut_select').disabled = false;
  1844. }
  1845.  
  1846.  
  1847. HITStorage.do_search = function(options)
  1848. {
  1849. HITStorage.indexedDB.getHITs(options);
  1850.  
  1851. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 3000);
  1852.  
  1853. HITStorage.enable_inputs();
  1854. }
  1855.  
  1856. HITStorage.show_results = function(results)
  1857. {
  1858. resultsWindow = window.open();
  1859. resultsWindow.document.write("<html><head><title>Status Detail Search Results</title></head><body>\n");
  1860. resultsWindow.document.write("<h1>HITs matching your search:</h1>\n");
  1861. resultsWindow.document.write('<table style="border: 1px solid black;border-collapse:collapse;width:90%;margin-left:auto;margin-right:auto;">\n');
  1862. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>Date</th><th>Requester</th><th>HIT Title</th><th>Reward</th><th>Status</th><th>Feedback</th></tr>\n');
  1863.  
  1864. var odd = true;
  1865. var sum = 0;
  1866. var sum_rejected = 0;
  1867. var sum_approved = 0;
  1868. var sum_pending = 0;
  1869.  
  1870.  
  1871. var new_day = false;
  1872.  
  1873. for (var i=0; i<results.length; i++) {
  1874. odd = !odd;
  1875. sum += results[i].reward;
  1876. if (results[i].status == 'Rejected')
  1877. sum_rejected += results[i].reward;
  1878. else if (results[i].status == 'Pending Approval')
  1879. sum_pending += results[i].reward;
  1880. else
  1881. sum_approved += results[i].reward;
  1882.  
  1883. if (i>0 && (results[i-1].date != results[i].date))
  1884. new_day = true;
  1885. else
  1886. new_day = false;
  1887. resultsWindow.document.write(HITStorage.format_hit_line(results[i], odd, HITStorage.status_color(results[i].status), new_day ));
  1888. }
  1889.  
  1890. resultsWindow.document.write('<tr style="background-color:lightgrey"><th></th><th></th><th></th><th>$' + sum.toFixed(2) + '</th><th></th><th></th></tr>\n');
  1891. resultsWindow.document.write("</table>");
  1892. resultsWindow.document.write("<p>Found " + results.length + " matching HITs. $" + sum_approved.toFixed(2) + " approved, " +
  1893. "$" + sum_rejected.toFixed(2) + " rejected and $" + sum_pending.toFixed(2) + " pending.</p>");
  1894. resultsWindow.document.write("</body></html>")
  1895. resultsWindow.document.close();
  1896. }
  1897.  
  1898. HITStorage.status_color = function(status)
  1899. {
  1900. var color = "green";
  1901.  
  1902. if (status.match("Pending Approval"))
  1903. color = "orange";
  1904. else if (status.match("Rejected"))
  1905. color = "red";
  1906.  
  1907. return color;
  1908. }
  1909.  
  1910. HITStorage.format_hit_line = function(hit, odd, status_color, new_day)
  1911. {
  1912. var line = '<tr style="background-color:';
  1913. if (odd)
  1914. line += '#f1f3eb;';
  1915. else
  1916. line += 'white;';
  1917. line += ' valign=top;';
  1918. if (new_day)
  1919. line += ' border: 0px dotted #000000; border-width: 2px 0px 0px 0px">';
  1920. else
  1921. line += '">';
  1922.  
  1923. line += '<td>' + hit.date + '</td>';
  1924. if (hit.requesterLink != null)
  1925. line += '<td style="width:165px"><a href="' + hit.requesterLink + '" title="Contact this Requester">' + hit.requesterName + '</a></td>';
  1926. else
  1927. line += '<td style="width:165px">' + hit.requesterName + '</td>';
  1928. line += '<td style="width:213px">' + hit.title + '</td>';
  1929. line += '<td style="width:45px">$' + hit.reward.toFixed(2) + '</td>';
  1930. line += '<td style="color:' + status_color + '; width:55px">' + hit.status + '</td>';
  1931. line += '<td><div style="width:225px; overflow:hidden">' + hit.feedback + '</div></td>';
  1932. line += '</tr>\n';
  1933. return line;
  1934. }
  1935.  
  1936. HITStorage.show_pending_overview = function(results)
  1937. {
  1938. resultsWindow = window.open();
  1939. resultsWindow.document.write("<html><head><title>Summary of Pending HITs</title></head><body>\n");
  1940. resultsWindow.document.write("<h1>Summary of Pending HITs</h1>\n");
  1941. resultsWindow.document.write('<table style="border: 1px solid black;border-collapse:collapse;width:90%;margin-left:auto;margin-right:auto;">\n');
  1942. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>requesterId</th><th>Requester</th><th></th><th>Pending</th><th>Rewards</th>\n');
  1943.  
  1944. // 'requesterId,requesterName,pending,reward';
  1945. var odd = false;
  1946. var sum = 0;
  1947. var pending = 0;
  1948.  
  1949. for (var i=0; i<results.length; i++) {
  1950. odd = !odd;
  1951. sum += results[i][3];
  1952. pending += results[i][2];
  1953. resultsWindow.document.write(HITStorage.format_pending_line(results[i], odd, i));
  1954. }
  1955.  
  1956. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>' + results.length + ' different requesterIds</th><th></th><th></th><th style="text-align: right">' + pending + '</th><th style="text-align: right">$' + sum.toFixed(2) + '</th>\n');
  1957. resultsWindow.document.write("</table>");
  1958. resultsWindow.document.write("</body></html>")
  1959. resultsWindow.document.close();
  1960.  
  1961. for (var i=0; i<results.length; i++)
  1962. {
  1963. resultsWindow.document.getElementById('id-' + i).addEventListener("click", search_func(results[i][0], 'requesterId'), false);
  1964. resultsWindow.document.getElementById('id2-' + i).addEventListener("click", show_requester_func(results[i][0]) , false);
  1965. }
  1966. }
  1967.  
  1968. HITStorage.show_status_overview = function(results, date)
  1969. {
  1970. resultsWindow = window.open();
  1971. resultsWindow.document.write("<html><head><title>Daily HIT stats</title></head><body>\n");
  1972. if (date)
  1973. resultsWindow.document.write("<h1>Daily HIT stats</h1>\n");
  1974. else
  1975. resultsWindow.document.write("<h1>Daily HIT stats (' + date + ')</h1>\n");
  1976. resultsWindow.document.write('<table style="border: 1px solid black;border-collapse:collapse;width:90%;margin-left:auto;margin-right:auto;">\n');
  1977. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>Date</th><th>Submitted</th><th>Approved</th><th>Rejected</th><th>Pending</th><th>Earnings</th>\n');
  1978.  
  1979. var odd = false;
  1980. var sum = 0;
  1981. var submitted = 0;
  1982. var approved = 0;
  1983. var rejected = 0;
  1984. var pending = 0;
  1985. var new_month = false;
  1986.  
  1987. for (var i=results.length-1; i>=0; i--) {
  1988. odd = !odd;
  1989. sum += results[i].earnings;
  1990. submitted += results[i].submitted;
  1991. approved += results[i].approved;
  1992. rejected += results[i].rejected;
  1993. pending += results[i].pending;
  1994. if (i<results.length-1)
  1995. new_month = (results[i].date.substr(0,7) != results[i+1].date.substr(0,7));
  1996. resultsWindow.document.write(HITStorage.format_status_line(results[i], odd, new_month));
  1997. }
  1998.  
  1999. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>' + results.length + ' days</th><th style="text-align: left">' + submitted +
  2000. '</th><th style="text-align: left">' + approved +
  2001. '</th><th style="text-align: left">' + rejected +
  2002. '</th><th style="text-align: left">' + pending +
  2003. '</th><th style="text-align: left">$' + sum.toFixed(2) + '</th>\n');
  2004. resultsWindow.document.write("</table>");
  2005. resultsWindow.document.write("</body></html>")
  2006. resultsWindow.document.close();
  2007.  
  2008. for (var i=0; i<results.length; i++)
  2009. resultsWindow.document.getElementById(results[i].date).addEventListener("click", search_func('', 'date', results[i].date, results[i].date), false);
  2010. }
  2011.  
  2012. HITStorage.show_requester_overview = function(results, date)
  2013. {
  2014. resultsWindow = window.open();
  2015. resultsWindow.document.write("<html><head><title>Requester Overview</title></head><body>\n");
  2016. if (date)
  2017. resultsWindow.document.write("<h1>Requester Overview " + date + "</h1>\n");
  2018. else
  2019. resultsWindow.document.write("<h1>Requester Overview</h1>\n");
  2020. resultsWindow.document.write('<table style="border: 1px solid black;border-collapse:collapse;width:90%;margin-left:auto;margin-right:auto;">\n');
  2021. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>requesterId</th><th>Requester</th><th></th><th>HITs</th><th>Pending</th><th>Rewards</th><th colspan="2">Rejected</th></tr>\n');
  2022.  
  2023. // 'requesterId,requesterName,hits,pending,reward,rejected';
  2024. var odd = false;
  2025. var sum = 0;
  2026. var hits = 0;
  2027. var rejected = 0;
  2028. var pending = 0;
  2029. var new_day = false;
  2030. var top = true;
  2031. var dot_line;
  2032.  
  2033. for (var i=0; i<results.length; i++) {
  2034. odd = !odd;
  2035. sum += results[i][3];
  2036. hits += results[i][2];
  2037. rejected += results[i][4];
  2038. pending += results[i][5];
  2039. dot_line = false;
  2040. if (i==10)
  2041. {
  2042. dot_line = true;
  2043. top = false;
  2044. }
  2045. if (i>10 && results[i][3] == 0 && results[i-1][3] != 0)
  2046. dot_line = true;
  2047.  
  2048. resultsWindow.document.write(HITStorage.format_overview_line(results[i], odd, dot_line, top, i));
  2049. }
  2050.  
  2051. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>' + results.length + ' different requesterIds</th>' +
  2052. '<th></th><th></th><th style="text-align: right">' + hits + '<th style="text-align: right">' + pending +
  2053. '</th><th style="text-align: right">$' + sum.toFixed(2) + '</th><th style="text-align: right">' + rejected + '</th>' +
  2054. '<th style="text-align: right">' +
  2055. (rejected/hits*100).toFixed(2) + '%</th></tr>\n');
  2056. resultsWindow.document.write("</table>");
  2057. resultsWindow.document.write("<p>Reward includes all 'Paid' and 'Approved - Pending Payment' HITs. " +
  2058. "Reward does not include any bonuses.</p>");
  2059. resultsWindow.document.write("</body></html>")
  2060. resultsWindow.document.close();
  2061.  
  2062. for (var i=0; i<results.length; i++)
  2063. {
  2064. resultsWindow.document.getElementById('id-' + i).addEventListener("click", search_func(results[i][0], 'requesterId'), false);
  2065. resultsWindow.document.getElementById('id2-' + i).addEventListener("click", show_requester_func(results[i][0]) , false);
  2066. }
  2067. }
  2068.  
  2069. HITStorage.show_requester = function(results)
  2070. {
  2071. resultsWindow = window.open();
  2072. resultsWindow.document.write('<html><head><title>' + results[0].requesterName + '</title></head><body>\n');
  2073. resultsWindow.document.write('<h1>' + results[0].requesterName + ' (' + results[0].requesterId + ')</h1>\n');
  2074.  
  2075. resultsWindow.document.write('You have submitted ' + results.length + ' HITs for this requester. Earliest ' + results[results.length-1].date +
  2076. ', latest ' + results[0].date);
  2077.  
  2078. resultsWindow.document.write('<p><a href="https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=' + results[0].requesterId + '">' +
  2079. 'Search HITs created by this requester</a></p>');
  2080.  
  2081.  
  2082. resultsWindow.document.write('<p><a href="http://turkopticon.differenceengines.com/' + results[0].requesterId + '">' +
  2083. 'See reviews about this requester on Turkopticon</a> or ');
  2084. resultsWindow.document.write('<a href="' + TO_report_link(results[0].requesterId,results[0].requesterName) + '">' +
  2085. 'review this requester on Turkopticon</a></p>');
  2086.  
  2087. var reward = 0;
  2088. var hits = 0;
  2089. var sum = 0;
  2090. var rejected = 0;
  2091. var approved = 0;
  2092. var pending = 0;
  2093. var all_rejected = 0;
  2094. var all_approved = 0;
  2095. var all_pending = 0;
  2096.  
  2097. resultsWindow.document.write('<table style="border: 1px solid black;border-collapse:collapse;margin-left:10px;margin-right:auto;">\n');
  2098. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>Month' +
  2099. '</th><th>Submitted' +
  2100. '</th><th>Approved' +
  2101. '</th><th>Rejected' +
  2102. '</th><th>Pending' +
  2103. '</th><th>Earnings</th></tr>\n');
  2104.  
  2105. for (var i=0; i<results.length; i++) {
  2106. hits++;
  2107. if (results[i].status == 'Rejected')
  2108. {
  2109. all_rejected++;
  2110. rejected++;
  2111. }
  2112. else if (results[i].status == 'Pending Approval')
  2113. {
  2114. all_pending++;
  2115. pending++;
  2116. }
  2117. else
  2118. {
  2119. all_approved++;
  2120. approved++;
  2121. sum += results[i].reward;
  2122. reward += results[i].reward;
  2123. }
  2124.  
  2125. if (i==results.length-1 || (i<results.length-1 && (results[i].date.substr(0,7) != results[i+1].date.substr(0,7))))
  2126. {
  2127. resultsWindow.document.write('<tr><td style="text-align: right">' + results[i].date.substr(0,7) +
  2128. '</td><td style="text-align: right">' + hits +
  2129. '</td><td style="text-align: right">' + approved +
  2130. '</td><td style="text-align: right">' + rejected +
  2131. '</td><td style="text-align: right">' + pending +
  2132. '</td><td style="text-align: right">$' + reward.toFixed(2) + '</td></tr>\n');
  2133. reward = 0;
  2134. hits = 0;
  2135. approved = 0;
  2136. rejected = 0;
  2137. pending = 0;
  2138. }
  2139. }
  2140. resultsWindow.document.write('<tr style="background-color:lightgrey"><th>' +
  2141. '</th><th style="text-align: right">' + results.length +
  2142. '</th><th style="text-align: right">' + all_approved +
  2143. '</th><th style="text-align: right">' + all_rejected +
  2144. '</th><th style="text-align: right">' + all_pending +
  2145. '</th><th style="text-align: right">$' + sum.toFixed(2) + '</th></tr>\n');
  2146. resultsWindow.document.write('</table>');
  2147.  
  2148. resultsWindow.document.write('<p>Rewards do not include any bonuses</p>');
  2149.  
  2150. resultsWindow.document.write("</body></html>");
  2151. resultsWindow.document.close();
  2152. }
  2153.  
  2154. function TO_report_link(requesterId, requesterName)
  2155. {
  2156. return 'http://turkopticon.differenceengines.com/report?requester[amzn_id]=' + requesterId +
  2157. '&requester[amzn_name]=' + encodeURI(requesterName.trim());
  2158. }
  2159.  
  2160. HITStorage.format_overview_line = function(req, odd, dot_line, top, i)
  2161. {
  2162. var color;
  2163. if (top)
  2164. color = (odd)? 'ffffe0;' : '#eee8aa;';
  2165. else
  2166. color = (odd)? 'white;' : '#f1f3eb;';
  2167. var line = '<tr style="background-color:' + color;
  2168. if (dot_line)
  2169. line += ' border: 0px dotted #000000; border-width: 2px 0px 0px 0px';
  2170. line += '">';
  2171. line += '<td><button type="button" title="Show all HITs" style="height: 16px;font-size: 8px; padding: 0px;" id="id-' +
  2172. i + '">&gt;&gt;</button>' +
  2173. '<button type="button" title="Show details about requester" style="height: 16px;font-size: 8px; padding: 0px;" id="id2-' +
  2174. i + '">+</button> ' + req[0].trim() +
  2175. '</td>';
  2176. line += '<td><a title="Requesters Turkopticon page" target="_blank" href="http://turkopticon.differenceengines.com/' + req[0].trim() + '">[TO]</a> ';
  2177. line += req[1].trim() + '</td>';
  2178. line += '<td style="width: 50px"><a title="Report requester to Turkopticon" target="_blank" href="' + TO_report_link(req[0], req[1]) + '">[report]</a></td>';
  2179. line += '<td style="text-align: right">' + req[2] + '</td>';
  2180. line += '<td style="text-align: right">' + req[5] + '</td>';
  2181. line += '<td style="text-align: right">$' + req[3].toFixed(2) + '</td>';
  2182. var p = (req[4]/req[2]*100).toFixed(1);
  2183. var pc = (p>0)? 'red' : 'green';
  2184. line += '<td style="text-align: right; color:' + pc + ';">' + req[4] + '</td>';
  2185. line += '<td style="text-align: right; color:' + pc + ';">' + p + '%</td>';
  2186. line += '</tr>\n';
  2187. return line;
  2188. }
  2189.  
  2190. HITStorage.format_pending_line = function(req, odd, i)
  2191. {
  2192. console.log(req);
  2193. var color = (odd)? 'white;' : '#f1f3eb;';
  2194. var line = '<tr style="background-color:' + color;
  2195. line += '">';
  2196. line += '<td style="white-space: nowrap; width: 150px; margin-right: 10px;"><button type="button" title="Show all HITs" style="height: 16px;font-size: 8px; padding: 0px;" id="id-' +
  2197. i + '">&gt;&gt;&gt;</button>' +
  2198. '<button type="button" title="Show details about requester" style="height: 16px;font-size: 8px; padding: 0px;" id="id2-' +
  2199. i + '">+</button> ' + req[0].trim() + '</td>';
  2200. line += '<td><a title="Requesters Turkopticon page" target="_blank" href="http://turkopticon.differenceengines.com/' + req[0].trim() + '">[TO]</a> ';
  2201. line += req[1].trim() + '</td>';
  2202. line += '<td style="width: 50px"><a title="Report requester to Turkopticon" target="_blank" href="' + TO_report_link(req[0], req[1]) + '">[report]</a></td>';
  2203. line += '<td style="text-align: right">' + req[2] + '</td>';
  2204. line += '<td style="text-align: right">$' + req[3].toFixed(2) + '</td>';
  2205. line += '</tr>\n';
  2206. return line;
  2207. }
  2208.  
  2209. HITStorage.format_status_line = function(d, odd, new_month)
  2210. {
  2211. var color = (odd)? 'white;' : '#f1f3eb;';
  2212. var line = '<tr style="background-color:' + color;
  2213. if (new_month)
  2214. line += ' border: 0px dotted #000000; border-width: 2px 0px 0px 0px">';
  2215. else
  2216. line += '">';
  2217. line += '<td><button type="button" title="Show all HITs" style="height: 16px;font-size: 8px; padding: 0px;" id="' +
  2218. d.date + '">&gt;&gt;&gt;</button> ' + d.date + '</td>';
  2219. line += '<td>' + d.submitted + '</td>';
  2220. line += '<td>' + d.approved + '</td>';
  2221. line += '<td>' + d.rejected + '</td>';
  2222. line += '<td>' + d.pending + '</td>';
  2223. line += '<td>$' + d.earnings.toFixed(2) + '</td>';
  2224. line += '</tr>\n';
  2225. return line;
  2226. }
  2227.  
  2228. HITStorage.show_pending_overview_csv = function(results)
  2229. {
  2230. var csvData = [];
  2231. csvData.push(["requesterId","requesterName","pending","reward","\n"]);
  2232. for (var i=0; i<results.length; i++) {
  2233. csvData.push(HITStorage.format_pending_line_csv(results[i]));
  2234. }
  2235. var blob = new Blob(csvData, {type: "text/csv;charset=utf-8"});
  2236. saveAs(blob, "pending_overview.csv");
  2237. }
  2238.  
  2239. HITStorage.format_pending_line_csv = function(req)
  2240. {
  2241. var line = [];
  2242. line.push(req[0].trim());
  2243. line.push('"' + req[1].trim() + '"');
  2244. line.push(req[2]);
  2245. line.push(req[3].toFixed(2));
  2246. line.push('\n');
  2247. return line;
  2248. }
  2249.  
  2250.  
  2251. HITStorage.show_requester_overview_csv = function(results)
  2252. {
  2253. var csvData = [];
  2254. csvData.push(['requesterId','requesterName','hits','reward','rejected','pending','\n']);
  2255. for (var i=0; i<results.length; i++) {
  2256. csvData.push(HITStorage.format_overview_line_csv(results[i]));
  2257. }
  2258. var blob = new Blob(csvData, {type: "text/csv;charset=utf-8"});
  2259. saveAs(blob, "requester_overview.csv");
  2260. }
  2261.  
  2262. HITStorage.format_overview_line_csv = function(req)
  2263. {
  2264. var line = [];
  2265. line.push(req[0].trim());
  2266. line.push('"' + req[1].trim() + '"');
  2267. line.push(req[2]);
  2268. line.push(req[3].toFixed(2));
  2269. line.push(req[4]);
  2270. line.push(req[5]);
  2271. line.push('\n');
  2272. return line;
  2273. }
  2274.  
  2275. HITStorage.show_status_overview_csv = function(results)
  2276. {
  2277. var csvData = [];
  2278. csvData.push(['Date','Submitted','Approved','Rejected','Pending','Earnings','\n']);
  2279. for (var i=results.length-1; i>=0; i--) {
  2280. csvData.push(HITStorage.format_status_line_csv(results[i]));
  2281. }
  2282. var blob = new Blob(csvData, {type: "text/csv;charset=utf-8"});
  2283. //location.href='data:text/csv;charset=utf8,' + encodeURIComponent(csvData);
  2284. saveAs(blob, "status_overview.csv");
  2285. }
  2286.  
  2287. HITStorage.format_status_line_csv = function(d)
  2288. {
  2289. var line = [];
  2290. line.push('"' + d.date + '"');
  2291. line.push(d.submitted);
  2292. line.push(d.approved);
  2293. line.push(d.rejected);
  2294. line.push(d.pending);
  2295. line.push(d.earnings.toFixed(2));
  2296. line.push('\n');
  2297. return line;
  2298. }
  2299.  
  2300. HITStorage.export_csv = function(results)
  2301. {
  2302. var csvData = [];
  2303. csvData.push(['hitId','date','requesterName','requesterId','title','reward','status','feedback','\n']);
  2304. for (var i=0; i<results.length; i++) {
  2305. csvData.push(HITStorage.format_csv_line(results[i]));
  2306. }
  2307. var blob = new Blob(csvData, {type: "text/csv;charset=utf-8"});
  2308. //location.href='data:text/csv;charset=utf8,' + encodeURIComponent(csvData);
  2309. saveAs(blob, "hit_database.csv");
  2310. }
  2311.  
  2312. HITStorage.format_csv_line = function(hit)
  2313. {
  2314. var line = [];
  2315. line.push('"' + hit.hitId.trim() + '"');
  2316. line.push('"' + hit.date.trim() + '"');
  2317. line.push('"' + hit.requesterName.trim() + '"');
  2318. line.push('"' + hit.requesterId.trim() + '"');
  2319. line.push('"' + hit.title.trim() + '"');
  2320. line.push(hit.reward.toFixed(2));
  2321. line.push('"' + hit.status.trim().replace(/\&nbsp;/g,' ') + '"');
  2322. line.push('"' + hit.feedback.trim() + '"');
  2323. line.push('\n');
  2324. return line;
  2325. }
  2326.  
  2327. HITStorage.do_update = function(days_to_update)
  2328. {
  2329. if (DAYS_TO_FETCH.length<1)
  2330. {
  2331. HITStorage.check_update();
  2332. return;
  2333. }
  2334. HITStorage.update_status_label('Please wait: ' + progress_bar(days_to_update-DAYS_TO_FETCH.length, days_to_update) +
  2335. ' (' + (days_to_update-DAYS_TO_FETCH.length) + '/' + days_to_update + ')', 'red');
  2336.  
  2337. var hits = [];
  2338. setTimeout(function(){ HITStorage.getHITData( DAYS_TO_FETCH.shift(), hits, 1, days_to_update); }, 2000);
  2339. }
  2340.  
  2341. HITStorage.update_done = function()
  2342. {
  2343. HITStorage.update_status_label('Script monkeys have updated your local database', 'green');
  2344. setTimeout( function() { HITStorage.update_status_label("Search powered by non-amazonian script monkeys"); }, 5000);
  2345.  
  2346. HITStorage.enable_inputs();
  2347.  
  2348. localStorage['HITDB UPDATED'] = new Date().toString();
  2349.  
  2350. var e = document.getElementById('user_activities.date_column_header.tooltip').parentNode.parentNode.childNodes[2].childNodes[1].childNodes[1];
  2351. if (e != null && e.textContent.trim() == 'Today') {
  2352. var today = e.href.slice(-8);
  2353. today = convert_date(today);
  2354. HITStorage.indexedDB.get_todays_projected_earnings(today);
  2355. }
  2356. HITStorage.indexedDB.get_pending_approvals();
  2357. HITStorage.indexedDB.get_pending_payments();
  2358. }
  2359.  
  2360.  
  2361. HITStorage.update_database = function()
  2362. {
  2363. HITStorage.disable_inputs();
  2364.  
  2365. if (localStorage['HITDB TRY_EXTRA_DAYS'] == 'YES') {
  2366. DAYS_TO_FETCH = HITStorage.getAllAvailableDays(20);
  2367. delete localStorage['HITDB TRY_EXTRA_DAYS'];
  2368. }
  2369. else
  2370. {
  2371. DAYS_TO_FETCH = HITStorage.getAllAvailableDays();
  2372. }
  2373. DAYS_TO_FETCH_CHECK = DAYS_TO_FETCH.slice(0);
  2374.  
  2375. // remove extra days from checklist
  2376. for (var i=0; i<DAYS_TO_FETCH_CHECK.length; i++)
  2377. {
  2378. if (DAYS_TO_FETCH_CHECK[i].submitted == -1) {
  2379. DAYS_TO_FETCH_CHECK = DAYS_TO_FETCH_CHECK.slice(0,i);
  2380. break;
  2381. }
  2382. }
  2383.  
  2384. DAYS_TO_FETCH = DAYS_TO_FETCH_CHECK.slice(0);
  2385. HITStorage.update_status_label('Please wait: script monkeys are preparing to start working', 'red');
  2386. setTimeout(function(){ HITStorage.prepare_update_and_check_pending_payments(); }, 100);
  2387. }
  2388.  
  2389. HITStorage.show_overview = function()
  2390. {
  2391. if (HITStorage.validate_dates() == false)
  2392. return;
  2393. var options = {};
  2394. options.term = document.getElementById('search_term').value;
  2395. options.status = document.getElementById('status_select').value;
  2396. options.donut = document.getElementById('donut_select').value;
  2397. options.from_date = document.getElementById('from_date').value;
  2398. options.to_date = document.getElementById('to_date').value;
  2399. options.export_csv = document.getElementById('export_csv').checked;
  2400.  
  2401. HITStorage.update_status_label('Please wait: script monkeys are picking bananas ?', 'red');
  2402. HITStorage.disable_inputs();
  2403. HITStorage.indexedDB.requesterOverview(options);
  2404. }
  2405.  
  2406. HITStorage.show_pendings = function()
  2407. {
  2408. var options = {};
  2409. options.term = document.getElementById('search_term').value;
  2410. options.status = document.getElementById('status_select').value;
  2411. options.donut = document.getElementById('donut_select').value;
  2412. options.from_date = document.getElementById('from_date').value;
  2413. options.to_date = document.getElementById('to_date').value;
  2414. options.export_csv = document.getElementById('export_csv').checked;
  2415.  
  2416. HITStorage.update_status_label('Please wait: script monkeys are picking bananas ?', 'red');
  2417. HITStorage.disable_inputs();
  2418. HITStorage.indexedDB.pendingOverview(options);
  2419. }
  2420.  
  2421. HITStorage.show_status = function()
  2422. {
  2423. if (HITStorage.validate_dates() == false)
  2424. return;
  2425. var options = {};
  2426. options.term = document.getElementById('search_term').value;
  2427. options.status = document.getElementById('status_select').value;
  2428. options.donut = document.getElementById('donut_select').value;
  2429. options.from_date = document.getElementById('from_date').value;
  2430. options.to_date = document.getElementById('to_date').value;
  2431. options.export_csv = document.getElementById('export_csv').checked;
  2432.  
  2433. HITStorage.update_status_label('Please wait: script monkeys are picking bananas ?', 'red');
  2434. HITStorage.disable_inputs();
  2435. HITStorage.indexedDB.statusOverview(options);
  2436. }
  2437.  
  2438. var IMPORT_DIALOG = null;
  2439.  
  2440. function import_dialog()
  2441. {
  2442. if (IMPORT_DIALOG == null)
  2443. {
  2444. IMPORT_DIALOG = document.createElement('div');
  2445. IMPORT_DIALOG.style.display = 'block';
  2446.  
  2447. IMPORT_DIALOG.style.position = 'fixed';
  2448. IMPORT_DIALOG.style.width = '600px';
  2449. //IMPORT_DIALOG.style.height = '400px';
  2450. IMPORT_DIALOG.style.height = '90%';
  2451. IMPORT_DIALOG.style.left = '50%';
  2452. IMPORT_DIALOG.style.right = '50%';
  2453. IMPORT_DIALOG.style.margin = '-300px 0px 0px -300px';
  2454. //IMPORT_DIALOG.style.top = '400px';
  2455. IMPORT_DIALOG.style.bottom = '10px';
  2456. IMPORT_DIALOG.style.padding = '10px';
  2457. IMPORT_DIALOG.style.border = '2px';
  2458. IMPORT_DIALOG.style.textAlign = 'center';
  2459. IMPORT_DIALOG.style.verticalAlign = 'middle';
  2460. IMPORT_DIALOG.style.borderStyle = 'solid';
  2461. IMPORT_DIALOG.style.borderColor = 'black';
  2462. IMPORT_DIALOG.style.backgroundColor = 'white';
  2463. IMPORT_DIALOG.style.color = 'black';
  2464. IMPORT_DIALOG.style.zIndex = '100';
  2465.  
  2466. var table = document.createElement('table');
  2467. var input = document.createElement('textarea');
  2468. var input2 = document.createElement('input');
  2469. var label = document.createElement('label');
  2470. var label2 = document.createElement('label');
  2471.  
  2472. label.textContent = 'Paste CSV-file in the textarea below.';
  2473. label2.textContent = 'CVS separator: ';
  2474. input.style.width = '100%';
  2475. input.style.height = '90%';
  2476.  
  2477. input2.maxLength = '1';
  2478. input2.size = '1';
  2479. input2.defaultValue = ',';
  2480.  
  2481. var import_button = document.createElement('button');
  2482. import_button.textContent = 'Import HITs';
  2483. import_button.addEventListener("click", import_dialog_close_func(true, input, input2), false);
  2484. import_button.style.margin = '5px';
  2485. var cancel_button = document.createElement('button');
  2486. cancel_button.textContent = 'Cancel';
  2487. cancel_button.addEventListener("click", import_dialog_close_func(false, input, input2), false);
  2488. cancel_button.style.margin = '5px';
  2489.  
  2490. IMPORT_DIALOG.appendChild(label);
  2491. IMPORT_DIALOG.appendChild(document.createElement('br'));
  2492. IMPORT_DIALOG.appendChild(label2);
  2493. IMPORT_DIALOG.appendChild(input2);
  2494. IMPORT_DIALOG.appendChild(document.createElement('br'));
  2495. IMPORT_DIALOG.appendChild(input);
  2496. IMPORT_DIALOG.appendChild(document.createElement('br'));
  2497. IMPORT_DIALOG.appendChild(cancel_button);
  2498. IMPORT_DIALOG.appendChild(import_button);
  2499. document.body.appendChild(IMPORT_DIALOG);
  2500. }
  2501. else
  2502. {
  2503. IMPORT_DIALOG.style.display = 'block';
  2504. }
  2505. }
  2506.  
  2507.  
  2508. /*
  2509. * CSVToArray() function is taken from:
  2510. *
  2511. * Blog Entry:
  2512. * Ask Ben: Parsing CSV Strings With Javascript Exec() Regular Expression Command
  2513. *
  2514. * Author:
  2515. * Ben Nadel / Kinky Solutions
  2516. *
  2517. * Link:
  2518. * http://www.bennadel.com/index.cfm?event=blog.view&id=1504
  2519. *
  2520. * Date Posted:
  2521. * Feb 19, 2009 at 10:03 AM
  2522. */
  2523. // This will parse a delimited string into an array of
  2524. // arrays. The default delimiter is the comma, but this
  2525. // can be overriden in the second argument.
  2526. function CSVToArray( strData, strDelimiter ) {
  2527. // Check to see if the delimiter is defined. If not,
  2528. // then default to comma.
  2529. strDelimiter = (strDelimiter || ",");
  2530.  
  2531. // Create a regular expression to parse the CSV values.
  2532. var objPattern = new RegExp(
  2533. (
  2534. // Delimiters.
  2535. "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
  2536.  
  2537. // Quoted fields.
  2538. "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
  2539.  
  2540. // Standard fields.
  2541. "([^\"\\" + strDelimiter + "\\r\\n]*))"
  2542. ),
  2543. "gi"
  2544. );
  2545.  
  2546. // Create an array to hold our data. Give the array
  2547. // a default empty first row.
  2548. var arrData = [[]];
  2549.  
  2550. // Create an array to hold our individual pattern
  2551. // matching groups.
  2552. var arrMatches = null;
  2553.  
  2554.  
  2555. // Keep looping over the regular expression matches
  2556. // until we can no longer find a match.
  2557. while (arrMatches = objPattern.exec( strData )){
  2558.  
  2559. // Get the delimiter that was found.
  2560. var strMatchedDelimiter = arrMatches[ 1 ];
  2561.  
  2562. // Check to see if the given delimiter has a length
  2563. // (is not the start of string) and if it matches
  2564. // field delimiter. If id does not, then we know
  2565. // that this delimiter is a row delimiter.
  2566. if (
  2567. strMatchedDelimiter.length &&
  2568. (strMatchedDelimiter != strDelimiter)
  2569. ){
  2570.  
  2571. // Since we have reached a new row of data,
  2572. // add an empty row to our data array.
  2573. arrData.push( [] );
  2574.  
  2575. }
  2576.  
  2577.  
  2578. // Now that we have our delimiter out of the way,
  2579. // let's check to see which kind of value we
  2580. // captured (quoted or unquoted).
  2581. if (arrMatches[ 2 ]){
  2582.  
  2583. // We found a quoted value. When we capture
  2584. // this value, unescape any double quotes.
  2585. var strMatchedValue = arrMatches[ 2 ].replace(
  2586. new RegExp( "\"\"", "g" ),
  2587. "\""
  2588. );
  2589.  
  2590. } else {
  2591.  
  2592. // We found a non-quoted value.
  2593. var strMatchedValue = arrMatches[ 3 ];
  2594.  
  2595. }
  2596.  
  2597.  
  2598. // Now that we have our value string, let's add
  2599. // it to the data array.
  2600. arrData[ arrData.length - 1 ].push( strMatchedValue );
  2601. }
  2602.  
  2603. // Return the parsed data.
  2604. return( arrData );
  2605. }
  2606.  
  2607. function import_dialog_close_func(save, input, separator)
  2608. {
  2609. return function()
  2610. {
  2611. if (save == true)
  2612. {
  2613.  
  2614. var lines = [];
  2615. var hits = [];
  2616. var dates = [];
  2617.  
  2618. if (input.value.length > 0)
  2619. lines = CSVToArray(input.value, separator.value);
  2620.  
  2621. var errors = 0;
  2622. for (var i = 0; i<lines.length; i++)
  2623. {
  2624. var error = false;
  2625. try {
  2626. if (lines[i][0] == null || lines[i][0] == 'hitId')
  2627. continue;
  2628.  
  2629. if(lines[i][6] == 'Approved - Pending Payment')
  2630. lines[i][6] = 'Approved&nbsp;- Pending&nbsp;Payment';
  2631.  
  2632. if (lines[i].length != 8)
  2633. error = true;
  2634.  
  2635. var hit = {
  2636. hitId : lines[i][0],
  2637. date : convert_date(lines[i][1]),
  2638. requesterName : lines[i][2],
  2639. //This line was null in the version I was using. I added it in, giving it the proper format.
  2640. //This setting is for the links to contact the requester in the status window
  2641. requesterLink : "https://www.mturk.com/mturk/contact?subject=Regarding+Amazon+Mechanical+Turk+HIT+"+lines[i][0]+"&requesterId="+lines[i][3]+"&requesterName="+lines[i][2].replace(" ","+"),
  2642. requesterId : lines[i][3],
  2643. title : lines[i][4],
  2644. reward : parseFloat(lines[i][5]),
  2645. status : lines[i][6],
  2646. feedback : lines[i][7] || "" // If no feedback, put empty string
  2647. };
  2648. //This status thing is actually for the Hit Status page (daily overview). It was non-existent with the current version, I added the functionality in here.
  2649. //This sets up a simple associative array for the initial "date" entry for the hit stats. See below for implementation
  2650. var status = {
  2651. date : hit.date,
  2652. approved : (hit.status != "Rejected" ? 1 : 0),
  2653. earnings : (hit.status != "Rejected" ? hit.reward : 0),
  2654. pending : (hit.status != "Pending" ? 0 : 1),
  2655. rejected : (hit.status == "Rejected" ? 1 : 0),
  2656. submitted : 1
  2657. };
  2658. } catch(err) { error = true; }
  2659.  
  2660. if (error == false){
  2661. hits.push(hit);
  2662. //Implementation of status stuff. First I see if the object exists in my "dates" array,
  2663. var index = lookup(hit.date, "date", dates);
  2664. if (index != -1){
  2665. //if it does, add each value except date to update it. The values will either be 1 or 0, so just += should give the proper values (and it does based on testing
  2666. for (var key in dates[index]){
  2667. if (key != "date")
  2668. dates[index][key] += status[key];
  2669. }
  2670. }
  2671. else
  2672. dates.push(status); //if the date doesn't exist in the array, add it as an initial object
  2673. }
  2674. else
  2675. errors++;
  2676. }
  2677. if (hits.length < 1)
  2678. {
  2679. alert('No HITs found!');
  2680. return;
  2681. }
  2682. else if (confirm('Found ' + hits.length + ' HITs' + (errors>0? ' and ' + errors + (errors==1? ' error' : ' errors') : '') +
  2683. '.\nDo not reload this page until import is ready.\n' +
  2684. 'Press Ok to start.') == true)
  2685. {
  2686. HITStorage.disable_inputs();
  2687. HITStorage.update_status_label('Please wait: importing HITs', 'red');
  2688. IMPORT_DIALOG.style.display = 'none';
  2689. input.value = '';
  2690. HITStorage.indexedDB.importHITs(hits);
  2691. //You have to call updateHITstats on a date object:
  2692. //object = { date:"yyyy-mm-dd", (approved|pending|rejected):int num(Approved|Pending|Rejected), earnings:float totalEarningsForDay, submitted:int numHitsSubmittedThatDay }
  2693. //Easiest hack to do so, parse over the dates objects I manipulated above, call update hit stats on each of them.
  2694. for (var i = 0; i < dates.length; i++){
  2695. HITStorage.indexedDB.updateHITstats(dates[i]);
  2696. }
  2697. return;
  2698. }
  2699. else { return; }
  2700. }
  2701.  
  2702. IMPORT_DIALOG.style.display = 'none';
  2703. input.value = '';
  2704. };
  2705. }
  2706.  
  2707. //simple lookup function for searching I'm reusing.
  2708. function lookup (needle, key, haystack) {
  2709. for (var i = 0; i < haystack.length; i++){
  2710. if (haystack[i][key] == needle)
  2711. return i;
  2712. }
  2713. return -1;
  2714. }
  2715.  
  2716. function get_requester_id(s) {
  2717. var idx = 12 + s.search('requesterId=');
  2718. return s.substr(idx);
  2719. }
  2720.  
  2721. function show_requester_func(requesterId)
  2722. {
  2723. return function()
  2724. {
  2725. HITStorage.indexedDB.showRequester(requesterId);
  2726. };
  2727. }
  2728.  
  2729. function search_func(key, index, d1, d2)
  2730. {
  2731. d1 = d1 || '';
  2732. d2 = d2 || d1;
  2733. return function()
  2734. {
  2735. HITStorage.indexedDB.getHITs({term: key, index: index, status: '---', from_date: d1, to_date: d2, donut: '', this_day: ''});
  2736. };
  2737. }
  2738.  
  2739. function visible_func(element, visible)
  2740. {
  2741. return function()
  2742. {
  2743. element.style.visibility = (visible)? 'visible' : 'hidden';
  2744. };
  2745. }
  2746.  
  2747. function delete_func()
  2748. {
  2749. return function()
  2750. {
  2751. if (confirm('This will remove your local HIT DataBase!\nContinue?'))
  2752. {
  2753. HITStorage.indexedDB.deleteDB();
  2754. }
  2755. };
  2756. }
  2757.  
  2758. function import_func()
  2759. {
  2760. return function()
  2761. {
  2762. import_dialog();
  2763. };
  2764. }
  2765.  
  2766. function note_func(id, label)
  2767. {
  2768. return function()
  2769. {
  2770. note = prompt('Note for requesterId \'' + id + '\':', label.textContent);
  2771.  
  2772. if (note == null)
  2773. {
  2774. return;
  2775. }
  2776.  
  2777. HITStorage.indexedDB.addNote(id, note);
  2778. label.textContent = note;
  2779.  
  2780. label.style.border = '1px dotted';
  2781. if (note.indexOf('!') >= 0)
  2782. label.style.color = 'red';
  2783. else
  2784. label.style.color = 'black';
  2785. };
  2786. }
  2787.  
  2788. function block_func(requesterId, title, hitElement)
  2789. {
  2790. return function()
  2791. {
  2792. re = prompt('Block HITs from requesterId \'' + requesterId + '\' matching:\n' +
  2793. '(default matches only exactly same HIT title, leave empty to match all HITS)', '^'
  2794. + title.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1") + '$');
  2795.  
  2796. if (re == null)
  2797. {
  2798. return;
  2799. }
  2800. re = new RegExp(re);
  2801.  
  2802. if (!re.test(title)) {
  2803. if (confirm("Your regular expression does not match current HIT title.\nSave it anyway?") == false)
  2804. return;
  2805. }
  2806.  
  2807. HITStorage.indexedDB.addBlock(requesterId, re);
  2808. };
  2809. }
  2810.  
  2811. function unblock_func(requesterId, title)
  2812. {
  2813. return function()
  2814. {
  2815. var unblock = confirm('Unblocking removes all blocks that match this HITs title and requesterId.');
  2816. if (unblock == true)
  2817. {
  2818. HITStorage.indexedDB.removeBlocks(requesterId, title);
  2819. }
  2820. };
  2821. }
  2822.  
  2823. function auto_update_func()
  2824. {
  2825. return function()
  2826. {
  2827. var button = document.getElementById('auto_button');
  2828.  
  2829. if (localStorage['HITDB AUTO UPDATE'] === undefined)
  2830. {
  2831. alert('Enable Hit DataBase Auto Update\nWhen enabled, script will fetch last ' +
  2832. 'statusdetail pages and add them to database when this page is reloaded ' +
  2833. 'and at least two minutes have passed from last update. You still need to ' +
  2834. 'do full update from dashboard every now and then.');
  2835. button.textContent = 'Auto Update is ON';
  2836. button.style.color = 'green';
  2837. localStorage['HITDB AUTO UPDATE'] = 'ON';
  2838. }
  2839. else if (localStorage['HITDB AUTO UPDATE'] == 'ON')
  2840. {
  2841. button.textContent = 'Auto Update is OFF';
  2842. button.style.color = 'red';
  2843. localStorage['HITDB AUTO UPDATE'] = 'OFF';
  2844. }
  2845. else
  2846. {
  2847. button.textContent = 'Auto Update is ON';
  2848. button.style.color = 'green';
  2849. localStorage['HITDB AUTO UPDATE'] = 'ON';
  2850. }
  2851. };
  2852. }
  2853.  
  2854. function set_target_func(date)
  2855. {
  2856. return function()
  2857. {
  2858. var target = localStorage['TODAYS TARGET'];
  2859. if (target === undefined)
  2860. target = '';
  2861. else
  2862. target = parseFloat(localStorage['TODAYS TARGET']).toFixed(2);
  2863. target = prompt('Set your target:', target);
  2864.  
  2865. if (target == null)
  2866. return;
  2867. target = parseFloat(target);
  2868.  
  2869. localStorage['TODAYS TARGET'] = target.toFixed(2);
  2870. if (date != null)
  2871. HITStorage.indexedDB.get_todays_projected_earnings(date);
  2872. };
  2873. }
  2874.  
  2875. function random_face()
  2876. {
  2877. var faces = ['?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?'];
  2878. var n = Math.floor((Math.random()*faces.length));
  2879. return '<span style="color: black; font-weight: normal;" title="Featured non-amazonian script ' + ((n>11) ? '... kitten?': 'monkey') + '">' + faces[n] + '</span>';
  2880. }
  2881.  
  2882. function progress_bar(done, max, full, empty, c1, c2)
  2883. {
  2884. max = (max<1)? 1 : max;
  2885. done = (done<0)? 0 : done;
  2886. done = (done>max)? max : done;
  2887.  
  2888. var bar = '<span style="color: ' + (c1||'green') + '">';
  2889. for (var i=0; i<done; i++)
  2890. {
  2891. bar += full || '■';
  2892. }
  2893. bar += '</span><span style="color: ' + (c2||'black') + '">';
  2894. for (var i=done; i<max; i++)
  2895. {
  2896. bar += empty || '⬜';
  2897. }
  2898. bar += '</span>';
  2899. return bar;
  2900. }
  2901.  
  2902. // convert date to more practical form (MMDDYYYY => YYYY-MM-DD)
  2903. function convert_date(date)
  2904. {
  2905. if (date.indexOf('-') > 0)
  2906. return date;
  2907. var day = date.substr(2,2);
  2908. var month = date.substr(0,2);
  2909. var year = date.substr(4,4);
  2910. return (year + '-' + month + '-' + day);
  2911. }
  2912.  
  2913. // convert date from YYYY-MM-DD to MMDDYYYY if it isn't already
  2914. function convert_iso_date(date)
  2915. {
  2916. if (date.indexOf('-') < 0)
  2917. return date;
  2918. var t = date.split('-');
  2919. return t[1] + t[2] + t[0];
  2920. }
  2921.  
  2922. // Format date for display YYYY-MM-DD, DD/MM/YYYY or DD.MM.YYYY
  2923. function display_date(date, format)
  2924. {
  2925. if (format === undefined || format == null)
  2926. return date;
  2927.  
  2928. var d = date.split('-');
  2929.  
  2930. if (format == 'little')
  2931. {
  2932. return d[2] + '.' + d[1] + '.' + d[0];
  2933. }
  2934. if (format == 'middle')
  2935. {
  2936. return d[1] + '/' + d[2] + '/' + d[0];
  2937. }
  2938. }
  2939.  
  2940. HITStorage.indexedDB.create();
  2941.  
  2942. // Backup plan
  2943. //HITStorage.update_date_format(true);
  2944.  
  2945. if (document.location.href.match('https://www.mturk.com/mturk/dashboard'))
  2946. {
  2947. var footer = document.getElementsByClassName('footer_separator')[0];
  2948. if (footer == null)
  2949. return;
  2950.  
  2951. var extra_table = document.createElement('table');
  2952. extra_table.width = '700';
  2953. extra_table.style.boder = '1px solid black';
  2954. extra_table.align = 'center';
  2955. extra_table.cellSpacing = '0px';
  2956. extra_table.cellPadding = '0px';
  2957. var row1 = document.createElement('tr');
  2958. var row2 = document.createElement('tr');
  2959. var td1 = document.createElement('td');
  2960. var content_td = document.createElement('td');
  2961. var whatsthis = document.createElement('a');
  2962.  
  2963. row1.style.height = '25px';
  2964. td1.setAttribute('class', 'white_text_14_bold');
  2965. td1.style.backgroundColor = '#7fb448';//'#7fb4cf';
  2966. td1.style.paddingLeft = '10px';
  2967. td1.innerHTML = 'HIT DataBase' + random_face() + ' ';
  2968. content_td.setAttribute('class', 'container-content');
  2969.  
  2970. whatsthis.href = 'http://userscripts.org/scripts/show/149548';
  2971. whatsthis.setAttribute('class', 'whatis');
  2972. whatsthis.textContent = '(What\'s this?)';
  2973.  
  2974. extra_table.appendChild(row1);
  2975. row1.appendChild(td1);
  2976. td1.appendChild(whatsthis);
  2977. extra_table.appendChild(row2);
  2978. row2.appendChild(content_td);
  2979. footer.parentNode.insertBefore(extra_table, footer);
  2980.  
  2981. var my_bar = document.createElement('div');
  2982. var search_button = document.createElement('button');
  2983. var status_select = document.createElement('select');
  2984. var label = document.createElement('label');
  2985. var label2 = document.createElement('label');
  2986. var input = document.createElement('input');
  2987. var donut_select = document.createElement('select');
  2988. var csv_label = document.createElement('label');
  2989. var csv = document.createElement('input');
  2990.  
  2991. var update_button = document.createElement('button');
  2992. var delete_button = document.createElement('button');
  2993. var pending_button = document.createElement('button');
  2994. var overview_button = document.createElement('button');
  2995. var import_button = document.createElement('button');
  2996. var status_button = document.createElement('button');
  2997.  
  2998. var from_input = document.createElement('input');
  2999. var to_input = document.createElement('input');
  3000. var date_label1 = document.createElement('label');
  3001. var date_label2 = document.createElement('label');
  3002. date_label1.textContent = 'from date ';
  3003. date_label2.textContent = ' to ';
  3004. from_input.setAttribute('id', "from_date");
  3005. to_input.setAttribute('id', "to_date");
  3006. to_input.setAttribute('maxlength', "10");
  3007. from_input.setAttribute('maxlength', "10");
  3008. to_input.setAttribute('size', "10");
  3009. from_input.setAttribute('size', "10");
  3010. from_input.title = 'Date format YYYY-MM-DD\nOr leave empty.';
  3011. to_input.title = 'Date format YYYY-MM-DD\nOr leave empty.';
  3012.  
  3013. var donut_options = [];
  3014. donut_options[0] = document.createElement("option");
  3015. donut_options[1] = document.createElement("option");
  3016. donut_options[2] = document.createElement("option");
  3017. donut_options[0].text = "---";
  3018. donut_options[1].text = "Donut Chart HITS";
  3019. donut_options[2].text = "Donut Chart REWARDS";
  3020. donut_options[0].value = "---";
  3021. donut_options[1].value = "HITS";
  3022. donut_options[2].value = "REWARDS";
  3023.  
  3024. var status_options = [];
  3025. status_options[0] = document.createElement("option");
  3026. status_options[1] = document.createElement("option");
  3027. status_options[2] = document.createElement("option");
  3028. status_options[3] = document.createElement("option");
  3029. status_options[4] = document.createElement("option");
  3030. status_options[5] = document.createElement("option");
  3031. status_options[0].text = "Pending Approval";
  3032. status_options[0].style.color = "orange";
  3033. status_options[1].text = "Rejected";
  3034. status_options[1].style.color = "red";
  3035. status_options[2].text = "Approved - Pending Payment";
  3036. status_options[2].style.color = "green";
  3037. status_options[3].text = "Paid";
  3038. status_options[3].style.color = "green";
  3039. status_options[4].text = "Paid AND Approved";
  3040. status_options[4].style.color = "green";
  3041. status_options[5].text = "ALL";
  3042. status_options[0].value = "Pending Approval";
  3043. status_options[1].value = "Rejected";
  3044. status_options[2].value = "Approved";
  3045. status_options[3].value = "Paid";
  3046. status_options[4].value = "Paid|Approved";
  3047. status_options[5].value = "---";
  3048.  
  3049. search_button.setAttribute('id', "search_button");
  3050. input.setAttribute('id', "search_term");
  3051. status_select.setAttribute('id', "status_select");
  3052. label.setAttribute('id', "status_label");
  3053. donut_select.setAttribute('id', "donut_select");
  3054. delete_button.setAttribute('id', "delete_button");
  3055. update_button.setAttribute('id', "update_button");
  3056. overview_button.setAttribute('id', "overview_button");
  3057. import_button.setAttribute('id', "import_button");
  3058. pending_button.setAttribute('id', "pending_button");
  3059. status_button.setAttribute('id', "status_button");
  3060.  
  3061. my_bar.style.marginLeft = 'auto';
  3062. my_bar.style.marginRight = 'auto';
  3063. my_bar.style.textAlign = 'center';
  3064. label.style.marginLeft = 'auto';
  3065. label.style.marginRight = 'auto';
  3066. label.style.textAlign = 'center';
  3067.  
  3068. var donut = document.createElement('div');
  3069. donut.setAttribute('id', "container");
  3070. donut.style.display = 'none';
  3071.  
  3072. content_td.appendChild(my_bar);
  3073. my_bar.appendChild(delete_button);
  3074. my_bar.appendChild(import_button);
  3075. my_bar.appendChild(update_button);
  3076. my_bar.appendChild(document.createElement("br"));
  3077. my_bar.appendChild(pending_button);
  3078. my_bar.appendChild(overview_button);
  3079. my_bar.appendChild(status_button);
  3080. my_bar.appendChild(document.createElement("br"));
  3081. my_bar.appendChild(donut_select);
  3082. my_bar.appendChild(status_select);
  3083. my_bar.appendChild(label2);
  3084. my_bar.appendChild(input);
  3085. my_bar.appendChild(search_button);
  3086. my_bar.appendChild(document.createElement("br"));
  3087. my_bar.appendChild(date_label1);
  3088. my_bar.appendChild(from_input);
  3089. my_bar.appendChild(date_label2);
  3090. my_bar.appendChild(to_input);
  3091. my_bar.appendChild(csv_label);
  3092. my_bar.appendChild(csv);
  3093. my_bar.appendChild(document.createElement("br"));
  3094. my_bar.appendChild(label);
  3095. my_bar.appendChild(document.createElement("br"));
  3096. (footer.parentNode).insertBefore(donut, footer);
  3097.  
  3098. my_bar.style.textAlign = "float";
  3099. search_button.textContent = "Search";
  3100. search_button.title = "Search from local HIT database\nYou can set time limits and export as CSV-file";
  3101. label2.textContent = " HITs matching: ";
  3102. input.value = "";
  3103.  
  3104. label.textContent = "Search powered by non-amazonian script monkeys";
  3105.  
  3106. for (var i=0; i<status_options.length; i++)
  3107. status_select.options.add(status_options[i]);
  3108. for (var i=0; i<donut_options.length; i++)
  3109. donut_select.options.add(donut_options[i]);
  3110.  
  3111. update_button.title = "Fetch status pages and copy HITs to local indexed database.\nFirst time may take several minutes!";
  3112. update_button.textContent = "Update database";
  3113. update_button.style.color = 'green';
  3114. update_button.style.margin = '5px 5px 5px 5x';
  3115. delete_button.textContent = "Delete database";
  3116. delete_button.style.color = 'red';
  3117. delete_button.style.margin = '5px 5px 5px 5px';
  3118. delete_button.title = "Delete Local DataBase!";
  3119. import_button.textContent = "Import";
  3120. import_button.style.margin = '5px 5px 5px 5px';
  3121. import_button.title = "Import HIT data from exported CSV-file";
  3122. overview_button.textContent = "Requester Overview";
  3123. overview_button.style.margin = '0px 5px 5px 5px';
  3124. overview_button.title = "Summary of all requesters you have worked for\nYou can set time limit and export as CSV-file";
  3125. pending_button.textContent = "Pending Overview";
  3126. pending_button.style.margin = '0px 5px 5px 5px';
  3127. pending_button.title = "Summary of all pending HITs\nYou can export as CSV-file";
  3128. status_button.textContent = "Daily Overview";
  3129. status_button.style.margin = '0px 5px 5px 5px';
  3130. status_button.title = "Summary of each day you have worked on MTurk\nYou can set time limit and export as CSV-file";
  3131.  
  3132. pending_button.addEventListener("click", HITStorage.show_pendings, false);
  3133. overview_button.addEventListener("click", HITStorage.show_overview, false);
  3134. search_button.addEventListener("click", HITStorage.start_search, false);
  3135. update_button.addEventListener("click", HITStorage.update_database, false);
  3136. delete_button.addEventListener("click", delete_func(), false);
  3137. import_button.addEventListener("click", import_func(), false);
  3138. status_button.addEventListener("click", HITStorage.show_status, false);
  3139.  
  3140. csv_label.textContent = 'export CSV';
  3141. csv_label.title = 'Export results as comma-separated values';
  3142. csv_label.style.verticalAlign = 'middle';
  3143. csv_label.style.marginLeft = '50px';
  3144. csv.title = 'Export results as comma-separated values';
  3145. csv.setAttribute('type', 'checkbox');
  3146. csv.setAttribute('id', 'export_csv');
  3147. csv.style.verticalAlign = 'middle';
  3148.  
  3149. from_input.value = '';
  3150. to_input.value = '';
  3151.  
  3152. var table = document.getElementById('lnk_show_earnings_details');
  3153. if (table != null)
  3154. {
  3155. table = table.parentNode.parentNode.parentNode.parentNode;
  3156. var pending_tr = document.createElement('tr');
  3157. var pending_td1 = document.createElement('td');
  3158. var pending_td2 = document.createElement('td');
  3159. var today_tr = document.createElement('tr');
  3160. var today_td1 = document.createElement('td');
  3161. var today_td2 = document.createElement('td');
  3162.  
  3163. pending_tr.setAttribute('class', 'even');
  3164. pending_td1.setAttribute('class', 'metrics-table-first-value');
  3165. pending_td1.setAttribute('id', 'pending_earnings_header');
  3166. pending_td2.setAttribute('id', 'pending_earnings_value');
  3167. today_tr.setAttribute('class', 'odd');
  3168. today_td1.setAttribute('class', 'metrics-table-first-value');
  3169. today_td1.setAttribute('id', 'projected_earnings_header');
  3170. today_td2.setAttribute('id', 'projected_earnings_value');
  3171.  
  3172. pending_tr.appendChild(pending_td1);
  3173. pending_tr.appendChild(pending_td2);
  3174. today_tr.appendChild(today_td1);
  3175. today_tr.appendChild(today_td2);
  3176. table.appendChild(pending_tr);
  3177. table.appendChild(today_tr);
  3178.  
  3179. pending_td1.style.borderTop = '1px dotted darkgrey';
  3180. pending_td2.style.borderTop = '1px dotted darkgrey';
  3181. today_td1.style.borderBottom = '1px dotted darkgrey';
  3182. today_td2.style.borderBottom = '1px dotted darkgrey';
  3183.  
  3184. today_td1.title = 'This value can be inaccurate if HITDB has not been updated recently';
  3185. pending_td1.title = 'This value can be inaccurate if HITDB has not been updated recently';
  3186.  
  3187. if (localStorage['HITDB UPDATED'] === undefined)
  3188. pending_td1.textContent = 'Pending earnings';
  3189. else
  3190. pending_td1.textContent = 'Pending earnings (HITDB updated: ' + localStorage['HITDB UPDATED'] + ')';
  3191. today_td1.innerHTML = 'Projected earnings for today &nbsp;&nbsp;';
  3192. today_td2.textContent = 'ಠ_ಠ';
  3193. pending_td2.textContent = 'ಠ_ಠ';
  3194.  
  3195.  
  3196. var e = document.getElementById('user_activities.date_column_header.tooltip').parentNode.parentNode.childNodes[2].childNodes[1].childNodes[1];
  3197. var today = null;
  3198. if (e != null && e.textContent.trim() == 'Today') {
  3199. today = convert_date(e.href.slice(-8));
  3200. HITStorage.indexedDB.get_todays_projected_earnings(today);
  3201. }
  3202. HITStorage.indexedDB.get_pending_approvals();
  3203. HITStorage.indexedDB.get_pending_payments();
  3204.  
  3205. var target = document.createElement('span');
  3206. target.setAttribute('id', 'my_target');
  3207. target.textContent = 'click here to set your target';
  3208. target.style.fontSize = 'small';
  3209. target.style.color = 'blue';
  3210. today_td1.appendChild(target);
  3211. target.addEventListener("click", set_target_func(today), false);
  3212. }
  3213. }
  3214. else if (document.location.href.match('https://www.mturk.com/mturk/preview'))
  3215. {
  3216. var table = document.getElementById('requester.tooltip');
  3217. if (table == null)
  3218. return;
  3219. table = table.parentNode.parentNode.parentNode;
  3220. var title = table.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0].textContent.trim();
  3221.  
  3222. var extra_row = document.createElement('tr');
  3223. var td_1 = document.createElement('td');
  3224. var td_2 = document.createElement('td');
  3225.  
  3226. var requesterId = document.getElementsByName('requesterId')[0].value;
  3227. var auto_approve = parseInt(document.getElementsByName('hitAutoAppDelayInSeconds')[0].value);
  3228.  
  3229. var buttons = [];
  3230. var b = ['Requester', 'HIT Title'];
  3231. for (var i=0; i<b.length; i++)
  3232. {
  3233. buttons[i] = document.createElement('button');
  3234. buttons[i].textContent = b[i];
  3235. buttons[i].id = b[i] + 'Button' + i;
  3236. buttons[i].style.fontSize = '10px';
  3237. buttons[i].style.height = '18px';
  3238. buttons[i].style.width = '80px';
  3239. buttons[i].style.border = '1px solid';
  3240. buttons[i].style.paddingLeft = '3px';
  3241. buttons[i].style.paddingRight = '3px';
  3242. buttons[i].style.backgroundColor = 'lightgrey';
  3243. buttons[i].setAttribute('form', 'NOThitForm');
  3244. td_1.appendChild(buttons[i]);
  3245. }
  3246. buttons[0].title = 'Search requester ' + requesterId + ' from HIT database';
  3247. buttons[1].title = 'Search title \'' + title + '\' from HIT database';
  3248.  
  3249. HITStorage.indexedDB.colorRequesterButton(requesterId, buttons[0]);
  3250. HITStorage.indexedDB.colorTitleButton(title, buttons[1]);
  3251. buttons[0].addEventListener("click", search_func(requesterId, 'requesterId'), false);
  3252. buttons[1].addEventListener("click", search_func(title, 'title'), false);
  3253.  
  3254. td_2.innerHTML = '<span class="capsule_field_title">Auto-Approval:</span>&nbsp&nbsp' + HITStorage.formatTime(auto_approve*1000) + '';
  3255. td_1.colSpan = '3';
  3256. td_2.colSpan = '8';
  3257.  
  3258. extra_row.appendChild(td_1);
  3259. extra_row.appendChild(td_2);
  3260. table.appendChild(extra_row);
  3261. }
  3262. else
  3263. {
  3264. for (var item=0; item<10; item++) {
  3265. var tooltip = document.getElementById('requester.tooltip--' + item);
  3266. if (tooltip == null)
  3267. break; // no need to continue
  3268. var titleElement = document.getElementById('capsule' + item + '-0');
  3269. var emptySpace = tooltip.parentNode.parentNode.parentNode.parentNode.parentNode;
  3270.  
  3271. var hitItem = tooltip.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;//.parentNode;
  3272.  
  3273. var requesterLabel = tooltip.parentNode;
  3274. var requesterId = tooltip.parentNode.parentNode.getElementsByTagName('a');
  3275. var title = titleElement.textContent.trim();
  3276.  
  3277. requesterId = get_requester_id(requesterId[1].href);
  3278.  
  3279. var buttons = [];
  3280. var row = document.createElement('tr');
  3281. var div = document.createElement('div');
  3282. emptySpace.appendChild(row);
  3283. row.appendChild(div);
  3284.  
  3285. /* Turkopticon link next to requester name */
  3286. //to_link = document.createElement('a');
  3287. //to_link.textContent = ' TO ';
  3288. //to_link.href = 'http://turkopticon.differenceengines.com/' + requesterId;
  3289. //to_link.target = '_blank';
  3290. //to_link.title = requesterId + ' on Turkopticon';
  3291. //tooltip.parentNode.parentNode.appendChild(to_link);
  3292. /*-----------------------------------------*/
  3293.  
  3294. HITStorage.indexedDB.blockHITS(requesterId, title, hitItem, titleElement);
  3295.  
  3296. var b = ['R', 'T', 'N', 'B'];
  3297. for (var i=0; i<b.length; i++)
  3298. {
  3299. buttons[i] = document.createElement('button');
  3300. buttons[i].textContent = b[i];
  3301. buttons[i].id = b[i] + 'Button' + i;
  3302. buttons[i].style.height = '18px';
  3303. buttons[i].style.fontSize = '10px';
  3304. buttons[i].style.border = '1px solid';
  3305. buttons[i].style.paddingLeft = '3px';
  3306. buttons[i].style.paddingRight = '3px';
  3307. buttons[i].style.backgroundColor = 'lightgrey';
  3308. div.appendChild(buttons[i]);
  3309. }
  3310. buttons[0].title = 'Search requester ' + requesterId + ' from HIT database';
  3311. buttons[1].title = 'Search title \'' + title + '\' from HIT database';
  3312. buttons[2].title = 'Add a requester note';
  3313. buttons[3].title = '"Block" requester';
  3314.  
  3315. var notelabel = document.createElement('label');
  3316. notelabel.textContent = '';
  3317. notelabel.id = b[i] + 'notelabel' + item;
  3318. notelabel.style.height = '18px';
  3319. notelabel.style.fontSize = '10px';
  3320. notelabel.style.marginLeft = '10px';
  3321. notelabel.style.padding = '1px';
  3322. notelabel.style.backgroundColor = 'transparent';
  3323. HITStorage.indexedDB.updateNoteButton(requesterId, notelabel);
  3324. div.appendChild(notelabel);
  3325.  
  3326. HITStorage.indexedDB.colorRequesterButton(requesterId, buttons[0]);
  3327. HITStorage.indexedDB.colorTitleButton(title, buttons[1]);
  3328. buttons[0].addEventListener("click", search_func(requesterId, 'requesterId'), false);
  3329. buttons[1].addEventListener("click", search_func(title, 'title'), false);
  3330. buttons[2].addEventListener("click", note_func(requesterId, notelabel), false);
  3331. buttons[3].addEventListener("click", block_func(requesterId, title, hitItem), false);
  3332.  
  3333. div.style.margin = "0px";
  3334.  
  3335. buttons[2].style.visibility = "hidden"; // "visible"
  3336. buttons[2].parentNode.addEventListener("mouseover", visible_func(buttons[2], true), false);
  3337. buttons[2].parentNode.addEventListener("mouseout", visible_func(buttons[2], false), false);
  3338. buttons[2].addEventListener("mouseout", visible_func(buttons[2], false), false);
  3339. buttons[3].style.visibility = "hidden"; // "visible"
  3340. buttons[3].parentNode.addEventListener("mouseover", visible_func(buttons[3], true), false);
  3341. buttons[3].parentNode.addEventListener("mouseout", visible_func(buttons[3], false), false);
  3342. buttons[3].addEventListener("mouseout", visible_func(buttons[3], false), false);
  3343. }
  3344.  
  3345. var auto_button = document.createElement('button');
  3346. auto_button.setAttribute('id', 'auto_button');
  3347. auto_button.title = 'HIT DataBase Auto Update\nAutomagically update newest HITs to database when reloading this page';
  3348.  
  3349. if (localStorage['HITDB AUTO UPDATE'] === undefined)
  3350. {
  3351. auto_button.textContent = 'Auto update ?';
  3352. auto_button.style.color = 'red';
  3353. }
  3354. else if (localStorage['HITDB AUTO UPDATE'] == 'ON')
  3355. {
  3356. auto_button.textContent = 'Auto Update is ON';
  3357. auto_button.style.color = 'green';
  3358. }
  3359. else
  3360. {
  3361. auto_button.textContent = 'Auto Update is OFF';
  3362. auto_button.style.color = 'red';
  3363. }
  3364.  
  3365. //var element = document.body.childNodes[13].childNodes[3].childNodes[1].childNodes[0].childNodes[5];
  3366. var element = document.getElementsByName("/sort")[0];
  3367. //element.insertBefore(auto_button, element.firstChild);
  3368. element.parentNode.insertBefore(auto_button, element.nextSibling);
  3369. auto_button.addEventListener("click", auto_update_func(), false);
  3370.  
  3371. setTimeout(HITStorage.getLatestHITs, 100);
  3372. }