Files
TypeWords/public/migrate.html
Zyronon 8af36d93f8 save4
2025-11-18 15:00:26 +08:00

87 lines
2.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TypeWords 数据迁移(旧域名)</title>
</head>
<body>
<h2>等待新域名发送迁移指令...</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 = 'https://cdn.jsdelivr.net/npm/idb-keyval@6.2.2/dist/umd.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(() => {
window.close();
}, 500);
});
</script>
</body>
</html>