Import Export Data

Import and export data using localForage in Tampermonkey script

  1. // ==UserScript==
  2. // @name Import Export Data
  3. // @namespace https://github.com/Adachi-Git
  4. // @version 1.01
  5. // @description Import and export data using localForage in Tampermonkey script
  6. // @author Adachi
  7. // @match https://u2.dmhy.org/offers.php
  8. // @match https://u2.dmhy.org/torrents.php*
  9. // @include /^https?://(bangumi\.tv|bgm\.tv|chii\.in)/.*
  10. // @grant none
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/localforage/1.10.0/localforage.min.js
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // 设置并发操作的上限
  19. const MAX_CONCURRENT_OPERATIONS = 5;
  20.  
  21. // 创建导入按钮
  22. const importButton = document.createElement('input');
  23. importButton.type = 'file';
  24. importButton.style.position = 'fixed';
  25. importButton.style.top = '10px';
  26. importButton.style.left = '10px';
  27. document.body.appendChild(importButton);
  28.  
  29. // 创建导出按钮
  30. const exportButton = document.createElement('button');
  31. exportButton.textContent = 'Export Data';
  32. exportButton.style.position = 'fixed';
  33. exportButton.style.top = '40px';
  34. exportButton.style.left = '10px';
  35. document.body.appendChild(exportButton);
  36.  
  37. // 点击导入按钮时触发事件
  38. importButton.addEventListener('change', async function(event) {
  39. const file = event.target.files[0];
  40. if (!file) return;
  41.  
  42. try {
  43. const data = await readFile(file);
  44. await importDataConcurrently(data);
  45. alert('Data imported successfully!');
  46. } catch (error) {
  47. console.error('Error importing data:', error);
  48. alert('Error importing data. See console for details.');
  49. }
  50. });
  51.  
  52. // 点击导出按钮时触发事件
  53. exportButton.addEventListener('click', async function() {
  54. try {
  55. const data = await exportData();
  56. downloadData(data);
  57. } catch (error) {
  58. console.error('Error exporting data:', error);
  59. alert('Error exporting data. See console for details.');
  60. }
  61. });
  62.  
  63. // 读取文件并返回数据
  64. function readFile(file) {
  65. return new Promise((resolve, reject) => {
  66. const reader = new FileReader();
  67. reader.onload = () => resolve(reader.result);
  68. reader.onerror = error => reject(error);
  69. reader.readAsText(file);
  70. });
  71. }
  72.  
  73. // 并发导入数据到 IndexedDB
  74. async function importDataConcurrently(data) {
  75. const jsonData = JSON.parse(data);
  76. const keys = Object.keys(jsonData);
  77.  
  78. // 切割任务,以 MAX_CONCURRENT_OPERATIONS 为一组进行并发操作
  79. const chunks = [];
  80. for (let i = 0; i < keys.length; i += MAX_CONCURRENT_OPERATIONS) {
  81. chunks.push(keys.slice(i, i + MAX_CONCURRENT_OPERATIONS));
  82. }
  83.  
  84. // 对每个组进行并发操作
  85. for (const chunk of chunks) {
  86. await Promise.all(chunk.map(async (key) => {
  87. await localforage.setItem(key, jsonData[key]);
  88. }));
  89. }
  90. }
  91.  
  92. // 导出数据
  93. async function exportData() {
  94. const keys = await localforage.keys();
  95. const data = {};
  96. for (const key of keys) {
  97. data[key] = await localforage.getItem(key);
  98. }
  99. return JSON.stringify(data, null, 2);
  100. }
  101.  
  102. // 下载数据为 JSON 文件
  103. function downloadData(data) {
  104. const blob = new Blob([data], { type: 'application/json' });
  105. const url = URL.createObjectURL(blob);
  106. const link = document.createElement('a');
  107. link.href = url;
  108. link.download = 'indexedDB_data.json';
  109. link.click();
  110. }
  111. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址