Browse Source

scrape card image from main tweet only

Mike L 3 years ago
parent
commit
8cfa9a5e38
2 changed files with 7 additions and 6 deletions
  1. 3 3
      dist/webshot.js
  2. 4 3
      src/webshot.ts

+ 3 - 3
dist/webshot.js

@@ -127,10 +127,10 @@ class Webshot extends CallableInstance {
                             }
                             catch (_a) { }
                             document.documentElement.scrollTop = 0;
-                        });
+                        }).then(() => handle);
                     })
-                        .then(() => page.evaluate(() => {
-                        const cardImg = document.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
+                        .then(handle => handle.evaluate(div => {
+                        const cardImg = div.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
                         if (typeof (cardImg === null || cardImg === void 0 ? void 0 : cardImg.getAttribute('src')) === 'string') {
                             const match = /^(.*\/card_img\/(\d+)\/.+\?format=.*)&name=/.exec(cardImg === null || cardImg === void 0 ? void 0 : cardImg.getAttribute('src'));
                             if (match) {

+ 4 - 3
src/webshot.ts

@@ -154,10 +154,11 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
                   }
                 } catch {/* handle errors like none-found cases */}
                 document.documentElement.scrollTop = 0;
-              });
+              }).then(() => handle);
             })
-            .then(() => page.evaluate(() => {
-              const cardImg = document.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
+            // scrape card image from main tweet
+            .then(handle => handle.evaluate(div => {
+              const cardImg = div.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img');
               if (typeof cardImg?.getAttribute('src') === 'string') {
                 const match = /^(.*\/card_img\/(\d+)\/.+\?format=.*)&name=/.exec(cardImg?.getAttribute('src'));
                 if (match) {