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.0
  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 = `generating 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. const exportFile = document.createElement(`a`);
  36. exportFile.href = window.URL.createObjectURL(new Blob([data], { type: `application/json` }));
  37. exportFile.download = `hit_database.json`;
  38. exportFile.click();
  39.  
  40. document.body.innerHTML = `hit_database.json.... downloaded`;
  41. }
  42.  
  43. function getObjectStore(name) {
  44. return new Promise((resolve) => {
  45. try {
  46. const transaction = hitdb.transaction([name], `readonly`);
  47. const objectStore = transaction.objectStore(name);
  48.  
  49. objectStore.getAll().onsuccess = (event) => {
  50. resolve(event.target.result);
  51. };
  52. }
  53. catch (error) {
  54. resolve();
  55. }
  56. });
  57. }