فهرست منبع

make webshot timeout retrial actually work

Mike L 3 سال پیش
والد
کامیت
25b7535cdb
2فایلهای تغییر یافته به همراه26 افزوده شده و 23 حذف شده
  1. 9 9
      dist/webshot.js
  2. 17 14
      src/webshot.ts

+ 9 - 9
dist/webshot.js

@@ -71,10 +71,16 @@ class Webshot extends CallableInstance {
                     const startTime = new Date().getTime();
                     const getTimerTime = () => new Date().getTime() - startTime;
                     const getTimeout = () => Math.max(500, webshotDelay - getTimerTime());
-                    const goto = () => page.goto(url, { waitUntil: 'load', timeout: Math.min(10000, getTimeout()) }).catch(err => {
+                    const gotoUrlAndWaitForTweet = () => page.goto(url, { waitUntil: 'load', timeout: Math.min(10000, getTimeout()) })
+                        .then(() => Promise.race([
+                        page.waitForSelector('article', { state: 'attached', timeout: getTimeout() }),
+                        page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', { timeout: getTimeout() }),
+                        page.waitForSelector('div[role="button"]>div>span>:text-matches("^やりなおす|更新$")', { state: 'attached', timeout: getTimeout() }).then(() => page.reload({ timeout: getTimeout() })),
+                    ]))
+                        .catch(err => {
                         if (err.name === 'TimeoutError' && webshotDelay > getTimerTime()) {
                             logger.warn(`navigation timed out after ${getTimerTime()} ms, retrying...`);
-                            return goto();
+                            return gotoUrlAndWaitForTweet();
                         }
                         throw err;
                     });
@@ -83,13 +89,7 @@ class Webshot extends CallableInstance {
                         height: height / zoomFactor,
                     })
                         .then(() => page.route('*:\/\/video.twimg.com\/**', route => route.abort()))
-                        .then(goto)
-                        .then(() => Promise.race([
-                        page.waitForSelector('article', { state: 'attached', timeout: getTimeout() }),
-                        page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', { timeout: getTimeout() }),
-                        page.waitForSelector('div[role="button"]>div>span>:text-matches("^やりなおす|更新$")', { state: 'attached', timeout: getTimeout() })
-                            .then(() => page.reload({ timeout: getTimeout() })),
-                    ]))
+                        .then(gotoUrlAndWaitForTweet)
                         .then(() => page.addStyleTag({
                         content: 'header,#layers{display:none!important}article{background-color:transparent!important}' +
                             '[data-testid="caret"],[role="group"],[data-testid="tweet"] [class*=" "]+:last-child>*+[class*=" "]~div{display:none}',

+ 17 - 14
src/webshot.ts

@@ -95,25 +95,28 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
           const startTime = new Date().getTime();
           const getTimerTime = () => new Date().getTime() - startTime;
           const getTimeout = () => Math.max(500, webshotDelay - getTimerTime());
-          const goto = () => page.goto(url, {waitUntil: 'load', timeout: Math.min(10000, getTimeout())}).catch(err => {
-            if (err.name === 'TimeoutError' && webshotDelay > getTimerTime()) {
-              logger.warn(`navigation timed out after ${getTimerTime()} ms, retrying...`);
-              return goto();
-            }
-            throw err;
-          });
+          const gotoUrlAndWaitForTweet = () =>
+            page.goto(url, {waitUntil: 'load', timeout: Math.min(10000, getTimeout())})
+              .then(() => Promise.race([
+                page.waitForSelector('article', {state: 'attached', timeout: getTimeout()}),
+                page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', {timeout: getTimeout()}),
+                page.waitForSelector(
+                  'div[role="button"]>div>span>:text-matches("^やりなおす|更新$")'
+                , {state: 'attached', timeout: getTimeout()}).then(() => page.reload({timeout: getTimeout()})),
+              ]))
+              .catch(err => {
+                if (err.name === 'TimeoutError' && webshotDelay > getTimerTime()) {
+                  logger.warn(`navigation timed out after ${getTimerTime()} ms, retrying...`);
+                  return gotoUrlAndWaitForTweet();
+                }
+                throw err;
+              });
           page.setViewportSize({
             width: width / zoomFactor,
             height: height / zoomFactor,
           })
             .then(() => page.route('*:\/\/video.twimg.com\/**', route => route.abort()))
-            .then(goto)
-            .then(() => Promise.race([
-              page.waitForSelector('article', {state: 'attached', timeout: getTimeout()}),
-              page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', {timeout: getTimeout()}),
-              page.waitForSelector('div[role="button"]>div>span>:text-matches("^やりなおす|更新$")', {state: 'attached', timeout: getTimeout()})
-                .then(() => page.reload({timeout: getTimeout()})),
-            ]))
+            .then(gotoUrlAndWaitForTweet)
             // hide header, "more options" button, like and retweet count
             .then(() => page.addStyleTag({
               content: 'header,#layers{display:none!important}article{background-color:transparent!important}' +