Pārlūkot izejas kodu

handle exceptions if a tweet is hidden/deleted

Mike L 3 gadi atpakaļ
vecāks
revīzija
6f3467073a
2 mainītis faili ar 20 papildinājumiem un 15 dzēšanām
  1. 10 7
      dist/webshot.js
  2. 10 8
      src/webshot.ts

+ 10 - 7
dist/webshot.js

@@ -115,14 +115,17 @@ class Webshot extends CallableInstance {
                         if (handle === null)
                             throw new puppeteer.errors.TimeoutError();
                         return handle.evaluate(div => {
-                            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;
+                            try {
+                                const selector = '[data-testid="tweet"]>:nth-child(2)>:first-child a';
+                                const getProfileUrl = () => (div.querySelector(selector) || { href: '' }).href;
+                                const ownerProfileUrl = getProfileUrl();
+                                while (div = div.previousElementSibling) {
+                                    if (getProfileUrl() !== ownerProfileUrl)
+                                        continue;
+                                    return document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
+                                }
                             }
+                            catch (_a) { }
                             document.documentElement.scrollTop = 0;
                         });
                     })

+ 10 - 8
src/webshot.ts

@@ -142,14 +142,16 @@ 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 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;
-                }
+                try {
+                  const selector = '[data-testid="tweet"]>:nth-child(2)>:first-child a';
+                  const getProfileUrl = () => (div.querySelector<HTMLAnchorElement>(selector) || {href: ''}).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;
+                  }
+                } catch {/* handle errors like none-found cases */}
                 document.documentElement.scrollTop = 0;
               });
             })