MTurk HIT Database Mk.II

Keep track of the HITs you've done (and more!). Cross browser compatible.

当前为 2016-03-23 提交的版本,查看 最新版本

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