|
| 1 | +import { getRealmIdFromLink } from './storage.js'; |
| 2 | + |
| 3 | +const providers = []; |
| 4 | + |
| 5 | +export function registerExportInfo(provider) { |
| 6 | + if (!provider || typeof provider !== 'object') return; |
| 7 | + providers.push(provider); |
| 8 | +} |
| 9 | + |
| 10 | +function collectKeys(provider, realmId) { |
| 11 | + const keys = typeof provider.keys === 'function' |
| 12 | + ? provider.keys(realmId) |
| 13 | + : (Array.isArray(provider.keys) ? provider.keys : []); |
| 14 | + return Array.isArray(keys) ? keys : []; |
| 15 | +} |
| 16 | + |
| 17 | +function keyMatches(provider, realmId, key) { |
| 18 | + if (!provider.match) return false; |
| 19 | + const pattern = typeof provider.match === 'function' ? provider.match(realmId) : provider.match; |
| 20 | + if (pattern instanceof RegExp) return pattern.test(key); |
| 21 | + return false; |
| 22 | +} |
| 23 | + |
| 24 | +function readRaw(key) { |
| 25 | + const raw = localStorage.getItem(key); |
| 26 | + if (raw === null) return null; |
| 27 | + const entry = { raw }; |
| 28 | + try { |
| 29 | + entry.parsed = JSON.parse(raw); |
| 30 | + } catch (e) { |
| 31 | + // 非 JSON 值保持原始字符串 |
| 32 | + } |
| 33 | + return entry; |
| 34 | +} |
| 35 | + |
| 36 | +export function collectExportData() { |
| 37 | + const realmId = typeof getRealmIdFromLink === 'function' ? getRealmIdFromLink() : null; |
| 38 | + const realm = {}; |
| 39 | + const global = {}; |
| 40 | + const realmSeen = new Set(); |
| 41 | + const globalSeen = new Set(); |
| 42 | + |
| 43 | + const add = (target, seen, key) => { |
| 44 | + if (seen.has(key)) return; |
| 45 | + const entry = readRaw(key); |
| 46 | + if (entry === null) return; |
| 47 | + seen.add(key); |
| 48 | + target[key] = entry; |
| 49 | + }; |
| 50 | + |
| 51 | + for (const provider of providers) { |
| 52 | + const isRealm = provider.scope === 'realm'; |
| 53 | + const target = isRealm ? realm : global; |
| 54 | + const seen = isRealm ? realmSeen : globalSeen; |
| 55 | + for (const key of collectKeys(provider, realmId)) add(target, seen, key); |
| 56 | + if (provider.match) { |
| 57 | + for (const key of Object.keys(localStorage)) { |
| 58 | + if (keyMatches(provider, realmId, key)) add(target, seen, key); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const meta = { |
| 64 | + pluginName: typeof GM_info !== 'undefined' ? GM_info.script.name : 'autoMaxPPHPL', |
| 65 | + scriptVersion: typeof GM_info !== 'undefined' ? GM_info.script.version : '未知', |
| 66 | + exportedAt: new Date().toISOString(), |
| 67 | + pageUrl: location.href, |
| 68 | + realmId, |
| 69 | + registeredProviders: providers.map(p => p.name || '未命名'), |
| 70 | + realmKeyCount: Object.keys(realm).length, |
| 71 | + globalKeyCount: Object.keys(global).length |
| 72 | + }; |
| 73 | + |
| 74 | + return { meta, realm, global }; |
| 75 | +} |
| 76 | + |
| 77 | +export function downloadExportData() { |
| 78 | + const data = collectExportData(); |
| 79 | + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); |
| 80 | + const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json;charset=utf-8' }); |
| 81 | + const url = URL.createObjectURL(blob); |
| 82 | + const link = document.createElement('a'); |
| 83 | + link.href = url; |
| 84 | + link.download = `SC_Export_${data.meta.realmId ?? 'unknown'}_${timestamp}.json`; |
| 85 | + document.body.appendChild(link); |
| 86 | + link.click(); |
| 87 | + link.remove(); |
| 88 | + setTimeout(() => URL.revokeObjectURL(url), 1000); |
| 89 | + return data; |
| 90 | +} |
| 91 | + |
| 92 | +window.SC_ExportInfo = { |
| 93 | + registerExportInfo, |
| 94 | + collectExportData, |
| 95 | + downloadExportData |
| 96 | +}; |
0 commit comments