Skip to content

Commit 36defc7

Browse files
committed
feat(weibo): add upload completion check for images
- Implemented a helper function to wait for all image uploads to complete before proceeding. - Added a delay before clicking the send button to ensure all actions are completed smoothly.
1 parent f28cf72 commit 36defc7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/sync/dynamic/weibo.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ export async function DynamicWeibo(data: SyncData) {
5454
});
5555
}
5656

57+
// 辅助函数:等待所有图片上传完成
58+
function waitForUploadsToComplete(timeout = 60000): Promise<void> {
59+
return new Promise((resolve, reject) => {
60+
const startTime = Date.now();
61+
62+
const checkStatus = () => {
63+
// 查找所有表示正在加载的元素
64+
const loadingElements = document.querySelectorAll('.Image_loading_1lfUB');
65+
66+
// 如果没有正在加载的元素,说明上传完成
67+
if (loadingElements.length === 0) {
68+
console.log('所有图片上传已完成。');
69+
resolve();
70+
return;
71+
}
72+
73+
// 检查是否超时
74+
if (Date.now() - startTime > timeout) {
75+
reject(new Error(`图片上传在 ${timeout}ms 内未完成。`));
76+
return;
77+
}
78+
79+
// 稍后重试
80+
setTimeout(checkStatus, 500);
81+
};
82+
83+
// `waitForElements` 返回后,加载动画可能还未渲染出来,延迟一下首次检查
84+
setTimeout(checkStatus, 100);
85+
});
86+
}
87+
5788
// 辅助函数:上传文件
5889
async function uploadFiles() {
5990
const fileInput = (await waitForElement('input[type="file"]')) as HTMLInputElement;
@@ -105,6 +136,7 @@ export async function DynamicWeibo(data: SyncData) {
105136
if (images && images.length > 0) {
106137
await uploadFiles();
107138
await waitForElements('i[title="删除"]', images.length);
139+
await waitForUploadsToComplete();
108140
}
109141

110142
console.log('成功填入微博内容和图片');
@@ -116,6 +148,7 @@ export async function DynamicWeibo(data: SyncData) {
116148

117149
if (sendButton) {
118150
console.log('点击发送按钮');
151+
await new Promise((resolve) => setTimeout(resolve, 10000));
119152
(sendButton as HTMLElement).click();
120153
await new Promise((resolve) => setTimeout(resolve, 3000));
121154
window.location.reload();

0 commit comments

Comments
 (0)