MTurk HIT Database Mk.II

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

当前为 2016-05-08 提交的版本,查看 最新版本

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