MTurk HIT Database Mk.II

Keep track of the HITs you've done (and more!)

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

  1. // ==UserScript==
  2. // @name MTurk HIT Database Mk.II
  3. // @author feihtality
  4. // @namespace https://greasyfork.org/en/users/12709
  5. // @version 0.8.101
  6. // @description Keep track of the HITs you've done (and more!)
  7. // @include /^https://www\.mturk\.com/mturk/(dash|view|sort|find|prev|search|accept|cont).*/
  8. // @exclude https://www.mturk.com/mturk/findhits?*hit_scraper
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /**\
  13. **
  14. ** This is a complete rewrite of the MTurk HIT Database script from the ground up, which
  15. ** eliminates obsolete methods, fixes many bugs, and brings this script up-to-date
  16. ** with the current modern browser environment.
  17. **
  18. \**/
  19.  
  20.  
  21. /*
  22. * TODO
  23. * misc refactoring
  24. * expand hits in daily overview
  25. * rewrite error handling
  26. * tagging (?)
  27. * refine searching via R/T buttons
  28. * import from old csv format (?)
  29. *
  30. */
  31.  
  32.  
  33.  
  34. const DB_VERSION = 2;
  35. const DB_NAME = 'HITDB_TESTING';
  36. const MTURK_BASE = 'https://www.mturk.com/mturk/';
  37. //const TO_BASE = 'http://turkopticon.ucsd.edu/api/multi-attrs.php';
  38.  
  39. // polyfill for chrome until v45(?)
  40. if (!NodeList.prototype[Symbol.iterator]) NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
  41. // format leading zeros
  42. Number.prototype.toPadded = function(length) {
  43. 'use strict';
  44.  
  45. length = length || 2;
  46. return ("0000000"+this).substr(-length);
  47. };
  48. // decimal rounding
  49. Math.decRound = function(v, shift) {
  50. 'use strict';
  51.  
  52. v = Math.round(+(v+"e"+shift));
  53. return +(v+"e"+-shift);
  54. };
  55. Date.prototype.toLocalISOString = function() {
  56. 'use strict';
  57.  
  58. var pad = function(num) { return Number(num).toPadded(); },
  59. offset = pad(Math.floor(this.getTimezoneOffset()/60)) + pad(this.getTimezoneOffset()%60),
  60. timezone = this.getTimezoneOffset() > 0 ? "-" + offset : "+" + offset;
  61. return this.getFullYear() + "-" + pad(this.getMonth()+1) + "-" + pad(this.getDate()) +
  62. "T" + pad(this.getHours()) + ":" + pad(this.getMinutes()) + ":" + pad(this.getSeconds()) + timezone;
  63. };
  64.  
  65. /***********************************************************************************************/
  66.  
  67. // TODO defer qc init to Promise for case in large obj JSON parsing
  68. var qc = {
  69. extraDays: !!localStorage.getItem("hitdb_extraDays") || false,
  70. fetchData: document.location.pathname === "/mturk/dashboard" ? JSON.parse(localStorage.getItem("hitdb_fetchData") || "{}") : null,
  71. seen: {},
  72. aat: ~document.location.pathname.search(/(dash|accept|cont)/) ? JSON.parse(localStorage.getItem("hitdb_autoAppTemp") || "{}") : null,
  73. save: function(key, name, isObj) {
  74. 'use strict';
  75.  
  76. if (isObj)
  77. localStorage.setItem(name, JSON.stringify(this[key]));
  78. else
  79. localStorage.setItem(name, this[key]);
  80. }
  81. },
  82. metrics = {};
  83.  
  84. var HITStorage = { //{{{
  85. data: {},
  86.  
  87. versionChange: function hsversionChange() { //{{{
  88. 'use strict';
  89.  
  90. var db = this.result;
  91. db.onerror = HITStorage.error;
  92. db.onversionchange = function(e) { console.log("detected version change??",console.dir(e)); db.close(); };
  93. this.onsuccess = function() { db.close(); };
  94. var dbo;
  95.  
  96. console.groupCollapsed("HITStorage.versionChange::onupgradeneeded");
  97.  
  98. if (!db.objectStoreNames.contains("HIT")) {
  99. console.log("creating HIT OS");
  100. dbo = db.createObjectStore("HIT", { keyPath: "hitId" });
  101. dbo.createIndex("date", "date", { unique: false });
  102. dbo.createIndex("requesterName", "requesterName", { unique: false});
  103. dbo.createIndex("title", "title", { unique: false });
  104. dbo.createIndex("reward", "reward", { unique: false });
  105. dbo.createIndex("status", "status", { unique: false });
  106. dbo.createIndex("requesterId", "requesterId", { unique: false });
  107.  
  108. localStorage.setItem("hitdb_extraDays", true);
  109. qc.extraDays = true;
  110. }
  111. if (!db.objectStoreNames.contains("STATS")) {
  112. console.log("creating STATS OS");
  113. dbo = db.createObjectStore("STATS", { keyPath: "date" });
  114. }
  115. if (this.transaction.objectStore("STATS").indexNames.length < 5) { // new in v5: schema additions
  116. this.transaction.objectStore("STATS").createIndex("approved", "approved", { unique: false });
  117. this.transaction.objectStore("STATS").createIndex("earnings", "earnings", { unique: false });
  118. this.transaction.objectStore("STATS").createIndex("pending", "pending", { unique: false });
  119. this.transaction.objectStore("STATS").createIndex("rejected", "rejected", { unique: false });
  120. this.transaction.objectStore("STATS").createIndex("submitted", "submitted", { unique: false });
  121. }
  122.  
  123. (function _updateNotes(dbt) { // new in v5: schema change
  124. if (!db.objectStoreNames.contains("NOTES")) {
  125. console.log("creating NOTES OS");
  126. dbo = db.createObjectStore("NOTES", { keyPath: "id", autoIncrement: true });
  127. dbo.createIndex("hitId", "hitId", { unique: false });
  128. dbo.createIndex("requesterId", "requesterId", { unique: false });
  129. dbo.createIndex("tags", "tags", { unique: false, multiEntry: true });
  130. dbo.createIndex("date", "date", { unique: false });
  131. }
  132. if (db.objectStoreNames.contains("NOTES") && dbt.objectStore("NOTES").indexNames.length < 3) {
  133. _mv(db, dbt, "NOTES", "NOTES", _updateNotes);
  134. }
  135. })(this.transaction);
  136.  
  137. if (db.objectStoreNames.contains("BLOCKS")) {
  138. console.log("migrating BLOCKS to NOTES");
  139. var temp = [];
  140. this.transaction.objectStore("BLOCKS").openCursor().onsuccess = function() {
  141. var cursor = this.result;
  142. if (cursor) {
  143. temp.push( {
  144. requesterId: cursor.value.requesterId,
  145. tags: "Blocked",
  146. note: "This requester was blocked under the old HitDB. Blocking has been deprecated and removed "+
  147. "from HIT Databse. All blocks have been converted to a Note."
  148. } );
  149. cursor.continue();
  150. } else {
  151. console.log("deleting blocks");
  152. db.deleteObjectStore("BLOCKS");
  153. for (var entry of temp)
  154. this.transaction.objectStore("NOTES").add(entry);
  155. }
  156. };
  157. }
  158.  
  159. function _mv(db, transaction, source, dest, fn) { //{{{
  160. var _data = [];
  161. transaction.objectStore(source).openCursor().onsuccess = function() {
  162. var cursor = this.result;
  163. if (cursor) {
  164. _data.push(cursor.value);
  165. cursor.continue();
  166. } else {
  167. db.deleteObjectStore(source);
  168. fn(transaction);
  169. if (_data.length)
  170. for (var i=0;i<_data.length;i++)
  171. transaction.objectStore(dest).add(_data[i]);
  172. //console.dir(_data);
  173. }
  174. };
  175. } //}}}
  176.  
  177. console.groupEnd();
  178. }, // }}} versionChange
  179.  
  180. error: function(e) { //{{{
  181. 'use strict';
  182.  
  183. if (e === "DatabaseCreationError") {
  184. var s = document.getElementById("hdbStatusText");
  185. s.style.color = "red";
  186. s.innerHTML = "Something went wrong during database creation!<br>Please refresh the page and try again";
  187. console.log("Writing failed with",e);
  188. return;
  189. }
  190. if (typeof e === "string")
  191. console.log(e);
  192. else
  193. console.log("Encountered",e.target.error.name,"--",e.target.error.message,e);
  194. }, //}}} onerror
  195.  
  196. parseDOM: function(doc) {//{{{
  197. 'use strict';
  198. var statusLabel = document.querySelector("#hdbStatusText");
  199. statusLabel.style.color = "black";
  200.  
  201. var errorCheck = doc.querySelector('td[class="error_title"]');
  202.  
  203. if (doc.title.search(/Status$/) > 0) // status overview
  204. parseStatus();
  205. else if (doc.querySelector('td[colspan="4"]')) // valid status detail, but no data
  206. parseMisc("next");
  207. else if (doc.title.search(/Status Detail/) > 0) // status detail with data
  208. parseDetail();
  209. else if (errorCheck) { // encountered an error page
  210. // hit max request rate
  211. if (~errorCheck.textContent.indexOf("page request rate")) {
  212. var _d = doc.documentURI.match(/\d{8}/)[0],
  213. _p = doc.documentURI.match(/ber=(\d+)/)[1];
  214. metrics.dbupdate.mark("[PRE]"+_d+"p"+_p, "start");
  215. console.log("exceeded max requests; refetching", doc.documentURI);
  216. statusLabel.innerHTML = "Exceeded maximum server requests; retrying "+Utils.ISODate(_d)+" page "+_p+"."+
  217. "<br>Please wait...";
  218. setTimeout(HITStorage.fetch, 550, doc.documentURI);
  219. return;
  220. }
  221. // no more staus details left in range
  222. else if (qc.extraDays)
  223. parseMisc("end");
  224. }
  225. else
  226. throw "ParseError::unhandled document received @"+doc.documentURI;
  227.  
  228.  
  229. function parseStatus() {//{{{
  230. HITStorage.data = { HIT: [], STATS: [] };
  231. qc.seen = {};
  232. ProjectedEarnings.clear();
  233.  
  234. // reload auto-approval data to cover not refreshing the dashboard before running an update
  235. qc.aat = JSON.parse(localStorage.getItem("hitdb_autoAppTemp") || "{}");
  236. qc.aac = JSON.parse(localStorage.getItem("hitdb_autoAppCollection") || "{}");
  237.  
  238. var _pastDataExists = Boolean(Object.keys(qc.fetchData).length);
  239. var raw = {
  240. day: doc.querySelectorAll(".statusDateColumnValue"),
  241. sub: doc.querySelectorAll(".statusSubmittedColumnValue"),
  242. app: doc.querySelectorAll(".statusApprovedColumnValue"),
  243. rej: doc.querySelectorAll(".statusRejectedColumnValue"),
  244. pen: doc.querySelectorAll(".statusPendingColumnValue"),
  245. pay: doc.querySelectorAll(".statusEarningsColumnValue")
  246. };
  247. var timeout = 0;
  248. for (var i=0;i<raw.day.length;i++) {
  249. var d = {};
  250. var _date = raw.day[i].childNodes[1].href.substr(53);
  251. d.date = Utils.ISODate(_date);
  252. d.submitted = +raw.sub[i].textContent;
  253. d.approved = +raw.app[i].textContent;
  254. d.rejected = +raw.rej[i].textContent;
  255. d.pending = +raw.pen[i].textContent;
  256. d.earnings = +raw.pay[i].textContent.substr(1);
  257. HITStorage.data.STATS.push(d);
  258.  
  259. // check whether or not we need to get status detail pages for date, then
  260. // fetch status detail pages per date in range and slightly slow
  261. // down GET requests to avoid making too many in too short an interval
  262. var payload = { encodedDate: _date, pageNumber: 1, sortType: "All" };
  263. if (_pastDataExists) {
  264. // date not in range but is new date (or old date but we need updates)
  265. // lastDate stored in ISO format, fetchData date keys stored in mturk's URI ecnodedDate format
  266. if ( (d.date > qc.fetchData.lastDate) || ~(Object.keys(qc.fetchData).indexOf(_date)) ) {
  267. setTimeout(HITStorage.fetch, timeout, MTURK_BASE+"statusdetail", payload);
  268. timeout += 250;
  269.  
  270. qc.fetchData[_date] = { submitted: d.submitted, pending: d.pending };
  271. }
  272. } else { // get everything
  273. setTimeout(HITStorage.fetch, timeout, MTURK_BASE+"statusdetail", payload);
  274. timeout += 250;
  275.  
  276. qc.fetchData[_date] = { submitted: d.submitted, pending: d.pending };
  277. }
  278. } // for
  279. qc.fetchData.expectedTotal = _calcTotals(qc.fetchData);
  280.  
  281. // try for extra days
  282. if (qc.extraDays === true) {
  283. localStorage.removeItem("hitdb_extraDays");
  284. d = _decDate(HITStorage.data.STATS[HITStorage.data.STATS.length-1].date);
  285. qc.extraDays = d; // repurpose extraDays for QC
  286. payload = { encodedDate: d, pageNumber: 1, sortType: "All" };
  287. console.log("fetchrequest for", d, "sent by parseStatus");
  288. setTimeout(HITStorage.fetch, 1000, MTURK_BASE+"statusdetail", payload);
  289. }
  290. qc.fetchData.lastDate = HITStorage.data.STATS[0].date; // most recent date seen
  291.  
  292. }//}}} parseStatus
  293.  
  294. function parseDetail() {//{{{
  295. var _date = doc.documentURI.replace(/.+(\d{8}).+/, "$1");
  296. var _page = doc.documentURI.replace(/.+ber=(\d+).+/, "$1");
  297.  
  298. metrics.dbupdate.mark("[PRE]"+_date+"p"+_page, "end");
  299. console.log("page:", _page, "date:", _date);
  300. statusLabel.textContent = "Processing "+Utils.ISODate(_date)+" page "+_page;
  301. var raw = {
  302. req: doc.querySelectorAll(".statusdetailRequesterColumnValue"),
  303. title: doc.querySelectorAll(".statusdetailTitleColumnValue"),
  304. pay: doc.querySelectorAll(".statusdetailAmountColumnValue"),
  305. status: doc.querySelectorAll(".statusdetailStatusColumnValue"),
  306. feedback: doc.querySelectorAll(".statusdetailRequesterFeedbackColumnValue")
  307. };
  308.  
  309. for (var i=0;i<raw.req.length;i++) {
  310. var d = {};
  311. d.date = Utils.ISODate(_date);
  312. d.feedback = raw.feedback[i].textContent.trim();
  313. d.hitId = raw.req[i].childNodes[1].href.replace(/.+HIT\+(.+)/, "$1");
  314. d.requesterId = raw.req[i].childNodes[1].href.replace(/.+rId=(.+?)&.+/, "$1");
  315. d.requesterName = raw.req[i].textContent.trim().replace(/\|/g,"");
  316. d.reward = +raw.pay[i].textContent.substr(1);
  317. d.status = raw.status[i].textContent.replace(/\s/g, " "); // replace char160 spaces with char32 spaces
  318. d.title = raw.title[i].textContent.replace(/\|/g, "");
  319.  
  320. // mturk apparently never marks $0.00 HITs as 'Paid' so we fix that
  321. if (!d.reward && ~d.status.search(/approved/i)) d.status = "Paid";
  322. // insert autoApproval times
  323. d.autoAppTime = HITStorage.autoApprovals.getTime(_date,d.hitId);
  324.  
  325. HITStorage.data.HIT.push(d);
  326.  
  327. if (!qc.seen[_date]) qc.seen[_date] = {};
  328. qc.seen[_date] = {
  329. submitted: qc.seen[_date].submitted + 1 || 1,
  330. pending: ~d.status.search(/pending/i) ?
  331. (qc.seen[_date].pending + 1 || 1) : (qc.seen[_date].pending || 0)
  332. };
  333.  
  334. ProjectedEarnings.updateValues(d);
  335. }
  336.  
  337. // additional pages remain; get them
  338. if (doc.querySelector('img[src="/media/right_dbl_arrow.gif"]')) {
  339. var payload = { encodedDate: _date, pageNumber: +_page+1, sortType: "All" };
  340. setTimeout(HITStorage.fetch, 250, MTURK_BASE+"statusdetail", payload);
  341. return;
  342. }
  343.  
  344. if (!qc.extraDays) { // not fetching extra days
  345. //no longer any more useful data here, don't need to keep rechecking this date
  346. if (Utils.ISODate(_date) !== qc.fetchData.lastDate &&
  347. qc.seen[_date].submitted === qc.fetchData[_date].submitted &&
  348. qc.seen[_date].pending === 0) {
  349. console.log("no more pending hits, removing",_date,"from fetchData");
  350. delete qc.fetchData[_date];
  351. localStorage.setItem("hitdb_fetchData", JSON.stringify(qc.fetchData));
  352. HITStorage.autoApprovals.purge(_date);
  353. }
  354. // finished scraping; start writing
  355. console.log("totals", _calcTotals(qc.seen), qc.fetchData.expectedTotal);
  356. statusLabel.textContent += " [ "+_calcTotals(qc.seen)+"/"+ qc.fetchData.expectedTotal+" ]";
  357. if (_calcTotals(qc.seen) === qc.fetchData.expectedTotal) {
  358. statusLabel.textContent = "Writing to database...";
  359. HITStorage.autoApprovals.purge();
  360. HITStorage.write(HITStorage.data, "update");
  361. }
  362. } else if (_date <= qc.extraDays) { // day is older than default range and still fetching extra days
  363. parseMisc("next");
  364. console.log("fetchrequest for", _decDate(Utils.ISODate(_date)));
  365. }
  366. }//}}} parseDetail
  367.  
  368. function parseMisc(type) {//{{{
  369. var _d = doc.documentURI.match(/\d{8}/)[0],
  370. _p = doc.documentURI.match(/ber=(\d+)/)[1];
  371. metrics.dbupdate.mark("[PRE]"+_d+"p"+_p, "end");
  372. var payload = { encodedDate: _decDate(Utils.ISODate(_d)), pageNumber: 1, sortType: "All" };
  373.  
  374. if (type === "next" && +qc.extraDays > 1) {
  375. setTimeout(HITStorage.fetch, 250, MTURK_BASE+"statusdetail", payload);
  376. console.log("going to next page", payload.encodedDate);
  377. } else if (type === "end" && +qc.extraDays > 1) {
  378. statusLabel.textContent = "Writing to database...";
  379. HITStorage.write(HITStorage.data, "update");
  380. } else
  381. throw 'Unhandled case -- "'+type+'" in '+doc.documentURI;
  382. }//}}}
  383.  
  384. function _decDate(date) {//{{{
  385. var y = date.substr(0,4);
  386. var m = date.substr(5,2);
  387. var d = date.substr(8,2);
  388. date = new Date(y,m-1,d-1);
  389. return Number(date.getMonth()+1).toPadded() + Number(date.getDate()).toPadded() + date.getFullYear();
  390. }//}}}
  391.  
  392. function _calcTotals(obj) {//{{{
  393. var sum = 0;
  394. for (var k in obj){
  395. if (obj.hasOwnProperty(k) && !isNaN(+k))
  396. sum += obj[k].submitted;
  397. }
  398. return sum;
  399. }//}}}
  400. },//}}} parseDOM
  401. autoApprovals: {//{{{
  402. getTime : function(date, hitId) {
  403. 'use strict';
  404.  
  405. if (qc.extraDays || (!Object.keys(qc.aac).length && !Object.keys(qc.aat).length)) return "";
  406. var found = false,
  407. filter = function(id) { return id === hitId; },
  408. autoApp = "";
  409.  
  410. if (qc.aac[date]) {
  411. autoApp = qc.aac[date][Object.keys(qc.aac[date]).filter(filter)[0]] || "";
  412. if (autoApp) found = true;
  413. }
  414. if (!found && Object.keys(qc.aat).length) {
  415. for (var key in qc.aat) { if (qc.aat.hasOwnProperty(key)) { // for all dates in aat
  416. var id = Object.keys(qc.aat[key]).filter(filter)[0];
  417. autoApp = qc.aat[key][id] || "";
  418. if (autoApp) {
  419. found = true;
  420. qc.aac[date] = qc.aac[date] || {};
  421. qc.aac[date][id] = qc.aat[key][id]; // move time from temp var to collection var
  422. delete qc.aat[key][id];
  423. qc.save("aat", "hitdb_autoAppTemp", true);
  424. qc.save("aac", "hitdb_autoAppCollection", true);
  425. break;
  426. }
  427. }} // for key (dates)
  428. } // if !found && aat not empty
  429. return autoApp;
  430. },// getTime
  431. purge : function(date) {
  432. 'use strict';
  433. if (date) {
  434. delete qc.aac[date];
  435. qc.save("aac", "hitdb_autoAppCollection", true);
  436. return;
  437. }
  438.  
  439. if (!Object.keys(qc.aat).length) return; // nothing here
  440.  
  441. var pad = function(num) { return Number(num).toPadded(); },
  442. _date = Date.parse(new Date().getFullYear() + "-" + pad(new Date().getMonth()+1) + "-" + pad(new Date().getDate()));
  443.  
  444. for (var key of Object.keys(qc.aat)) {
  445. if (_date - key > 169200000) delete qc.aat[key]; // at least 2 days old, no need to keep it around
  446. }
  447. qc.save("aat", "hitdb_autoAppTemp", true);
  448. } // purge
  449. },//}}} autoApprovals
  450.  
  451. fetch: function(url, payload) { //{{{
  452. 'use strict';
  453.  
  454. //format GET request with query payload
  455. if (payload) {
  456. var args = 0;
  457. url += "?";
  458. for (var k in payload) {
  459. if (payload.hasOwnProperty(k)) {
  460. if (args++) url += "&";
  461. url += k + "=" + payload[k];
  462. }
  463. }
  464. }
  465. // defer XHR to a promise
  466. var fetch = new Promise( function(fulfill, deny) {
  467. var urlreq = new XMLHttpRequest();
  468. urlreq.open("GET", url, true);
  469. urlreq.responseType = "document";
  470. urlreq.send();
  471. urlreq.onload = function() {
  472. if (this.status === 200) {
  473. fulfill(this.response);
  474. } else {
  475. deny("Error ".concat(String(this.status)).concat(": "+this.statusText));
  476. }
  477. };
  478. urlreq.onerror = function() { deny("Error ".concat(String(this.status)).concat(": "+this.statusText)); };
  479. urlreq.ontimeout = function() { deny("Error ".concat(String(this.status)).concat(": "+this.statusText)); };
  480. } );
  481. fetch.then( HITStorage.parseDOM, HITStorage.error );
  482.  
  483. }, //}}} fetch
  484. write: function(input, statusUpdate) { //{{{
  485. 'use strict';
  486.  
  487. if (statusUpdate === "update")
  488. qc.timeoutTimer = setTimeout(HITStorage.error, 5555, "DatabaseCreationError");
  489.  
  490. var dbh = window.indexedDB.open(DB_NAME);
  491. dbh.onerror = HITStorage.error;
  492. dbh.onsuccess = function() { _write(this.result); };
  493.  
  494. var counts = { requests: 0, total: 0 };
  495.  
  496. function _write(db) {
  497. db.onerror = HITStorage.error;
  498. var os = Object.keys(input);
  499.  
  500. var dbt = db.transaction(os, "readwrite");
  501. var dbo = [];
  502. for (var i=0;i<os.length;i++) { // cycle object stores
  503. dbo[i] = dbt.objectStore(os[i]);
  504. for (var k of input[os[i]]) { // cycle entries to put into object stores
  505. if (statusUpdate && ++counts.requests)
  506. dbo[i].put(k).onsuccess = _statusCallback;
  507. else
  508. dbo[i].put(k);
  509. }
  510. }
  511. db.close();
  512. }
  513.  
  514. function _statusCallback() {
  515. if (++counts.total === counts.requests) {
  516. var statusLabel = document.querySelector("#hdbStatusText");
  517. statusLabel.style.color = "green";
  518. statusLabel.textContent = statusUpdate === "update" ? "Update Complete!" :
  519. statusUpdate === "restore" ? "Restoring " + counts.total + " entries... Done!" :
  520. "Done!";
  521. document.querySelector("#hdbProgressBar").style.display = "none";
  522.  
  523. if (statusUpdate === "update") {
  524. clearTimeout(qc.timeoutTimer);
  525. ProjectedEarnings.data.dbUpdated = new Date().toLocalISOString();
  526. ProjectedEarnings.saveState();
  527. ProjectedEarnings.draw(false);
  528.  
  529. metrics.dbupdate.stop();
  530. metrics.dbupdate.report();
  531. }
  532. }
  533. }
  534.  
  535. }, //}}} write
  536.  
  537. recall: function(store, options) {//{{{
  538. 'use strict';
  539.  
  540. var index = options ? (options.index || null) : null,
  541. range = options ? (options.range || null) : null,
  542. dir = options ? (options.dir || "next") : "next",
  543. fs = options ? (options.filter ? options.filter.status !== "*" ? new RegExp(options.filter.status, "i") : false : false) : false,
  544. fq = options ? (options.filter ? options.filter.query !== "*" ? new RegExp(options.filter.query,"i") : false : false) : false,
  545. limit = 0;
  546.  
  547. if (options && options.progress) {
  548. var progressBar = document.querySelector("#hdbProgressBar");
  549. //statusText = document.querySelector("#hdbStatusText");
  550. progressBar.style.display = "block";
  551. }
  552. var sr = new DatabaseResult();
  553. return new Promise( function(resolve) {
  554. window.indexedDB.open(DB_NAME).onsuccess = function() {
  555. var dbo = this.result.transaction(store, "readonly").objectStore(store), dbq = null;
  556. if (index)
  557. dbq = dbo.index(index).openCursor(range, dir);
  558. else
  559. dbq = dbo.openCursor(range, dir);
  560. dbq.onsuccess = function() {
  561. var c = this.result;
  562. if (c) {
  563. if ( (!fs && !fq) || // no query filter and no status filter OR
  564. (fs && !fq && ~c.value.status.search(fs)) || // status match and no query filter OR
  565. (!fs && fq && // query match and no status filter OR
  566. (~c.value.title.search(fq) || ~c.value.requesterName.search(fq) || ~c.value.hitId.search(fq))) ||
  567. (fs && fq && ~c.value.status.search(fs) && // status match and query match
  568. (~c.value.title.search(fq) || ~c.value.requesterName.search(fq) || ~c.value.hitId.search(fq))) )
  569. if (limit++ < 3800) // limit to save memory usage in large databases
  570. sr.include(c.value);
  571. c.continue();
  572. } else
  573. resolve(sr);
  574. };
  575. this.result.close();
  576. };
  577. } ); // promise
  578. },//}}} recall
  579.  
  580. backup: function() {//{{{
  581. 'use strict';
  582.  
  583. var bData = {},
  584. os = ["STATS", "NOTES", "HIT"],
  585. count = 0,
  586. prog = document.querySelector("#hdbProgressBar");
  587.  
  588. prog.style.display = "block";
  589.  
  590. window.indexedDB.open(DB_NAME).onsuccess = function() {
  591. for (var store of os) {
  592. this.result.transaction(os, "readonly").objectStore(store).openCursor().onsuccess = populateBackup;
  593. }
  594. this.result.close();
  595. };
  596. function populateBackup(e) {
  597. var cursor = e.target.result;
  598. if (cursor) {
  599. if (!bData[cursor.source.name]) bData[cursor.source.name] = [];
  600. bData[cursor.source.name].push(cursor.value);
  601. cursor.continue();
  602. } else
  603. if (++count === 3)
  604. finalizeBackup();
  605. }
  606. function finalizeBackup() {
  607. var backupblob = new Blob([JSON.stringify(bData)], {type:"application/json"});
  608. var date = new Date();
  609. var dl = document.createElement("A");
  610. date = date.getFullYear() + Number(date.getMonth()+1).toPadded() + Number(date.getDate()).toPadded();
  611. dl.href = URL.createObjectURL(backupblob);
  612. console.log(dl.href);
  613. dl.download = "hitdb_"+date+".bak";
  614. document.body.appendChild(dl); // FF doesn't support forced events unless element is part of the document
  615. dl.click(); // so we make it so and click,
  616. dl.remove(); // then immediately remove it
  617. prog.style.display = "none";
  618. }
  619.  
  620. }//}}} backup
  621.  
  622. }, //}}} HITStorage
  623.  
  624. Utils = { //{{{
  625. ftime : function(t, noBlanks) {//{{{
  626. 'use strict';
  627.  
  628. if (t === 0) return "0s";
  629. if (!t && noBlanks) return "n/a";
  630. var d = Math.floor(t/86400),
  631. h = Math.floor(t%86400/3600),
  632. m = Math.floor(t%86400%3600/60),
  633. s = t%86400%3600%60;
  634. return ((d>0) ? d+" day"+(d>1 ? "s " : " ") : "") + ((h>0) ? h+"h " : "") + ((m>0) ? m+"m " : "") + ((s>0) ? s+"s" : "");
  635. },//}}}ftime
  636.  
  637. ISODate: function(date) { //{{{ MMDDYYYY <-> YYYY-MM-DD
  638. 'use strict';
  639.  
  640. if (date.length === 10)
  641. return date.substr(5,2)+date.substr(-2)+date.substr(0,4);
  642. else
  643. return date.substr(4)+"-"+date.substr(0,2)+"-"+date.substr(2,2);
  644. }//}}} ISODate
  645.  
  646. }; //}}} Utils
  647.  
  648. // ProjectedEarnings doesn't belong up here, but it needs to be for variable assignment purposes :(
  649. var ProjectedEarnings = {//{{{
  650. data: JSON.parse(localStorage.getItem("hitdb_projectedEarnings") || "{}"),
  651. updateDate: function() {//{{{
  652. 'use strict';
  653.  
  654. var el = document.querySelectorAll(".metrics-table")[5].querySelector(".metrics-table-first-value").children[0],
  655. date = el.href.match(/\d{8}/)[0],
  656. day = el.textContent,
  657. isToday = day === "Today",
  658. _date = new Date(),
  659. pad = function(num) { return Number(num).toPadded(); },
  660. weekEnd = null,
  661. weekStart = null;
  662.  
  663. _date.setDate(_date.getDate() - _date.getDay()); // sunday
  664. weekStart = Date.parse(_date.getFullYear() + "-" + pad(_date.getMonth()+1) + "-" + pad(_date.getDate()));
  665. _date.setDate(_date.getDate() + 7); // next sunday
  666. weekEnd = Date.parse(_date.getFullYear() + "-" + pad(_date.getMonth()+1) + "-" + pad(_date.getDate()-_date.getDay()+7));
  667.  
  668. if (!Object.keys(this.data).length) {
  669. this.data = {
  670. today: date, weekStart: weekStart, weekEnd: weekEnd, day: new Date().getDay(), dbUpdated: "n/a",
  671. pending: 0, earnings: {}, target: { day: 0, week: 0 }
  672. };
  673. }
  674.  
  675. if ( (Date.parse(Utils.ISODate(date)) >= this.data.weekEnd) ||
  676. (!isToday && _date.getDay() < this.data.day) ) { // new week
  677. this.data.earnings = {};
  678. this.data.weekEnd = weekEnd;
  679. this.data.weekStart = weekStart;
  680. }
  681. if (date !== this.data.today || !isToday) { // new day
  682. this.data.today = date === this.data.today ? null : date;
  683. this.data.day = new Date().getDay();
  684. }
  685.  
  686. this.saveState();
  687. },//}}} updateDate
  688. draw: function(init) {//{{{
  689. 'use strict';
  690.  
  691. var parentTable = document.querySelector("#total_earnings_amount").offsetParent,
  692. rowPending = init ? parentTable.insertRow(-1) : parentTable.rows[4],
  693. rowProjectedDay = init ? parentTable.insertRow(-1) : parentTable.rows[5],
  694. rowProjectedWeek = init ? parentTable.insertRow(-1) : parentTable.rows[6],
  695. title = "Click to set/change the target value",
  696. weekTotal = this.getWeekTotal(),
  697. dayTotal = this.data.earnings[this.data.today] || 0;
  698.  
  699. if (init) {
  700. rowPending.insertCell(-1);rowPending.insertCell(-1);rowPending.className = "even";
  701. rowProjectedDay.insertCell(-1);rowProjectedDay.insertCell(-1);rowProjectedDay.className = "odd";
  702. rowProjectedWeek.insertCell(-1);rowProjectedWeek.insertCell(-1);rowProjectedWeek.className = "even";
  703. for (var i=0;i<rowPending.cells.length;i++) rowPending.cells[i].style.borderTop = "dotted 1px black";
  704. rowPending.cells[0].className = "metrics-table-first-value";
  705. rowProjectedDay.cells[0].className = "metrics-table-first-value";
  706. rowProjectedWeek.cells[0].className = "metrics-table-first-value";
  707. rowPending.cells[1].title = "This value includes all earnings that are not yet fully cleared as 'Paid'";
  708. }
  709.  
  710. rowPending.cells[0].innerHTML = 'Pending earnings '+
  711. '<span style="font-family:arial;font-size:10px;" title="Timestamp of last database update">[ ' + this.data.dbUpdated + ' ]</span>';
  712. rowPending.cells[1].textContent = "$"+Number(this.data.pending).toFixed(2);
  713. rowProjectedDay.cells[0].innerHTML = 'Projected earnings for the day<br>'+
  714. '<meter id="projectedDayProgress" style="width:220px;" title="'+title+
  715. '" value="'+dayTotal+'" max="'+this.data.target.day+'"></meter>'+
  716. '<span style="color:blue;font-family:arial;font-size:10px;"> ' + Number(dayTotal-this.data.target.day).toFixed(2) + '</span>';
  717. rowProjectedDay.cells[1].textContent = "$"+Number(dayTotal).toFixed(2);
  718. rowProjectedWeek.cells[0].innerHTML = 'Projected earnings for the week<br>' +
  719. '<meter id="projectedWeekProgress" style="width:220px;" title="'+title+
  720. '" value="'+weekTotal+'" max="'+this.data.target.week+'"></meter>' +
  721. '<span style="color:blue;font-family:arial;font-size:10px;"> ' + Number(weekTotal-this.data.target.week).toFixed(2) + '</span>';
  722. rowProjectedWeek.cells[1].textContent = "$"+Number(weekTotal).toFixed(2);
  723.  
  724. document.querySelector("#projectedDayProgress").onclick = updateTargets.bind(this, "day");
  725. document.querySelector("#projectedWeekProgress").onclick = updateTargets.bind(this, "week");
  726.  
  727. function updateTargets(span, e) {
  728. /*jshint validthis:true*/
  729. var goal = prompt("Set your " + (span === "day" ? "daily" : "weekly") + " target:",
  730. this.data.target[span === "day" ? "day" : "week"]);
  731. if (goal && !isNaN(goal)) {
  732. this.data.target[span === "day" ? "day" : "week"] = goal;
  733. e.target.max = goal;
  734. e.target.nextSibling.textContent = " "+Number((span === "day" ? dayTotal : weekTotal) - goal).toFixed(2);
  735. this.saveState();
  736. }
  737. }
  738. },//}}} draw
  739.  
  740. getWeekTotal: function() {
  741. 'use strict';
  742.  
  743. var totals = 0;
  744. for (var k of Object.keys(this.data.earnings))
  745. totals += this.data.earnings[k];
  746.  
  747. return Math.decRound(totals, 2);
  748. },
  749. saveState: function() {
  750. 'use strict';
  751.  
  752. localStorage.setItem("hitdb_projectedEarnings", JSON.stringify(this.data));
  753. },
  754.  
  755. clear: function() {
  756. 'use strict';
  757.  
  758. this.data.pending = 0;
  759.  
  760. for (var day of Object.keys(this.data.earnings)) {
  761. //
  762. // schema change -- remove if block after update propagation
  763. //
  764. if (isNaN(day)) { delete this.data.earnings[day]; continue; }
  765.  
  766. this.data.earnings[day] = 0;
  767. }
  768. },
  769.  
  770. updateValues: function(obj) {
  771. 'use strict';
  772.  
  773. var vDate = Date.parse(obj.date), iDate = Utils.ISODate(obj.date);
  774.  
  775. if (~obj.status.search(/pending/i)) // sum pending earnings (include approved until fully cleared as paid)
  776. this.data.pending = Math.decRound(obj.reward+this.data.pending, 2);
  777. if (vDate < this.data.weekEnd && vDate >= this.data.weekStart && !~obj.status.search(/rejected/i)){ // sum weekly earnings by day
  778. this.data.earnings[iDate] = Math.decRound(obj.reward+(this.data.earnings[iDate] || 0), 2 );
  779. }
  780. }
  781. };//}}} ProjectedEarnings
  782.  
  783. function DatabaseResult() {//{{{
  784. 'use strict';
  785.  
  786. this.results = [];
  787. this.formatHTML = function(type, simple) {//{{{
  788. simple = simple || false;
  789. var count = 0, htmlTxt = [], entry = null, _trClass = null;
  790.  
  791. if (this.results.length < 1) return "<h2>No entries found matching your query.</h2>";
  792.  
  793. if (type === "daily") {
  794. htmlTxt.push('<thead><tr class="hdbHeaderRow"><th style="background:white;"></th>'+
  795. '<th>Date</th><th>Submitted</th><th>Approved</th><th>Rejected</th><th>Pending</th><th>Earnings</th></tr></thead><tbody>');
  796. var r = _collate(this.results,"date");
  797. for (entry of this.results) {
  798. _trClass = (count++ % 2 === 0) ? 'class="even"' : 'class="odd"';
  799. htmlTxt.push('<tr '+_trClass+' style="text-align:center"><td style="background:white;"></td>'+
  800. '<td>' + entry.date + '</td><td>' + entry.submitted + '</td>' +
  801. '<td>' + entry.approved + '</td><td>' + entry.rejected + '</td><td>' + entry.pending + '</td>' +
  802. '<td>' + Number(entry.earnings).toFixed(2) + '</td></tr>');
  803. }
  804. htmlTxt.push('</tbody><tfoot><tr class="hdbTotalsRow"><td style="text-align:right;">Totals:</td>' +
  805. '<td style="text-align:right;">' + r.totalEntries + ' days</td><td style="text-align:center;">' + r.totalSub + '</td>' +
  806. '<td style="text-align:center;">' + r.totalApp + '</td><td style="text-align:center;">' + r.totalRej + '</td>' +
  807. '<td style="text-align:center;">' + r.totalPen + '</td><td style="text-align:center;">$' +
  808. Number(Math.decRound(r.totalPay,2)).toFixed(2) + '</td></tr></tfoot>');
  809. } else if (type === "pending" || type === "requester") {
  810. htmlTxt.push('<thead><tr data-sort="99999" class="hdbHeaderRow"><th>Requester ID</th>' +
  811. '<th width="500">Requester</th><th>' + (type === "pending" ? 'Pending' : 'HITs') + '</th><th>Rewards</th></tr></thead><tbody>');
  812. r = _collate(this.results,"requesterId");
  813. for (var k in r) {
  814. if (!~k.search(/total/) && r.hasOwnProperty(k)) {
  815. var tr = ['<tr data-hits="'+r[k].length+'"><td>' +
  816. '<span style="cursor:pointer;color:blue;" class="hdbExpandRow" title="Display all pending HITs from this requester">' +
  817. '[+]</span> ' + r[k][0].requesterId + '</td><td>' + r[k][0].requesterName + '</td>' +
  818. '<td style="text-align:center;">' + r[k].length + '</td><td>' + Number(Math.decRound(r[k].pay,2)).toFixed(2) + '</td></tr>'];
  819.  
  820. for (var hit of r[k]) { // hits in range per requester id
  821. tr.push('<tr data-rid="'+r[k][0].requesterId+'" style="color:#c60000;display:none;"><td style="text-align:right">' +
  822. hit.date + '</td><td width="500" colspan="2">[ <span class="helpSpan" title="Auto-approval time">AA: '+
  823. Utils.ftime(hit.autoAppTime, true).trim()+'</span> ] '+
  824. hit.title + '</td><td style="text-align:right">' + _parseRewards(hit.reward,"pay") + '</td></tr>');
  825. }
  826. htmlTxt.push(tr.join(''));
  827. }
  828. }
  829. htmlTxt.sort(function(a,b) { return +b.substr(15,5).match(/\d+/) - +a.substr(15,5).match(/\d+/); });
  830. htmlTxt.push('</tbody><tfoot><tr class="hdbTotalsRow"><td style="text-align:right;">Totals:</td>' +
  831. '<td style="text-align:center;">' + (Object.keys(r).length-7) + ' Requesters</td>' +
  832. '<td style="text-align:right;">' + r.totalEntries + '</td>'+
  833. '<td style="text-align:right;">$' + Number(Math.decRound(r.totalPay,2)).toFixed(2) + '</td></tr></tfoot>');
  834. } else { // default
  835. if (!simple)
  836. htmlTxt.push('<thead><tr class="hdbHeaderRow"><th colspan="3"></th>' +
  837. '<th colspan="2" title="Bonuses must be added in manually.\n\nClick inside' +
  838. 'the cell to edit, click out of the cell to save">Reward</th><th colspan="3"></th></tr>'+
  839. '<tr class="hdbHeaderRow">' +
  840. '<th>Date</th><th>Requester</th><th>HIT title</th><th style="font-size:10px;">Pay</th>'+
  841. '<th style="font-size:10px;"><span class="helpSpan" title="Click the cell to edit.\nIts value is automatically saved">'+
  842. 'Bonus</span></th><th>Status</th><th>'+
  843. '<span class="helpSpan" title="Auto-approval times">AA</span></th><th>Feedback</th></tr></thead><tbody>');
  844.  
  845. for (entry of this.results) {
  846. _trClass = (count++ % 2 === 0) ? 'class="even"' : 'class="odd"';
  847. var _stColor = ~entry.status.search(/(paid|approved)/i) ? "green" :
  848. entry.status === "Pending Approval" ? "orange" : "red";
  849. var href = MTURK_BASE+'contact?requesterId='+entry.requesterId+'&requesterName='+entry.requesterName+
  850. '&subject=Regarding+Amazon+Mechanical+Turk+HIT+'+entry.hitId;
  851.  
  852. if (!simple)
  853. htmlTxt.push('<tr '+_trClass+' data-id="'+entry.hitId+'">'+
  854. '<td width="74px">' + entry.date + '</td><td style="max-width:145px;">' +
  855. '<a target="_blank" title="Contact this requester" href="'+href+'">' + entry.requesterName + '</a></td>' +
  856. '<td width="375px" title="HIT ID: '+entry.hitId+'">' +
  857. '<span title="Add a note" id="note-'+entry.hitId+'" style="cursor:pointer;">&nbsp;&#128221;&nbsp;</span>' +
  858. entry.title + '</td><td style="text-align:right">' + _parseRewards(entry.reward,"pay") + '</td>' +
  859. '<td style="text-align:right" class="bonusCell" title="Click to add/edit" contenteditable="true" data-hitid="'+entry.hitId+'">' +
  860. (+_parseRewards(entry.reward,"bonus") ? _parseRewards(entry.reward,"bonus") : "") +
  861. '</td><td style="color:'+_stColor+';text-align:center">' + entry.status + '</td>' +
  862. '<td>' + Utils.ftime(entry.autoAppTime) + '</td><td>' + entry.feedback + '</td></tr>');
  863. else
  864. htmlTxt.push('<tr data-rid="'+entry.requesterId+'" style="display:none"><td>'+entry.date+'</td><td>'+entry.title+'</td><td>'+
  865. _parseRewards(entry.reward,"pay") + '</td><td>'+ entry.status+'</td></tr>');
  866. }
  867.  
  868. if (!simple) {
  869. r = _collate(this.results,"requesterId");
  870. htmlTxt.push('</tbody><tfoot><tr class="hdbTotalsRow"><td></td>' +
  871. '<td style="text-align:right">Totals:</td><td style="text-align:center;">' + r.totalEntries + ' HITs</td>' +
  872. '<td style="text-align:right">$' + Number(Math.decRound(r.totalPay,2)).toFixed(2) + '</td>' +
  873. '<td style="text-align:right">$' + Number(Math.decRound(r.totalBonus,2)).toFixed(2) + '</td>' +
  874. '<td colspan="3"></td></tr></tfoot>');
  875. }
  876. }
  877. return htmlTxt.join('');
  878. };//}}} formatHTML
  879. this.formatCSV = function(type) {//{{{
  880. var csvTxt = [], entry = null, delimiter="\t";
  881. if (type === "daily") {
  882. csvTxt.push( ["Date", "Submitted", "Approved", "Rejected", "Pending", "Earnings\n"].join(delimiter) );
  883. for (entry of this.results) {
  884. csvTxt.push( [entry.date, entry.submitted, entry.approved, entry.rejected,
  885. entry.pending, Number(entry.earnings).toFixed(2)+"\n"].join(delimiter) );
  886. }
  887. csvToFile(csvTxt, "hitdb_dailyOverview.csv");
  888. } else if (type === "pending" || type === "requester") {
  889. csvTxt.push( ["RequesterId","Requester", (type === "pending" ? "Pending" : "HITs"), "Rewards\n"].join(delimiter) );
  890. var r = _collate(this.results,"requesterId");
  891. for (var k in r) {
  892. if (!~k.search(/total/) && r.hasOwnProperty(k))
  893. csvTxt.push( [k, r[k][0].requesterName, r[k].length, Number(Math.decRound(r[k].pay,2)).toFixed(2)+"\n"].join(delimiter) );
  894. }
  895. csvToFile(csvTxt, "hitdb_"+type+"Overview.csv");
  896. } else {
  897. csvTxt.push(["Date","Requester","Title","Pay","Bonus","Status","Feedback\n"].join(delimiter));
  898. for (entry of this.results) {
  899. csvTxt.push([entry.date, entry.requesterName, entry.title, Number(_parseRewards(entry.reward,"pay")).toFixed(2),
  900. (+_parseRewards(entry.reward,"bonus") ? Number(_parseRewards(entry.reward,"bonus")).toFixed(2) : ""),
  901. entry.status, entry.feedback+"\n"].join(delimiter));
  902. }
  903. csvToFile(csvTxt, "hitdb_queryResults.csv");
  904. }
  905.  
  906. return "<pre>"+csvTxt.join('')+"</pre>";
  907.  
  908. function csvToFile(csv, filename) {
  909. var blob = new Blob(csv, {type: "text/csv", endings: "native"}),
  910. dl = document.createElement("A");
  911. dl.href = URL.createObjectURL(blob);
  912. dl.download = filename;
  913. document.body.appendChild(dl); // FF doesn't support forced events unless element is part of the document
  914. dl.click(); // so we make it so and click,
  915. dl.remove(); // then immediately remove it
  916. return dl;
  917. }
  918. };//}}} formatCSV
  919. this.include = function(value) {
  920. this.results.push(value);
  921. };
  922.  
  923. function _parseRewards(rewards,value) {
  924. if (!isNaN(rewards)) {
  925. if (value === "pay")
  926. return Number(rewards).toFixed(2);
  927. else
  928. return "0.00";
  929. } else {
  930. if (value === "pay")
  931. return Number(rewards.pay).toFixed(2);
  932. else
  933. return Number(rewards.bonus).toFixed(2);
  934. }
  935. } // _parse
  936. function _collate(data, index) {
  937. var r = {
  938. totalPay: 0, totalBonus: 0, totalEntries: data.length,
  939. totalSub: 0, totalApp: 0, totalRej: 0, totalPen: 0
  940. };
  941. for (var e of data) {
  942. if (!r[e[index]]) { r[e[index]] = []; r[e[index]].pay = 0; }
  943. r[e[index]].push(e);
  944.  
  945. if (index === "date") {
  946. r.totalSub += e.submitted;
  947. r.totalApp += e.approved;
  948. r.totalRej += e.rejected;
  949. r.totalPen += e.pending;
  950. r.totalPay += e.earnings;
  951. } else {
  952. r[e[index]].pay += (+_parseRewards(e.reward,"pay"));
  953. r.totalPay += (+_parseRewards(e.reward,"pay"));
  954. r.totalBonus += (+_parseRewards(e.reward,"bonus"));
  955. }
  956. }
  957. return r;
  958. } // _collate
  959. }//}}} databaseresult
  960.  
  961. /*
  962. *
  963. * Above contains the core functions. Below is the
  964. * main body, interface, and tangential functions.
  965. *
  966. *///{{{
  967. // the Set() constructor is never actually used other than to test for Chrome v38+
  968. if (!("indexedDB" in window && "Set" in window)) alert("HITDB::Your browser is too outdated or otherwise incompatible with this script!");
  969. else {
  970. /*
  971. var tdbh = window.indexedDB.open(DB_NAME);
  972. tdbh.onerror = function(e) { 'use strict'; console.log("[TESTDB]",e.target.error.name+":", e.target.error.message, e); };
  973. tdbh.onsuccess = INFLATEDUMMYVALUES;
  974. tdbh.onupgradeneeded = BLANKSLATE;
  975. var dbh = null;
  976. */
  977. if (document.location.pathname.search(/dashboard/) > 0) {
  978. var dbh = window.indexedDB.open(DB_NAME, DB_VERSION);
  979. dbh.onerror = function(e) { 'use strict'; console.log("[HITDB]",e.target.error.name+":", e.target.error.message, e); };
  980. dbh.onupgradeneeded = HITStorage.versionChange;
  981. dbh.onsuccess = function() { 'use strict'; this.result.close(); };
  982.  
  983. dashboardUI();
  984.  
  985. ProjectedEarnings.updateDate();
  986. ProjectedEarnings.draw(true);
  987. } else {
  988. beenThereDoneThat();
  989. }
  990. }
  991. /*}}}
  992. *
  993. * Above is the main body and core functions. Below
  994. * defines UI layout/appearance and tangential functions.
  995. *
  996. */
  997.  
  998. // {{{ css injection
  999. var css = "<style type='text/css'>" +
  1000. ".hitdbRTButtons {border:1px solid; font-size: 10px; height: 18px; padding-left: 5px; padding-right: 5px; background: pink;}" +
  1001. ".hitdbRTButtons-green {background: lightgreen;}" +
  1002. ".hitdbRTButtons-large {width:80px;}" +
  1003. ".hdbProgressContainer {margin:auto; width:500px; height:6px; position:relative; display:none; border-radius:10px; overflow:hidden; background:#d3d8db;}" +
  1004. ".hdbProgressInner {width:100%; position:absolute; left:0;top:0;bottom:0; animation: kfpin 1.4s infinite; background:" +
  1005. "linear-gradient(262deg, rgba(208,69,247,0), rgba(208,69,247,1), rgba(69,197,247,1), rgba(69,197,247,0)); background-size: 300% 500%;}" +
  1006. ".hdbProgressOuter {width:30%; position:absolute; left:0;top:0;bottom:0; animation: kfpout 2s cubic-bezier(0,0.55,0.2,1) infinite;}" +
  1007. "@keyframes kfpout { 0% {left:-100%;} 70%{left:100%;} 100%{left:100%;} }" +
  1008. "@keyframes kfpin { 0%{background-position: 0% 50%} 50%{background-position: 100% 15%} 100%{background-position:0% 30%} }" +
  1009. ".hdbCalControls {cursor:pointer;} .hdbCalControls:hover {color:c27fcf;}" +
  1010. ".hdbCalCells {background:#f0f6f9; height:19px}" +
  1011. ".hdbCalDays {cursor:pointer; text-align:center;} .hdbCalDays:hover {background:#7fb4cf; color:white;}" +
  1012. ".hdbDayHeader {width:26px; text-align:center; font-weight:bold; font-size:12px; background:#f0f6f9;}" +
  1013. ".hdbCalHeader {background:#7fb4cf; color:white; font-weight:bold; text-align:center; font-size:11px; padding:3px 0px;}" +
  1014. "#hdbCalendarPanel {position:absolute; z-index:10; box-shadow:-2px 3px 5px 0px rgba(0,0,0,0.68);}" +
  1015. ".hdbTotalsRow {background:#CCC; color:#369; font-weight:bold;}" +
  1016. ".hdbHeaderRow {background:#7FB448; font-size:12px; color:white}" +
  1017. ".helpSpan {border-bottom:1px dotted; cursor:help;}" +
  1018. "</style>";
  1019. document.head.innerHTML += css;
  1020. // }}}
  1021.  
  1022. function beenThereDoneThat() {//{{{
  1023. //
  1024. // TODO refine searching
  1025. //
  1026. 'use strict';
  1027.  
  1028. if (~document.location.pathname.search(/(accept|continue)/)) {
  1029. if (!document.querySelector('input[name="hitAutoAppDelayInSeconds"]')) return;
  1030.  
  1031. // capture autoapproval times
  1032. var _aa = document.querySelector('input[name="hitAutoAppDelayInSeconds"]').value,
  1033. _hid = document.querySelectorAll('input[name="hitId"]')[1].value,
  1034. pad = function(num) { return Number(num).toPadded(); },
  1035. _d = Date.parse(new Date().getFullYear() + "-" + pad(new Date().getMonth()+1) + "-" + pad(new Date().getDate()));
  1036.  
  1037. if (!qc.aat[_d]) qc.aat[_d] = {};
  1038. qc.aat[_d][_hid] = _aa;
  1039. qc.save("aat", "hitdb_autoAppTemp", true);
  1040. return;
  1041. }
  1042. var qualNode = document.querySelector('td[colspan="11"]');
  1043. if (qualNode) { // we're on the preview page!
  1044. var requester = document.querySelector('input[name="requesterId"]').value,
  1045. //hitId = document.querySelector('input[name="hitId"]').value,
  1046. autoApproval = document.querySelector('input[name="hitAutoAppDelayInSeconds"]').value,
  1047. hitTitle = document.querySelector('div[style*="ellipsis"]').textContent.trim().replace(/\|/g,""),
  1048. insertionNode = qualNode.parentNode.parentNode;
  1049. var row = document.createElement("TR"), cellL = document.createElement("TD"), cellR = document.createElement("TD");
  1050. var _resultsTable = document.createElement("TABLE");
  1051. _resultsTable.id = "resultsTableFor"+requester;
  1052. insertionNode.parentNode.parentNode.appendChild(_resultsTable);
  1053.  
  1054. cellR.innerHTML = '<span class="capsule_field_title">Auto-Approval:</span>&nbsp;&nbsp;'+Utils.ftime(autoApproval);
  1055. var rbutton = document.createElement("BUTTON");
  1056. rbutton.classList.add("hitdbRTButtons","hitdbRTButtons-large");
  1057. rbutton.textContent = "Requester";
  1058. rbutton.onclick = function(e) {
  1059. e.preventDefault();
  1060. showResults(requester);
  1061. };
  1062. var tbutton = rbutton.cloneNode(false);
  1063. rbutton.dataset.id = requester;
  1064. rbutton.title = "Show HITs completed from this requester";
  1065. tbutton.textContent = "HIT Title";
  1066. tbutton.onclick = function(e) { e.preventDefault(); };
  1067. HITStorage.recall("HIT", {index: "requesterId", range: window.IDBKeyRange.only(requester)})
  1068. .then(processResults.bind(rbutton));
  1069. HITStorage.recall("HIT", {index: "title", range: window.IDBKeyRange.only(hitTitle)})
  1070. .then(processResults.bind(tbutton));
  1071. row.appendChild(cellL);
  1072. row.appendChild(cellR);
  1073. cellL.appendChild(rbutton);
  1074. cellL.appendChild(tbutton);
  1075. cellL.colSpan = "3";
  1076. cellR.colSpan = "8";
  1077. insertionNode.appendChild(row);
  1078. } else { // browsing HITs n sutff
  1079. var titleNodes = document.querySelectorAll('a[class="capsulelink"]');
  1080. if (titleNodes.length < 1) return; // nothing left to do here!
  1081. var requesterNodes = document.querySelectorAll('a[href*="hitgroups&requester"]');
  1082. var insertionNodes = [];
  1083.  
  1084. for (var i=0;i<titleNodes.length;i++) {
  1085. var _title = titleNodes[i].textContent.trim().replace(/\|/g,"");
  1086. var _tbutton = document.createElement("BUTTON");
  1087. var _id = requesterNodes[i].href.replace(/.+Id=(.+)/, "$1");
  1088. var _rbutton = document.createElement("BUTTON");
  1089. var _div = document.createElement("DIV"), _tr = document.createElement("TR");
  1090. _resultsTable = document.createElement("TABLE");
  1091. insertionNodes.push(requesterNodes[i].parentNode.parentNode.parentNode);
  1092. insertionNodes[i].offsetParent.offsetParent.offsetParent.offsetParent.appendChild(_resultsTable);
  1093. _resultsTable.id = "resultsTableFor"+_id;
  1094.  
  1095. HITStorage.recall("HIT", {index: "title", range: window.IDBKeyRange.only(_title)} )
  1096. .then(processResults.bind(_tbutton));
  1097. HITStorage.recall("HIT", {index: "requesterId", range: window.IDBKeyRange.only(_id)} )
  1098. .then(processResults.bind(_rbutton));
  1099.  
  1100. _tr.appendChild(_div);
  1101. _div.id = "hitdbRTInjection-"+i;
  1102. _div.appendChild(_rbutton);
  1103. _rbutton.textContent = 'R';
  1104. _rbutton.classList.add("hitdbRTButtons");
  1105. _rbutton.dataset.id = _id;
  1106. _rbutton.onclick = showResults.bind(null, _id, null);
  1107. _rbutton.title = "Show HITs completed from this requester";
  1108. _div.appendChild(_tbutton);
  1109. _tbutton.textContent = 'T';
  1110. _tbutton.classList.add("hitdbRTButtons");
  1111. insertionNodes[i].appendChild(_tr);
  1112. }
  1113. } // else
  1114.  
  1115. function showResults(rid, title) {
  1116. console.log(rid, title);
  1117. var el = null;
  1118. if (rid) {
  1119. for (el of document.querySelectorAll('tr[data-rid="'+rid+'"]')) {
  1120. if (el.style.display === "none")
  1121. el.style.display = "table-row";
  1122. else
  1123. el.style.display = "none";
  1124. }
  1125. }
  1126. }
  1127.  
  1128. function processResults(r) {
  1129. /*jshint validthis: true*/
  1130. if (r.results.length) {
  1131. this.classList.add("hitdbRTButtons-green");
  1132. if (this.dataset.id) {
  1133. var rtable = document.querySelector("#resultsTableFor"+this.dataset.id);
  1134. rtable.innerHTML += r.formatHTML(null,true);
  1135. }
  1136. }
  1137. }
  1138.  
  1139. }//}}} btdt
  1140.  
  1141. function dashboardUI() {//{{{
  1142. //
  1143. // TODO refactor
  1144. //
  1145. 'use strict';
  1146.  
  1147. var controlPanel = document.createElement("TABLE");
  1148. var insertionNode = document.querySelector(".footer_separator").previousSibling;
  1149. document.body.insertBefore(controlPanel, insertionNode);
  1150. controlPanel.width = "760";
  1151. controlPanel.align = "center";
  1152. controlPanel.cellSpacing = "0";
  1153. controlPanel.cellPadding = "0";
  1154. controlPanel.innerHTML = '<tr height="25px"><td width="10" bgcolor="#7FB448" style="padding-left: 10px;"></td>' +
  1155. '<td class="white_text_14_bold" style="padding-left:10px; background-color:#7FB448;">' +
  1156. 'HIT Database Mk. II&nbsp;<a href="https://greasyfork.org/en/scripts/11733-mturk-hit-database-mk-ii" class="whatis" target="_blank">' +
  1157. '(What\'s this?)</a></td></tr>' +
  1158. '<tr><td class="container-content" colspan="2">' +
  1159. '<div style="text-align:center;" id="hdbDashboardInterface">' +
  1160. '<button id="hdbBackup" title="Export your entire database!\nPerfect for moving between computers or as a periodic backup">Create Backup</button>' +
  1161. '<button id="hdbRestore" title="Restore database from external backup file" style="margin:5px">Restore</button>' +
  1162. '<button id="hdbUpdate" title="Update... the database" style="color:green;">Update Database</button>' +
  1163. '<div id="hdbFileSelector" style="display:none"><input id="hdbFileInput" type="file" /></div>' +
  1164. '<br>' +
  1165. '<button id="hdbPending" title="Summary of all pending HITs\n Can be exported as CSV" style="margin: 0px 5px 5px;">Pending Overview</button>' +
  1166. '<button id="hdbRequester" title="Summary of all requesters\n Can be exported as CSV" style="margin: 0px 5px 5px;">Requester Overview</button>' +
  1167. '<button id="hdbDaily" title="Summary of each day you\'ve worked\nCan be exported as CSV" style="margin:0px 5px 5px;">Daily Overview</button>' +
  1168. '<br>' +
  1169. '<label>Find </label>' +
  1170. '<select id="hdbStatusSelect"><option value="*">ALL</option>' +
  1171. '<option value="Pending Approval" style="color: orange;">Pending Approval</option>' +
  1172. '<option value="Rejected" style="color: red;">Rejected</option>' +
  1173. '<option value="Approved - Pending Payment" style="color:green;">Approved - Pending Payment</option>' +
  1174. '<option value="(Paid|Approved)" style="color:green;">Paid OR Approved</option></select>' +
  1175. '<label> HITs matching: </label><input id="hdbSearchInput" title="Query can be HIT title, HIT ID, or requester name" />' +
  1176. '<button id="hdbSearch">Search</button>' +
  1177. '<br>' +
  1178. '<label>from date </label><input id="hdbMinDate" maxlength="10" size="10" title="Specify a date, or leave blank">' +
  1179. '<label> to </label><input id="hdbMaxDate" malength="10" size="10" title="Specify a date, or leave blank">' +
  1180. '<label for="hdbCSVInput" title="Export results as CSV file" style="margin-left:50px; vertical-align:middle;">export CSV</label>' +
  1181. '<input id="hdbCSVInput" title="Export results as CSV file" type="checkbox" style="vertical-align:middle;">' +
  1182. '<br>' +
  1183. '<label id="hdbStatusText">placeholder status text</label>' +
  1184. '<div id="hdbProgressBar" class="hdbProgressContainer"><div class="hdbProgressOuter"><div class="hdbProgressInner"></div></div></div>' +
  1185. '</div></td></tr>';
  1186.  
  1187. var updateBtn = document.querySelector("#hdbUpdate"),
  1188. backupBtn = document.querySelector("#hdbBackup"),
  1189. restoreBtn = document.querySelector("#hdbRestore"),
  1190. fileInput = document.querySelector("#hdbFileInput"),
  1191. exportCSVInput = document.querySelector("#hdbCSVInput"),
  1192. searchBtn = document.querySelector("#hdbSearch"),
  1193. searchInput = document.querySelector("#hdbSearchInput"),
  1194. pendingBtn = document.querySelector("#hdbPending"),
  1195. reqBtn = document.querySelector("#hdbRequester"),
  1196. dailyBtn = document.querySelector("#hdbDaily"),
  1197. fromdate = document.querySelector("#hdbMinDate"),
  1198. todate = document.querySelector("#hdbMaxDate"),
  1199. statusSelect = document.querySelector("#hdbStatusSelect"),
  1200. progressBar = document.querySelector("#hdbProgressBar");
  1201.  
  1202. var searchResults = document.createElement("DIV");
  1203. searchResults.align = "center";
  1204. searchResults.id = "hdbSearchResults";
  1205. searchResults.style.display = "block";
  1206. searchResults.innerHTML =
  1207. '<span style="border-bottom:1px solid;color:blue;cursor:pointer;display:none;">[ clear results ]</span><br>' +
  1208. '<table cellSpacing="0" cellpadding="2" id="hdbResultsTable"></table>';
  1209. document.body.insertBefore(searchResults, insertionNode);
  1210.  
  1211. searchResults.firstChild.onclick = function(e) {
  1212. e.target.style.display = "none";
  1213. searchResults.children[2].innerHTML = null;
  1214. };
  1215.  
  1216. updateBtn.onclick = function() {
  1217. progressBar.style.display = "block";
  1218. metrics.dbupdate = new Metrics("database_update");
  1219. HITStorage.fetch(MTURK_BASE+"status");
  1220. document.querySelector("#hdbStatusText").textContent = "fetching status page....";
  1221. };
  1222. exportCSVInput.addEventListener("click", function() {
  1223. if (exportCSVInput.checked) {
  1224. searchBtn.textContent = "Export CSV";
  1225. pendingBtn.textContent += " (csv)";
  1226. reqBtn.textContent += " (csv)";
  1227. dailyBtn.textContent += " (csv)";
  1228. }
  1229. else {
  1230. searchBtn.textContent = "Search";
  1231. pendingBtn.textContent = pendingBtn.textContent.replace(" (csv)","");
  1232. reqBtn.textContent = reqBtn.textContent.replace(" (csv)","");
  1233. dailyBtn.textContent = dailyBtn.textContent.replace(" (csv)", "");
  1234. }
  1235. });
  1236. fromdate.addEventListener("focus", function() {
  1237. var offsets = getPosition(this, true);
  1238. new Calendar(offsets.x, offsets.y, this).drawCalendar();
  1239. });
  1240. todate.addEventListener("focus", function() {
  1241. var offsets = getPosition(this, true);
  1242. new Calendar(offsets.x, offsets.y, this).drawCalendar();
  1243. });
  1244.  
  1245. backupBtn.onclick = HITStorage.backup;
  1246. restoreBtn.onclick = function() { fileInput.click(); };
  1247. fileInput.onchange = processFile;
  1248.  
  1249. searchBtn.onclick = function() {
  1250. var r = getRange();
  1251. var _filter = { status: statusSelect.value, query: searchInput.value.trim().length > 0 ? searchInput.value : "*" };
  1252. var _opt = { index: r.index, range: r.range, dir: r.dir, filter: _filter, progress: true };
  1253.  
  1254. metrics.dbrecall = new Metrics("database_recall::search");
  1255. HITStorage.recall("HIT", _opt).then(function(r) {
  1256. searchResults.children[0].style.display = "initial";
  1257. searchResults.children[2].innerHTML = exportCSVInput.checked ? r.formatCSV() : r.formatHTML();
  1258. autoScroll("#hdbSearchResults");
  1259.  
  1260. for (var _r of r.results) { // retrieve and append notes
  1261. HITStorage.recall("NOTES", { index: "hitId", range: window.IDBKeyRange.only(_r.hitId) }).then(noteHandler.bind(null,"attach"));
  1262. }
  1263.  
  1264. var el = null;
  1265. for (el of document.querySelectorAll(".bonusCell")) {
  1266. el.dataset.initial = el.textContent;
  1267. el.onblur = updateBonus;
  1268. el.onkeydown = updateBonus;
  1269. }
  1270. for (el of document.querySelectorAll('span[id^="note-"]')) {
  1271. el.onclick = noteHandler.bind(null,"new");
  1272. }
  1273. metrics.dbrecall.stop(); metrics.dbrecall.report();
  1274. progressBar.style.display = "none";
  1275. });
  1276. }; // search button click event
  1277. pendingBtn.onclick = function() {
  1278. var r = getRange();
  1279. var _filter = { status: "Approval", query: searchInput.value.trim().length > 0 ? searchInput.value : "*" },
  1280. _opt = { index: r.index, dir: "prev", range: r.range, filter: _filter, progress: true };
  1281.  
  1282. metrics.dbrecall = new Metrics("database_recall::pending");
  1283. HITStorage.recall("HIT", _opt).then(function(r) {
  1284. searchResults.children[0].style.display = "initial";
  1285. searchResults.children[2].innerHTML = exportCSVInput.checked ? r.formatCSV("pending") : r.formatHTML("pending");
  1286. autoScroll("#hdbSearchResults");
  1287. var expands = document.querySelectorAll(".hdbExpandRow");
  1288. for (var el of expands) {
  1289. el.onclick = showHiddenRows;
  1290. }
  1291. metrics.dbrecall.stop(); metrics.dbrecall.report();
  1292. progressBar.style.display = "none";
  1293. });
  1294. }; //pending overview click event
  1295. reqBtn.onclick = function() {
  1296. var r = getRange();
  1297. var _opt = { index: r.index, range: r.range, progress: true };
  1298.  
  1299. metrics.dbrecall = new Metrics("database_recall::requester");
  1300. HITStorage.recall("HIT", _opt).then(function(r) {
  1301. searchResults.children[0].style.display = "initial";
  1302. searchResults.children[2].innerHTML = exportCSVInput.checked ? r.formatCSV("requester") : r.formatHTML("requester");
  1303. autoScroll("#hdbSearchResults");
  1304. var expands = document.querySelectorAll(".hdbExpandRow");
  1305. for (var el of expands) {
  1306. el.onclick = showHiddenRows;
  1307. }
  1308. metrics.dbrecall.stop(); metrics.dbrecall.report();
  1309. progressBar.style.display = "none";
  1310. });
  1311. }; //requester overview click event
  1312. dailyBtn.onclick = function() {
  1313. metrics.dbrecall = new Metrics("database_recall::daily");
  1314. HITStorage.recall("STATS", { dir: "prev" }).then(function(r) {
  1315. searchResults.children[0].style.display = "initial";
  1316. searchResults.children[2].innerHTML = exportCSVInput.checked ? r.formatCSV("daily") : r.formatHTML("daily");
  1317. autoScroll("#hdbSearchResults");
  1318. metrics.dbrecall.stop(); metrics.dbrecall.report();
  1319. });
  1320. }; //daily overview click event
  1321.  
  1322. function getRange() {
  1323. var _min = fromdate.value.length === 10 ? fromdate.value : undefined,
  1324. _max = todate.value.length === 10 ? todate.value : undefined,
  1325. _status = statusSelect.value; // not having this breaks IDBKeyRange on "Approved - Pending Payment" for some reason
  1326. var _range =
  1327. (_min === undefined && _max === undefined) ?
  1328. (_status.length > 1 && !~_status.search(/\(/) ? window.IDBKeyRange.only(_status) : null) :
  1329. (_min === undefined) ? window.IDBKeyRange.upperBound(_max) :
  1330. (_max === undefined) ? window.IDBKeyRange.lowerBound(_min) :
  1331. (_max < _min) ? window.IDBKeyRange.bound(_max,_min) : window.IDBKeyRange.bound(_min,_max),
  1332. _index = _min === undefined && _max === undefined && _status.length > 1 && !~_status.search(/\(/) ? "status" : "date";
  1333. console.log(_index, _status, _range);
  1334. return { min: _min, max: _max, range: _range, dir: _max < _min ? "prev" : "next", index: _index };
  1335. }
  1336. function getPosition(element, includeHeight) {
  1337. var offsets = { x: 0, y: includeHeight ? element.offsetHeight : 0 };
  1338. do {
  1339. offsets.x += element.offsetLeft;
  1340. offsets.y += element.offsetTop;
  1341. element = element.offsetParent;
  1342. } while (element);
  1343. return offsets;
  1344. }
  1345. }//}}} dashboard
  1346.  
  1347. function showHiddenRows(e) {//{{{
  1348. 'use strict';
  1349.  
  1350. var rid = e.target.parentNode.textContent.substr(4);
  1351. var nodes = document.querySelectorAll('tr[data-rid="'+rid+'"]'), el = null;
  1352. if (e.target.textContent === "[+]") {
  1353. for (el of nodes)
  1354. el.style.display="table-row";
  1355. e.target.textContent = "[-]";
  1356. } else {
  1357. for (el of nodes)
  1358. el.style.display="none";
  1359. e.target.textContent = "[+]";
  1360. }
  1361. }//}}}
  1362.  
  1363. function updateBonus(e) {//{{{
  1364. 'use strict';
  1365.  
  1366. if (e instanceof window.KeyboardEvent && e.keyCode === 13) {
  1367. e.target.blur();
  1368. return false;
  1369. } else if (e instanceof window.FocusEvent) {
  1370. var _bonus = +e.target.textContent.replace(/\$/,"");
  1371. if (_bonus !== +e.target.dataset.initial) {
  1372. console.log("updating bonus to",_bonus,"from",e.target.dataset.initial,"("+e.target.dataset.hitid+")");
  1373. e.target.dataset.initial = _bonus;
  1374. var _pay = +e.target.previousSibling.textContent,
  1375. _range = window.IDBKeyRange.only(e.target.dataset.hitid);
  1376.  
  1377. window.indexedDB.open(DB_NAME).onsuccess = function() {
  1378. this.result.transaction("HIT", "readwrite").objectStore("HIT").openCursor(_range).onsuccess = function() {
  1379. var c = this.result;
  1380. if (c) {
  1381. var v = c.value;
  1382. v.reward = { pay: _pay, bonus: _bonus };
  1383. c.update(v);
  1384. }
  1385. }; // idbcursor
  1386. }; // idbopen
  1387. } // bonus is new value
  1388. } // keycode
  1389. } //}}} updateBonus
  1390.  
  1391. function noteHandler(type, e) {//{{{
  1392. //
  1393. // TODO restructure event handling/logic tree
  1394. // combine save and delete; it's ugly :(
  1395. // actually this whole thing is messy and in need of refactoring
  1396. //
  1397. 'use strict';
  1398.  
  1399. if (e instanceof window.KeyboardEvent) {
  1400. if (e.keyCode === 13) {
  1401. e.target.blur();
  1402. return false;
  1403. }
  1404. return;
  1405. }
  1406.  
  1407. if (e instanceof window.FocusEvent) {
  1408. if (e.target.textContent.trim() !== e.target.dataset.initial) {
  1409. if (!e.target.textContent.trim()) { e.target.previousSibling.previousSibling.firstChild.click(); return; }
  1410. var note = e.target.textContent.trim(),
  1411. _range = window.IDBKeyRange.only(e.target.dataset.id),
  1412. inote = e.target.dataset.initial,
  1413. hitId = e.target.dataset.id,
  1414. date = e.target.previousSibling.textContent;
  1415.  
  1416. e.target.dataset.initial = note;
  1417. window.indexedDB.open(DB_NAME).onsuccess = function() {
  1418. this.result.transaction("NOTES", "readwrite").objectStore("NOTES").index("hitId").openCursor(_range).onsuccess = function() {
  1419. if (this.result) {
  1420. var r = this.result.value;
  1421. if (r.note === inote) { // note already exists in database, so we update its value
  1422. r.note = note;
  1423. this.result.update(r);
  1424. return;
  1425. }
  1426. this.result.continue();
  1427. } else {
  1428. if (this.source instanceof window.IDBObjectStore)
  1429. this.source.put({ note:note, date:date, hitId:hitId });
  1430. else
  1431. this.source.objectStore.put({ note:note, date:date, hitId:hitId });
  1432. }
  1433. };
  1434. this.result.close();
  1435. };
  1436. }
  1437. return; // end of save event; no need to proceed
  1438. }
  1439.  
  1440. if (type === "delete") {
  1441. var tr = e.target.parentNode.parentNode,
  1442. noteCell = tr.lastChild;
  1443. _range = window.IDBKeyRange.only(noteCell.dataset.id);
  1444. if (!noteCell.dataset.initial) tr.remove();
  1445. else {
  1446. window.indexedDB.open(DB_NAME).onsuccess = function() {
  1447. this.result.transaction("NOTES", "readwrite").objectStore("NOTES").index("hitId").openCursor(_range).onsuccess = function() {
  1448. if (this.result) {
  1449. if (this.result.value.note === noteCell.dataset.initial) {
  1450. this.result.delete();
  1451. tr.remove();
  1452. return;
  1453. }
  1454. this.result.continue();
  1455. }
  1456. };
  1457. this.result.close();
  1458. };
  1459. }
  1460. return; // end of deletion event; no need to proceed
  1461. } else {
  1462. if (type === "attach" && !e.results.length) return;
  1463.  
  1464. var trow = e instanceof window.MouseEvent ? e.target.parentNode.parentNode : null,
  1465. tbody = trow ? trow.parentNode : null,
  1466. row = document.createElement("TR"),
  1467. c1 = row.insertCell(0),
  1468. c2 = row.insertCell(1),
  1469. c3 = row.insertCell(2);
  1470. date = new Date();
  1471. hitId = e instanceof window.MouseEvent ? e.target.id.substr(5) : null;
  1472.  
  1473. c1.innerHTML = '<span class="removeNote" title="Delete this note" style="cursor:pointer;color:crimson;">[x]</span>';
  1474. c1.firstChild.onclick = noteHandler.bind(null,"delete");
  1475. c1.style.textAlign = "right";
  1476. c2.title = "Date on which the note was added";
  1477. c3.style.color = "crimson";
  1478. c3.colSpan = "5";
  1479. c3.contentEditable = "true";
  1480. c3.onblur = noteHandler.bind(null,"blur");
  1481. c3.onkeydown = noteHandler.bind(null, "kb");
  1482. if (type === "new") {
  1483. row.classList.add(trow.classList);
  1484. tbody.insertBefore(row, trow.nextSibling);
  1485. c2.textContent = date.getFullYear()+"-"+Number(date.getMonth()+1).toPadded()+"-"+Number(date.getDate()).toPadded();
  1486. c3.dataset.initial = "";
  1487. c3.dataset.id = hitId;
  1488. c3.focus();
  1489. return;
  1490. }
  1491.  
  1492. for (var entry of e.results) {
  1493. trow = document.querySelector('tr[data-id="'+entry.hitId+'"]');
  1494. tbody = trow.parentNode;
  1495. row = row.cloneNode(true);
  1496. c1 = row.firstChild;
  1497. c2 = c1.nextSibling;
  1498. c3 = row.lastChild;
  1499. row.classList.add(trow.classList);
  1500. tbody.insertBefore(row, trow.nextSibling);
  1501.  
  1502. c1.firstChild.onclick = noteHandler.bind(null,"delete");
  1503. c2.textContent = entry.date;
  1504. c3.textContent = entry.note;
  1505. c3.dataset.initial = entry.note;
  1506. c3.dataset.id = entry.hitId;
  1507. c3.onblur = noteHandler.bind(null,"blur");
  1508. c3.onkeydown = noteHandler.bind(null, "kb");
  1509. }
  1510. } // new/attach
  1511. }//}}} noteHandler
  1512.  
  1513. function processFile(e) {//{{{
  1514. 'use strict';
  1515.  
  1516. var f = e.target.files;
  1517. if (f.length && f[0].name.search(/\.(bak|csv)$/) && ~f[0].type.search(/(text|json)/)) {
  1518. var reader = new FileReader(), testing = true, isCsv = false;
  1519. reader.readAsText(f[0].slice(0,10));
  1520. reader.onload = function(e) {
  1521. if (testing && e.target.result.search(/(STATS|NOTES|HIT)/) < 0) {
  1522. return error();
  1523. } else if (testing) {
  1524. testing = false;
  1525. document.querySelector("#hdbProgressBar").style.display = "block";
  1526. reader.readAsText(f[0]);
  1527. } else {
  1528. var data = JSON.parse(e.target.result);
  1529. console.log(data);
  1530. HITStorage.write(data, "restore");
  1531. }
  1532. }; // reader.onload
  1533. } else {
  1534. error();
  1535. }
  1536.  
  1537. function error() {
  1538. var s = document.querySelector("#hdbStatusText"),
  1539. e = "Restore::FileReadError : encountered unsupported file";
  1540. s.style.color = "red";
  1541. s.textContent = e;
  1542. throw e;
  1543. }
  1544. }//}}} processFile
  1545.  
  1546. function autoScroll(location, dt) {//{{{
  1547. 'use strict';
  1548.  
  1549. var target = document.querySelector(location).offsetTop,
  1550. pos = window.scrollY,
  1551. dpos = Math.ceil((target - pos)/3);
  1552. dt = dt ? dt-1 : 25; // time step/max recursions
  1553.  
  1554. if (target === pos || dpos === 0 || dt === 0) return;
  1555.  
  1556. window.scrollBy(0, dpos);
  1557. setTimeout(function() { autoScroll(location, dt); }, dt);
  1558. }//}}}
  1559.  
  1560. function Calendar(offsetX, offsetY, caller) {//{{{
  1561. 'use strict';
  1562.  
  1563. this.date = new Date();
  1564. this.offsetX = offsetX;
  1565. this.offsetY = offsetY;
  1566. this.caller = caller;
  1567. this.drawCalendar = function(year,month,day) {//{{{
  1568. year = year || this.date.getFullYear();
  1569. month = month || this.date.getMonth()+1;
  1570. day = day || this.date.getDate();
  1571. var longMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  1572. var date = new Date(year,month-1,day);
  1573. var anchors = _getAnchors(date);
  1574.  
  1575. //make new container if one doesn't already exist
  1576. var container = null;
  1577. if (document.querySelector("#hdbCalendarPanel")) {
  1578. container = document.querySelector("#hdbCalendarPanel");
  1579. container.removeChild( container.getElementsByTagName("TABLE")[0] );
  1580. }
  1581. else {
  1582. container = document.createElement("DIV");
  1583. container.id = "hdbCalendarPanel";
  1584. document.body.appendChild(container);
  1585. }
  1586. container.style.left = this.offsetX;
  1587. container.style.top = this.offsetY;
  1588. var cal = document.createElement("TABLE");
  1589. cal.cellSpacing = "0";
  1590. cal.cellPadding = "0";
  1591. cal.border = "0";
  1592. container.appendChild(cal);
  1593. cal.innerHTML = '<tr>' +
  1594. '<th class="hdbCalHeader hdbCalControls" title="Previous month" style="text-align:right;"><span>&lt;</span></th>' +
  1595. '<th class="hdbCalHeader hdbCalControls" title="Previous year" style="text-align:center;"><span>&#8810;</span></th>' +
  1596. '<th colspan="3" id="hdbCalTableTitle" class="hdbCalHeader">'+date.getFullYear()+'<br>'+longMonths[date.getMonth()]+'</th>' +
  1597. '<th class="hdbCalHeader hdbCalControls" title="Next year" style="text-align:center;"><span>&#8811;</span></th>' +
  1598. '<th class="hdbCalHeader hdbCalControls" title="Next month" style="text-align:left;"><span>&gt;</span></th>' +
  1599. '</tr><tr><th class="hdbDayHeader" style="color:red;">S</th><th class="hdbDayHeader">M</th>' +
  1600. '<th class="hdbDayHeader">T</th><th class="hdbDayHeader">W</th><th class="hdbDayHeader">T</th>' +
  1601. '<th class="hdbDayHeader">F</th><th class="hdbDayHeader">S</th></tr>';
  1602. document.querySelector('th[title="Previous month"]').addEventListener( "click", function() {
  1603. this.drawCalendar(date.getFullYear(), date.getMonth(), 1);
  1604. }.bind(this) );
  1605. document.querySelector('th[title="Previous year"]').addEventListener( "click", function() {
  1606. this.drawCalendar(date.getFullYear()-1, date.getMonth()+1, 1);
  1607. }.bind(this) );
  1608. document.querySelector('th[title="Next month"]').addEventListener( "click", function() {
  1609. this.drawCalendar(date.getFullYear(), date.getMonth()+2, 1);
  1610. }.bind(this) );
  1611. document.querySelector('th[title="Next year"]').addEventListener( "click", function() {
  1612. this.drawCalendar(date.getFullYear()+1, date.getMonth()+1, 1);
  1613. }.bind(this) );
  1614.  
  1615. var hasDay = false, thisDay = 1;
  1616. for (var i=0;i<6;i++) { // cycle weeks
  1617. var row = document.createElement("TR");
  1618. for (var j=0;j<7;j++) { // cycle days
  1619. if (!hasDay && j === anchors.first && thisDay < anchors.total)
  1620. hasDay = true;
  1621. else if (hasDay && thisDay > anchors.total)
  1622. hasDay = false;
  1623.  
  1624. var cell = document.createElement("TD");
  1625. cell.classList.add("hdbCalCells");
  1626. row.appendChild(cell);
  1627. if (hasDay) {
  1628. cell.classList.add("hdbCalDays");
  1629. cell.textContent = thisDay;
  1630. cell.addEventListener("click", _clickHandler.bind(this));
  1631. cell.dataset.year = date.getFullYear();
  1632. cell.dataset.month = date.getMonth()+1;
  1633. cell.dataset.day = thisDay++;
  1634. }
  1635. } // for j
  1636. cal.appendChild(row);
  1637. } // for i
  1638. var controls = cal.insertRow(-1);
  1639. controls.insertCell(0);
  1640. controls.cells[0].colSpan = "7";
  1641. controls.cells[0].classList.add("hdbCalCells");
  1642. controls.cells[0].innerHTML = ' &nbsp; &nbsp; <a href="javascript:void(0)" style="font-weight:bold;text-decoration:none;">Clear</a>' +
  1643. ' &nbsp; <a href="javascript:void(0)" style="font-weight:bold;text-decoration:none;">Close</a>';
  1644. controls.cells[0].children[0].onclick = function() { this.caller.value = ""; }.bind(this);
  1645. controls.cells[0].children[1].onclick = this.die;
  1646.  
  1647. function _clickHandler(e) {
  1648. /*jshint validthis:true*/
  1649.  
  1650. var y = e.target.dataset.year;
  1651. var m = Number(e.target.dataset.month).toPadded();
  1652. var d = Number(e.target.dataset.day).toPadded();
  1653. this.caller.value = y+"-"+m+"-"+d;
  1654. this.die();
  1655. }
  1656.  
  1657. function _getAnchors(date) {
  1658. var _anchors = {};
  1659. date.setMonth(date.getMonth()+1);
  1660. date.setDate(0);
  1661. _anchors.total = date.getDate();
  1662. date.setDate(1);
  1663. _anchors.first = date.getDay();
  1664. return _anchors;
  1665. }
  1666. };//}}} drawCalendar
  1667.  
  1668. this.die = function() { document.getElementById('hdbCalendarPanel').remove(); };
  1669.  
  1670. }//}}} Calendar
  1671.  
  1672. // instance metrics apart from window scoped PerformanceTiming API
  1673. function Metrics(name) {//{{{
  1674. 'use strict';
  1675.  
  1676. this.name = name || "undefined";
  1677. this.marks = {};
  1678. this.start = window.performance.now();
  1679. this.end = null;
  1680. this.stop = function(){
  1681. if (!this.end)
  1682. this.end = window.performance.now();
  1683. else
  1684. throw "Metrics::AccessViolation: end point cannot be overwritten";
  1685. };
  1686. this.mark = function(name,position) {
  1687. if (position === "end" && (!this.marks[name] || this.marks[name].end)) return;
  1688.  
  1689. if (!this.marks[name])
  1690. this.marks[name] = {};
  1691.  
  1692. this.marks[name][position] = window.performance.now();
  1693. };
  1694. this.report = function() {
  1695. console.group("Metrics for",this.name.toUpperCase());
  1696. console.log("Process completed in",+Number((this.end-this.start)/1000).toFixed(3),"seconds");
  1697. for (var k in this.marks) {
  1698. if (this.marks.hasOwnProperty(k)) {
  1699. console.log(k,"occurred after",+Number((this.marks[k].start-this.start)/1000).toFixed(3),"seconds,",
  1700. "resolving in", +Number((this.marks[k].end-this.marks[k].start)/1000).toFixed(3), "seconds");
  1701. }
  1702. }
  1703. console.groupEnd();
  1704. };
  1705. }//}}}
  1706.  
  1707. /*
  1708. *
  1709. *
  1710. * * * * * * * * * * * * * TESTING FUNCTIONS -- DELETE BEFORE FINAL RELEASE * * * * * * * * * * *
  1711. *
  1712. *
  1713. */
  1714.  
  1715. function INFLATEDUMMYVALUES() { //{{{
  1716. 'use strict';
  1717.  
  1718. var tdb = this.result;
  1719. tdb.onerror = function(e) { console.log("requesterror",e.target.error.name,e.target.error.message,e); };
  1720. tdb.onversionchange = function(e) { console.log("tdb received versionchange request", e); tdb.close(); };
  1721. //console.log(tdb.transaction("HIT").objectStore("HIT").indexNames.contains("date"));
  1722. console.groupCollapsed("Populating test database");
  1723. var tdbt = {};
  1724. tdbt.trans = tdb.transaction(["HIT", "NOTES", "BLOCKS"], "readwrite");
  1725. tdbt.hit = tdbt.trans.objectStore("HIT");
  1726. tdbt.notes = tdbt.trans.objectStore("NOTES");
  1727. tdbt.blocks= tdbt.trans.objectStore("BLOCKS");
  1728.  
  1729. var filler = { notes:[], hit:[], blocks:[]};
  1730. for (var n=0;n<100000;n++) {
  1731. filler.hit.push({ date: "2015-08-00", requesterName: "tReq"+(n+1), title: "Greatest Title Ever #"+(n+1),
  1732. reward: Number((n+1)%(200/n)+(((n+1)%200)/100)).toFixed(2), status: "moo",
  1733. requesterId: ("RRRRRRR"+n).substr(-7), hitId: ("HHHHHHH"+n).substr(-7) });
  1734. if (n%1000 === 0) {
  1735. filler.notes.push({ requesterId: ("RRRRRRR"+n).substr(-7), note: n+1 +
  1736. " Proin vel erat commodo mi interdum rhoncus. Sed lobortis porttitor arcu, et tristique ipsum semper a." +
  1737. " Donec eget aliquet lectus, vel scelerisque ligula." });
  1738. filler.blocks.push({requesterId: ("RRRRRRR"+n).substr(-7)});
  1739. }
  1740. }
  1741.  
  1742. _write(tdbt.hit, filler.hit);
  1743. _write(tdbt.notes, filler.notes);
  1744. _write(tdbt.blocks, filler.blocks);
  1745.  
  1746. function _write(store, obj) {
  1747. if (obj.length) {
  1748. var t = obj.pop();
  1749. store.put(t).onsuccess = function() { _write(store, obj) };
  1750. } else {
  1751. console.log("population complete");
  1752. }
  1753. }
  1754.  
  1755. console.groupEnd();
  1756.  
  1757. dbh = window.indexedDB.open(DB_NAME, DB_VERSION);
  1758. dbh.onerror = function(e) { console.log("[HITDB]",e.target.error.name+":", e.target.error.message, e); };
  1759. console.log(dbh.readyState, dbh);
  1760. dbh.onupgradeneeded = HITStorage.versionChange;
  1761. dbh.onblocked = function(e) { console.log("blocked event triggered:", e); };
  1762.  
  1763. tdb.close();
  1764.  
  1765. }//}}}
  1766.  
  1767. function BLANKSLATE() { //{{{ create empty db equivalent to original schema to test upgrade
  1768. 'use strict';
  1769. var tdb = this.result;
  1770. if (!tdb.objectStoreNames.contains("HIT")) {
  1771. console.log("creating HIT OS");
  1772. var dbo = tdb.createObjectStore("HIT", { keyPath: "hitId" });
  1773. dbo.createIndex("date", "date", { unique: false });
  1774. dbo.createIndex("requesterName", "requesterName", { unique: false});
  1775. dbo.createIndex("title", "title", { unique: false });
  1776. dbo.createIndex("reward", "reward", { unique: false });
  1777. dbo.createIndex("status", "status", { unique: false });
  1778. dbo.createIndex("requesterId", "requesterId", { unique: false });
  1779.  
  1780. }
  1781. if (!tdb.objectStoreNames.contains("STATS")) {
  1782. console.log("creating STATS OS");
  1783. dbo = tdb.createObjectStore("STATS", { keyPath: "date" });
  1784. }
  1785. if (!tdb.objectStoreNames.contains("NOTES")) {
  1786. console.log("creating NOTES OS");
  1787. dbo = tdb.createObjectStore("NOTES", { keyPath: "requesterId" });
  1788. }
  1789. if (!tdb.objectStoreNames.contains("BLOCKS")) {
  1790. console.log("creating BLOCKS OS");
  1791. dbo = tdb.createObjectStore("BLOCKS", { keyPath: "id", autoIncrement: true });
  1792. dbo.createIndex("requesterId", "requesterId", { unique: false });
  1793. }
  1794. } //}}}
  1795.  
  1796.  
  1797.  
  1798. // vim: ts=2:sw=2:et:fdm=marker:noai