Explorar o código

capture at least 1 tweet by owner, if any

Mike L %!s(int64=3) %!d(string=hai) anos
pai
achega
be2d0fcc65
Modificáronse 2 ficheiros con 16 adicións e 5 borrados
  1. 8 3
      dist/webshot.js
  2. 8 2
      src/webshot.ts

+ 8 - 3
dist/webshot.js

@@ -107,9 +107,14 @@ class Webshot extends CallableInstance {
                         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;
+                            const selector = '[data-testid="tweet"]>:nth-child(2)>:first-child a';
+                            const getProfileUrl = () => div.querySelector(selector).href;
+                            const ownerProfileUrl = getProfileUrl();
+                            while (div = div.previousElementSibling) {
+                                if (getProfileUrl() !== ownerProfileUrl)
+                                    continue;
+                                return document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
+                            }
                         });
                     })
                         .then(() => page.evaluate(() => {

+ 8 - 2
src/webshot.ts

@@ -132,8 +132,14 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
             .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;
+                const selector = '[data-testid="tweet"]>:nth-child(2)>:first-child a';
+                const getProfileUrl = () => div.querySelector<HTMLAnchorElement>(selector).href;
+                const ownerProfileUrl = getProfileUrl();
+                // eslint-disable-next-line no-cond-assign
+                while (div = div.previousElementSibling as HTMLDivElement) {
+                  if (getProfileUrl() !== ownerProfileUrl) continue;
+                  return document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
+                }
               });
             })
             .then(() => page.evaluate(() => {