Advertisement
Guest User

Untitled

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