HIT Database Backup

Does things...

目前为 2018-01-04 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name HIT Database Backup
  3. // @namespace https://github.com/Kadauchi
  4. // @version 1.0.1
  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. exportFile.href = window.URL.createObjectURL(new Blob([data], { type: `application/json` }));
  39. exportFile.download = `hit_database.json`;
  40. exportFile.click();
  41.  
  42. document.body.innerHTML = `hit_database.json.... downloaded`;
  43. }
  44.  
  45. function getObjectStore(name) {
  46. return new Promise((resolve) => {
  47. try {
  48. const transaction = hitdb.transaction([name], `readonly`);
  49. const objectStore = transaction.objectStore(name);
  50.  
  51. let cursorCount = 0, cursorAccumulator = [];
  52.  
  53. objectStore.openCursor().onsuccess = (event) => {
  54. document.body.innerHTML = `processing hit database.... ${name} ${++ cursorCount}`;
  55.  
  56. const cursor = event.target.result;
  57.  
  58. if (cursor) {
  59. cursorAccumulator.push(cursor.value);
  60. cursor.continue();
  61. }
  62. else {
  63. resolve(cursorAccumulator);
  64. }
  65. };
  66. }
  67. catch (error) {
  68. resolve();
  69. }
  70. });
  71. }