save2
This commit is contained in:
@@ -324,112 +324,6 @@
|
||||
toggleEl('#qqDialog', true)
|
||||
}
|
||||
</script>
|
||||
|
||||
<script>
|
||||
// 旧域名地址
|
||||
var OLD_ORIGIN = 'https://2study.top';
|
||||
|
||||
// 需要迁移的 IndexedDB key
|
||||
var IDB_KEYS = [
|
||||
'type-words-app-version',
|
||||
'typing-word-dict',
|
||||
'typing-word-setting',
|
||||
'typing-word-files'
|
||||
];
|
||||
|
||||
// 需要迁移的 localStorage key
|
||||
var LS_KEYS = [
|
||||
'PracticeSaveWord',
|
||||
'PracticeSaveArticle'
|
||||
];
|
||||
|
||||
// 1️⃣ 动态加载 idb-keyval
|
||||
function loadIDBKeyval() {
|
||||
return new Promise((resolve) => {
|
||||
if (window.idbKeyval) return resolve(window.idbKeyval);
|
||||
|
||||
var script = document.createElement('script');
|
||||
script.src = 'https://cdn.jsdelivr.net/npm/idb-keyval@6.2.2/dist/umd.js';
|
||||
script.onload = () => resolve(window.idbKeyval);
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
// 创建迁移窗口并接收数据
|
||||
async function migrateFromOldSite() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const db = await loadIDBKeyval();
|
||||
|
||||
const migrateWin = window.open(`${OLD_ORIGIN}/migrate.html`, '_blank', 'width=400,height=400');
|
||||
|
||||
if (!migrateWin) return reject('弹窗被阻止,请允许弹窗');
|
||||
|
||||
function onMessage(event) {
|
||||
if (event.origin !== OLD_ORIGIN) return;
|
||||
if (event.data?.type !== 'MIGRATION_RESULT') return;
|
||||
|
||||
const payload = event.data.payload;
|
||||
|
||||
console.log('payload', payload);
|
||||
|
||||
// 写入 localStorage
|
||||
LS_KEYS.forEach(key => {
|
||||
if (payload.localStorage[key] !== undefined) {
|
||||
localStorage.setItem(key, payload.localStorage[key]);
|
||||
}
|
||||
});
|
||||
|
||||
// 写入 IndexedDB
|
||||
(async () => {
|
||||
for (let key of IDB_KEYS) {
|
||||
if (payload.indexedDB[key] !== undefined) {
|
||||
await db.set(key, payload.indexedDB[key]);
|
||||
}
|
||||
}
|
||||
|
||||
window.removeEventListener('message', onMessage);
|
||||
resolve(true);
|
||||
})();
|
||||
}
|
||||
|
||||
window.addEventListener('message', onMessage);
|
||||
|
||||
// 等窗口加载完毕后发请求
|
||||
const timer = setInterval(() => {
|
||||
if (!migrateWin || migrateWin.closed) {
|
||||
clearInterval(timer);
|
||||
reject('迁移窗口已关闭');
|
||||
} else {
|
||||
try {
|
||||
migrateWin.postMessage({type: 'REQUEST_MIGRATION_DATA'}, OLD_ORIGIN);
|
||||
} catch (e) {
|
||||
// 跨域安全错误忽略,等窗口完全加载后再试
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (location.href === 'https://typewords.cc/') {
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
// 3️⃣ 自动执行迁移(只执行一次)
|
||||
(async () => {
|
||||
if (localStorage.getItem('__migrated_from_2study_top__')) return;
|
||||
|
||||
console.log('开始从旧域名迁移数据…');
|
||||
|
||||
try {
|
||||
await migrateFromOldSite();
|
||||
localStorage.setItem('__migrated_from_2study_top__', '1');
|
||||
console.log('迁移完成');
|
||||
} catch (e) {
|
||||
console.error('迁移失败', e);
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
|
||||
Reference in New Issue
Block a user