HIT Database Backup

Does things...

  1. // ==UserScript==
  2. // @name HIT Database Backup
  3. // @namespace https://github.com/Kadauchi
  4. // @version 1.0.2
  5. // @description Does things...
  6. // @author Kadauchi
  7. // @icon http://i.imgur.com/oGRQwPN.png
  8. // @include https://www.mturk.com/hitdb
  9. // ==/UserScript==
  10.  
  11. document.body.innerHTML = `creating hit_database.json.... please wait`;
  12.  
  13. let hitdb;
  14.  
  15. const request = window.indexedDB.open(`HITDB`);
  16.  
  17. request.onsuccess = (event) => {
  18. hitdb = event.target.result;
  19. generateFile();
  20. };
  21. request.onupgradeneeded = (event) => {
  22. document.body.innerHTML = `no hit database found`;
  23. };
  24. request.onerror = (event) => {
  25. document.body.innerHTML = `error: something went wrong`;
  26. };
  27.  
  28. async function generateFile() {
  29. const data = JSON.stringify({
  30. HIT: await getObjectStore(`HIT`),
  31. STATS: await getObjectStore(`STATS`),
  32. NOTES: await getObjectStore(`NOTES`)
  33. });
  34.  
  35. document.body.innerHTML = `creating hit_database.json.... this may take awhile`;
  36.  
  37. const exportFile = document.createElement(`a`);
  38. document.body.appendChild(exportFile);
  39. exportFile.href = window.URL.createObjectURL(new Blob([data], { type: `application/json` }));
  40. exportFile.download = `hit_database.json`;
  41. exportFile.click();
  42.  
  43. document.body.innerHTML = `hit_database.json.... downloaded`;
  44. }
  45.  
  46. function getObjectStore(name) {
  47. return new Promise((resolve) => {
  48. try {
  49. const transaction = hitdb.transaction([name], `readonly`);
  50. const objectStore = transaction.objectStore(name);
  51.  
  52. let cursorCount = 0, cursorAccumulator = [];
  53.  
  54. objectStore.openCursor().onsuccess = (event) => {
  55. document.body.innerHTML = `processing hit database.... ${name} ${++ cursorCount}`;
  56.  
  57. const cursor = event.target.result;
  58.  
  59. if (cursor) {
  60. cursorAccumulator.push(cursor.value);
  61. cursor.continue();
  62. }
  63. else {
  64. resolve(cursorAccumulator);
  65. }
  66. };
  67. }
  68. catch (error) {
  69. resolve();
  70. }
  71. });
  72. }