Skip to content

Commit 2a6ca38

Browse files
authored
fix: jianshu timeout (#198)
1 parent ed66b2b commit 2a6ca38

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

apps/extension/src/background.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ async function syncToPlatform(platformId, content) {
11731173
11741174
return { success: true, message: '已同步到支付宝开放平台', tabId: tab.id }
11751175
} else [DISABLED] */
1176-
if (platformId !== 'wechat') {
1176+
if (platformId !== 'wechat' && !tab) {
11771177
// 其他平台(排除微信,因为微信在上面已经处理)
11781178
let targetUrl = platform.publishUrl
11791179

@@ -3367,17 +3367,35 @@ function fillContentOnPage(content, platformId) {
33673367
}
33683368

33693369
// 等待标签页加载
3370-
function waitForTab(tabId, timeout = 300000) {
3370+
function waitForTab(tabId, timeout = 60000) {
33713371
return new Promise((resolve, reject) => {
33723372
const start = Date.now()
3373+
let urlReady = false
3374+
let urlReadyTime = 0
33733375
const check = () => {
33743376
chrome.tabs.get(tabId, tab => {
33753377
if (chrome.runtime.lastError) {
33763378
reject(new Error(chrome.runtime.lastError.message))
3377-
} else if (tab.status === 'complete') {
3379+
return
3380+
}
3381+
if (tab.status === 'complete') {
33783382
setTimeout(resolve, 1500)
3379-
} else if (Date.now() - start > timeout) {
3380-
reject(new Error('页面加载超时'))
3383+
return
3384+
}
3385+
// 如果 URL 已经不是 about:blank/chrome:// 且处于 loading 状态超过 10 秒,
3386+
// 说明主文档已加载但第三方资源可能超时,提前 resolve
3387+
if (!urlReady && tab.url && !tab.url.startsWith('about:') && !tab.url.startsWith('chrome:')) {
3388+
urlReady = true
3389+
urlReadyTime = Date.now()
3390+
}
3391+
if (urlReady && Date.now() - urlReadyTime > 10000) {
3392+
console.log('[COSE] waitForTab: 页面 URL 已就绪但 status 仍为 loading,提前继续')
3393+
setTimeout(resolve, 1500)
3394+
return
3395+
}
3396+
if (Date.now() - start > timeout) {
3397+
console.log('[COSE] waitForTab: 超时,继续执行')
3398+
resolve()
33813399
} else {
33823400
setTimeout(check, 300)
33833401
}

0 commit comments

Comments
 (0)