88 lines
2.3 KiB
HTML
88 lines
2.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>TypeWords 数据迁移(旧域名)</title>
|
|
</head>
|
|
<body>
|
|
<h2 id="title">等待新域名发送迁移指令...</h2>
|
|
<pre id="log"></pre>
|
|
|
|
<script>
|
|
function log(msg) {
|
|
console.log(msg);
|
|
document.getElementById('log').textContent += msg + "\n";
|
|
}
|
|
|
|
// 1️⃣ 先动态加载 idb-keyval
|
|
function loadIDBKeyval() {
|
|
return new Promise((resolve) => {
|
|
let script = document.createElement("script");
|
|
script.src = '/libs/idb-keyval.js';
|
|
script.onload = function () {
|
|
log("idb-keyval 加载完成");
|
|
resolve(window.idbKeyval);
|
|
};
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
loadIDBKeyval(); // 确保 idb-keyval 已经加载
|
|
|
|
// 2️⃣ 读取 IndexedDB
|
|
async function readAllStorageForMigration(db) {
|
|
// localStorage 数据
|
|
const localStorageData = {
|
|
PracticeSaveWord: localStorage.getItem('PracticeSaveWord'),
|
|
PracticeSaveArticle: localStorage.getItem('PracticeSaveArticle')
|
|
};
|
|
|
|
// IndexedDB 数据,key 对应你的老项目
|
|
const keys = [
|
|
'type-words-app-version',
|
|
'typing-word-dict',
|
|
'typing-word-setting',
|
|
'typing-word-files'
|
|
];
|
|
|
|
const indexedDBData = {};
|
|
for (let key of keys) {
|
|
let res = await db.get(key);
|
|
if (res) indexedDBData[key] = res
|
|
}
|
|
|
|
return {
|
|
localStorage: localStorageData,
|
|
indexedDB: indexedDBData
|
|
};
|
|
}
|
|
|
|
// 3️⃣ 接收新域名指令
|
|
window.addEventListener('message', async (event) => {
|
|
if (event.data?.type !== 'REQUEST_MIGRATION_DATA') return;
|
|
|
|
// 安全校验 origin,可选
|
|
// if (event.origin !== 'https://typewords.cc') return;
|
|
|
|
log("收到迁移指令,开始读取数据...");
|
|
|
|
const db = await loadIDBKeyval(); // 确保 idb-keyval 已经加载
|
|
const data = await readAllStorageForMigration(db);
|
|
|
|
log("读取完成,发送数据给新域名");
|
|
event.source.postMessage({
|
|
type: 'MIGRATION_RESULT',
|
|
payload: data
|
|
}, event.origin);
|
|
|
|
log("已发送迁移数据");
|
|
// 自动关闭窗口(延迟 500ms)
|
|
setTimeout(() => {
|
|
document.getElementById('title').textContent = '迁移完成,请手动关闭页面'
|
|
window.close();
|
|
}, 500);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|