Prechádzať zdrojové kódy

structure code better

Mike L 3 rokov pred
rodič
commit
637b3e7f1a
2 zmenil súbory, kde vykonal 70 pridanie a 72 odobranie
  1. 48 51
      dist/webshot.js
  2. 22 21
      src/webshot.ts

+ 48 - 51
dist/webshot.js

@@ -261,59 +261,56 @@ class Webshot extends CallableInstance {
             });
             if (this.mode === 0) {
                 const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
-                promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay, (_, handle) => {
-                    const extendEntity = (cardImg) => {
-                        var _a, _b;
-                        originTwi.extended_entities = Object.assign(Object.assign({}, originTwi.extended_entities), { media: [
-                                ...(_b = (_a = originTwi.extended_entities) === null || _a === void 0 ? void 0 : _a.media) !== null && _b !== void 0 ? _b : [],
-                                cardImg,
-                            ] });
-                    };
-                    const truncateLongThread = (atId) => {
-                        if (!atId)
-                            return;
-                        logger.info(`thread too long, truncating at tweet ${atId}...`);
-                        truncatedAt = atId;
-                    };
-                    return handle.evaluate(div => {
-                        try {
-                            const selector = '[data-testid="tweet"] :nth-child(2)>:first-child a';
-                            const getProfileUrl = () => (div.querySelector(selector) || { href: '' }).href;
-                            const ownerProfileUrl = getProfileUrl();
-                            const bottom = div;
-                            while (div = div.previousElementSibling) {
-                                if (getProfileUrl() !== ownerProfileUrl || div === bottom.previousElementSibling)
-                                    continue;
-                                const top = document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
-                                if (top > 10)
-                                    return div.querySelector('article a[aria-label]').href.replace(/.*\/status\//, '');
-                            }
+                const extendEntity = (cardImg) => {
+                    var _a, _b;
+                    originTwi.extended_entities = Object.assign(Object.assign({}, originTwi.extended_entities), { media: [
+                            ...(_b = (_a = originTwi.extended_entities) === null || _a === void 0 ? void 0 : _a.media) !== null && _b !== void 0 ? _b : [],
+                            cardImg,
+                        ] });
+                };
+                const truncateLongThread = (atId) => {
+                    if (!atId)
+                        return;
+                    logger.info(`thread too long, truncating at tweet ${atId}...`);
+                    truncatedAt = atId;
+                };
+                promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay, (_, tweetHandle) => tweetHandle.evaluate(div => {
+                    try {
+                        const selector = '[data-testid="tweet"] :nth-child(2)>:first-child a';
+                        const getProfileUrl = () => (div.querySelector(selector) || { href: '' }).href;
+                        const ownerProfileUrl = getProfileUrl();
+                        const bottom = div;
+                        while (div = div.previousElementSibling) {
+                            if (getProfileUrl() !== ownerProfileUrl || div === bottom.previousElementSibling)
+                                continue;
+                            const top = document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
+                            if (top > 10)
+                                return div.querySelector('article a[aria-label]').href.replace(/.*\/status\//, '');
                         }
-                        catch (_a) { }
-                        document.documentElement.scrollTop = 0;
-                    }).then(truncateLongThread)
-                        .then(() => 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) {
-                                const [media_url_https, id_str] = match.slice(1);
-                                return {
-                                    media_url: media_url_https.replace(/^https/, 'http'),
-                                    media_url_https,
-                                    url: '',
-                                    display_url: '',
-                                    expanded_url: '',
-                                    type: 'photo',
-                                    id: Number(id_str),
-                                    id_str,
-                                    sizes: undefined,
-                                };
-                            }
+                    }
+                    catch (_a) { }
+                    document.documentElement.scrollTop = 0;
+                }).then(truncateLongThread), (_, tweetHandle) => tweetHandle.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) {
+                            const [media_url_https, id_str] = match.slice(1);
+                            return {
+                                media_url: media_url_https.replace(/^https/, 'http'),
+                                media_url_https,
+                                url: '',
+                                display_url: '',
+                                expanded_url: '',
+                                type: 'photo',
+                                id: Number(id_str),
+                                id_str,
+                                sizes: undefined,
+                            };
                         }
-                    })).then(cardImg => { if (cardImg)
-                        extendEntity(cardImg); });
-                }))
+                    }
+                }).then(cardImg => { if (cardImg)
+                    extendEntity(cardImg); })))
                     .then(fileurl => {
                     if (fileurl)
                         return koishi_1.Message.Image(fileurl);

+ 22 - 21
src/webshot.ts

@@ -295,25 +295,25 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
       // invoke webshot
       if (this.mode === 0) {
         const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
-        promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay, (
-          _, handle: puppeteer.ElementHandle<HTMLDivElement>
-        ) => {
-          const extendEntity = (cardImg: MediaEntity) => {
-            originTwi.extended_entities = {
-              ...originTwi.extended_entities,
-              media: [
-                ...originTwi.extended_entities?.media ?? [],
-                cardImg,
-              ],
-            };
-          };
-          const truncateLongThread = (atId: string) => {
-            if (!atId) return;
-            logger.info(`thread too long, truncating at tweet ${atId}...`);
-            truncatedAt = atId;
+        const extendEntity = (cardImg: MediaEntity) => {
+          originTwi.extended_entities = {
+            ...originTwi.extended_entities,
+            media: [
+              ...originTwi.extended_entities?.media ?? [],
+              cardImg,
+            ],
           };
+        };
+        const truncateLongThread = (atId: string) => {
+          if (!atId) return;
+          logger.info(`thread too long, truncating at tweet ${atId}...`);
+          truncatedAt = atId;
+        };
+
+        promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay,
+
           // scroll back at least 2 tweets revealing 2nd last tweet by owner in thread, or top of thread
-          return handle.evaluate(div => {
+          (_, tweetHandle: puppeteer.ElementHandle<HTMLDivElement>) => tweetHandle.evaluate(div => {
             try {
               const selector = '[data-testid="tweet"] :nth-child(2)>:first-child a';
               const getProfileUrl = () => (div.querySelector<HTMLAnchorElement>(selector) || {href: ''}).href;
@@ -328,9 +328,10 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
               }
             } catch {/* handle errors like none-found cases */}
             document.documentElement.scrollTop = 0;
-          }).then(truncateLongThread)
+          }).then(truncateLongThread),
+
           // scrape card image from main tweet
-          .then(() => handle.evaluate(div => {
+          (_, tweetHandle: puppeteer.ElementHandle<HTMLDivElement>) => tweetHandle.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'));
@@ -350,8 +351,8 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
                 };
               }
             }
-          })).then(cardImg => { if (cardImg) extendEntity(cardImg); });
-        }))
+          }).then(cardImg => { if (cardImg) extendEntity(cardImg); })
+        ))
           .then(fileurl => {
             if (fileurl) return Message.Image(fileurl);
             return '[截图不可用] ' + author + text;