Просмотр исходного кода

limit captured preceding replies to 1

Mike L 3 лет назад
Родитель
Сommit
63a8ba4300
2 измененных файлов с 14 добавлено и 11 удалено
  1. 7 5
      dist/webshot.js
  2. 7 6
      src/webshot.ts

+ 7 - 5
dist/webshot.js

@@ -96,16 +96,21 @@ class Webshot extends CallableInstance {
                             });
                         }, 250);
                     }))
-                        .then(() => page.waitForSelector('article', { timeout: getTimeout() }))
+                        .then(() => page.waitForSelector('xpath=//section/*/*/div[.//article/*/*/*/*/*[@role="group"]]', { timeout: getTimeout() }))
                         .catch((err) => {
                         if (err.name !== 'TimeoutError')
                             throw err;
                         logger.warn(`navigation timed out at ${getTimerTime()} seconds`);
                         return null;
                     })
-                        .then(handle => {
+                        .then((handle) => {
                         if (handle === null)
                             throw new puppeteer.errors.TimeoutError();
+                        return handle.evaluate(div => {
+                            const replyingTo = div.previousElementSibling;
+                            if (replyingTo)
+                                document.documentElement.scrollTop = window.scrollY + replyingTo.getBoundingClientRect().top;
+                        });
                     })
                         .then(() => page.evaluate(() => {
                         const cardImg = document.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
@@ -131,9 +136,6 @@ class Webshot extends CallableInstance {
                         if (cardImg)
                             this.extendEntity(cardImg);
                     })
-                        .then(() => page.addScriptTag({
-                        content: 'document.documentElement.scrollTop=0;',
-                    }))
                         .then(() => utils_1.chainPromises(morePostProcessings.map(func => () => func(page))))
                         .then(() => util_1.promisify(setTimeout)(getTimeout()))
                         .then(() => page.screenshot())

+ 7 - 6
src/webshot.ts

@@ -123,14 +123,18 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
                 });
               }, 250);
             }))
-            .then(() => page.waitForSelector('article', {timeout: getTimeout()}))
-            .catch((err: Error): Promise<puppeteer.ElementHandle<Element> | null> => {
+            .then(() => page.waitForSelector('xpath=//section/*/*/div[.//article/*/*/*/*/*[@role="group"]]', {timeout: getTimeout()}))
+            .catch((err: Error): Promise<puppeteer.ElementHandle<HTMLDivElement> | null> => {
               if (err.name !== 'TimeoutError') throw err;
               logger.warn(`navigation timed out at ${getTimerTime()} seconds`);
               return null;
             })
-            .then(handle => {
+            .then((handle: puppeteer.ElementHandle<HTMLDivElement>) => {
               if (handle === null) throw new puppeteer.errors.TimeoutError();
+              return handle.evaluate(div => {
+                const replyingTo = div.previousElementSibling;
+                if (replyingTo) document.documentElement.scrollTop = window.scrollY + replyingTo.getBoundingClientRect().top;
+              });
             })
             .then(() => page.evaluate(() => {
               const cardImg = document.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
@@ -156,9 +160,6 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
             .then(cardImg => {
               if (cardImg) this.extendEntity(cardImg); 
             })
-            .then(() => page.addScriptTag({
-              content: 'document.documentElement.scrollTop=0;',
-            }))
             .then(() => chainPromises(morePostProcessings.map(func => () => func(page))))
             .then(() => promisify(setTimeout)(getTimeout()))
             .then(() => page.screenshot())