Skip to content

Commit

Permalink
feat: auto get import id
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed May 25, 2024
1 parent ed34803 commit c881935
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
githubJpgStorage,
githubZipStorage,
screenshotStorage,
syncImportStorage,
urlStorage,
} from './storage';
import Compressor from 'compressorjs';
Expand Down Expand Up @@ -103,6 +104,7 @@ export const exportSnapshotAsZipUrl = async (snapshot: Snapshot) => {
).then((r) => {
githubZipStorage[snapshot.id] = r.href;
urlStorage[r.href] = snapshot.id;
detectSnapshot(r.id);
return r.href;
})
);
Expand Down Expand Up @@ -134,3 +136,20 @@ export const batchCreateZipUrl = async (snapshots: Snapshot[]) => {
return p;
}, []);
};

export const detectSnapshot = async (
importId: number | string,
noFetch = false,
) => {
if (!Number.isSafeInteger(+importId)) {
return;
}
if (syncImportStorage[importId]) {
return;
}
syncImportStorage[importId] = true;
if (noFetch) {
return;
}
await fetch(`https://detect.gkd.li/api/detectSnapshot?importId=` + importId);
};
2 changes: 1 addition & 1 deletion src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const enhanceFetch = async (
// export snapshot need
return GM_fetch(input, init);
} else if (options?.proxy) {
const proxyUrl = new URL(`https://proxy-workers.lisonge.workers.dev/`);
const proxyUrl = new URL(`https://proxy.gkd.li`);
proxyUrl.searchParams.set(`proxyUrl`, u.href);
const request = new Request(input, init);
return fetch(proxyUrl, {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export const githubZipStorage = useReactiveStorage<Record<number, string>>(
{},
);

export const syncImportStorage = useReactiveStorage<
Record<number | string, boolean>
>(`syncImport`, {});

export const settingsStorage = useReactiveStorage<{
autoUploadImport: boolean;
ignoreUploadWarn: boolean;
Expand Down
35 changes: 31 additions & 4 deletions src/views/SnapshotPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ScreenshotCard from '@/components/ScreenshotCard.vue';
import SearchCard from '@/components/SearchCard.vue';
import WindowCard from '@/components/WindowCard.vue';
import { listToTree } from '@/utils/node';
import { message } from '@/utils/discrete';
import { loadingBar, message } from '@/utils/discrete';
import { delay } from '@/utils/others';
import {
snapshotStorage,
Expand All @@ -18,11 +18,16 @@ import { computed, shallowRef, watchEffect } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useTitle } from '@vueuse/core';
import { gmOk } from '@/utils/gm';
import { exportSnapshotAsJpgUrl, exportSnapshotAsZipUrl } from '@/utils/export';
import {
detectSnapshot,
exportSnapshotAsJpgUrl,
exportSnapshotAsZipUrl,
} from '@/utils/export';
import type { Selector } from '@/utils/selector';
import { NModal, NIcon } from 'naive-ui';
import MultiFocusCard from '@/components/MultiFocusCard.vue';
import { watch, defineAsyncComponent } from 'vue';
import { onMounted } from 'vue';
const AsyncTrackGraph = (() => {
const loader = () => import('@/components/TrackGraph.vue');
setTimeout(loader, 3000);
Expand All @@ -45,12 +50,34 @@ const showSize = computed(() => {
const screenshotUrl = shallowRef(``);
const snapshot = shallowRef<Snapshot>();
watchEffect(async () => {
onMounted(async () => {
if (!Number.isSafeInteger(+snapshotId.value)) {
message.error('非法快照ID');
return;
}
const localSnapshot = await snapshotStorage.getItem(snapshotId.value);
if (!localSnapshot) {
message.error(`快照数据缺失`);
loadingBar.start();
try {
const importId: number | null = await fetch(
'https://detect.gkd.li/api/getImportId?id=' + snapshotId.value,
).then((r) => r.json());
if (importId) {
router.replace('/i/' + importId);
return;
}
message.error(`快照数据缺失`);
} finally {
loadingBar.finish();
}
return;
}
setTimeout(() => {
const importId = githubZipStorage[localSnapshot.id]?.substring(41, 49);
if (importId) {
detectSnapshot(importId);
}
}, 1000);
if (gmOk() && settingsStorage.autoUploadImport) {
// 静默生成 jpg/zip
setTimeout(async () => {
Expand Down

0 comments on commit c881935

Please sign in to comment.