瀏覽代碼

auto uncollapse sensitive tweets

Mike L 3 年之前
父節點
當前提交
69acb556cf
共有 2 個文件被更改,包括 20 次插入2 次删除
  1. 9 1
      dist/webshot.js
  2. 11 1
      src/webshot.ts

+ 9 - 1
dist/webshot.js

@@ -80,7 +80,7 @@ class Webshot extends CallableInstance {
                     })
                         .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
                         .then(() => page.addStyleTag({
-                        content: 'header,#layers{display:none!important}' +
+                        content: 'header,#layers{display:none!important}article{background-color:transparent!important}' +
                             '[data-testid="caret"],[role="group"],[data-testid="tweet"]+*>[class*=" "]+div:nth-last-child(2){display:none}',
                     }))
                         .then(() => page.addStyleTag({
@@ -97,6 +97,14 @@ class Webshot extends CallableInstance {
                         }, 250);
                     }))
                         .then(() => page.waitForSelector('xpath=//section/*/*/div[.//article/*/*/*/*/*[@role="group"]]', { timeout: getTimeout() }))
+                        .then(handle => handle.$$('xpath=..//a[contains(@href,"content_you_see")]/../../..//*[@role="button"]')
+                        .then(sensitiveToggles => {
+                        const count = sensitiveToggles.length;
+                        if (count)
+                            logger.info(`found ${count} sensitive ${count === 1 ? 'tweet' : 'tweets'} on page, uncollapsing...`);
+                        return Promise.all(sensitiveToggles.map(toggle => toggle.click()));
+                    })
+                        .then(() => handle))
                         .catch((err) => {
                         if (err.name !== 'TimeoutError')
                             throw err;

+ 11 - 1
src/webshot.ts

@@ -106,7 +106,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
             .then(() => page.goto(url, {waitUntil: 'load', timeout: getTimeout()}))
             // hide header, "more options" button, like and retweet count
             .then(() => page.addStyleTag({
-              content: 'header,#layers{display:none!important}' +
+              content: 'header,#layers{display:none!important}article{background-color:transparent!important}' +
                 '[data-testid="caret"],[role="group"],[data-testid="tweet"]+*>[class*=" "]+div:nth-last-child(2){display:none}',
             }))
             .then(() => page.addStyleTag({
@@ -124,11 +124,21 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
               }, 250);
             }))
             .then(() => page.waitForSelector('xpath=//section/*/*/div[.//article/*/*/*/*/*[@role="group"]]', {timeout: getTimeout()}))
+            // toggle visibility of sensitive tweets
+            .then(handle => handle.$$('xpath=..//a[contains(@href,"content_you_see")]/../../..//*[@role="button"]')
+              .then(sensitiveToggles => {
+                const count = sensitiveToggles.length;
+                if (count) logger.info(`found ${count} sensitive ${count === 1 ? 'tweet' : 'tweets'} on page, uncollapsing...`);
+                return Promise.all(sensitiveToggles.map(toggle => toggle.click()));
+              })
+              .then(() => handle)
+            )
             .catch((err: Error): Promise<puppeteer.ElementHandle<HTMLDivElement> | null> => {
               if (err.name !== 'TimeoutError') throw err;
               logger.warn(`navigation timed out at ${getTimerTime()} seconds`);
               return null;
             })
+            // scroll to last tweet by owner in thread, if any, or top   of thread
             .then((handle: puppeteer.ElementHandle<HTMLDivElement>) => {
               if (handle === null) throw new puppeteer.errors.TimeoutError();
               return handle.evaluate(div => {