Explorar el Código

fix webshot browser no-cookie logic

Mike L hace 4 años
padre
commit
37d6caf6bb
Se han modificado 2 ficheros con 18 adiciones y 18 borrados
  1. 8 9
      dist/twitter.js
  2. 10 9
      src/twitter.ts

+ 8 - 9
dist/twitter.js

@@ -134,6 +134,7 @@ const retryOnError = (doWork, onRetry) => new Promise(resolve => {
 });
 class default_1 {
     constructor(opt) {
+        this.webshotCookies = [];
         this.launch = () => {
             this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => this.webshotCookies, () => setTimeout(this.work, this.workInterval * 1000));
         };
@@ -259,15 +260,13 @@ class default_1 {
         this.mode = opt.mode;
         this.wsUrl = opt.wsUrl;
         const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
-        if (fs.existsSync(cookiesFilePath)) {
-            try {
-                this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
-                logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
-            }
-            catch (err) {
-                logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err);
-                logger.warn('cookies will be saved to this file when needed');
-            }
+        try {
+            this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8'));
+            logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
+        }
+        catch (err) {
+            logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message);
+            logger.warn('cookies will be saved to this file when needed');
         }
         exports.browserLogin = (page) => {
             logger.warn('blocked by login dialog, trying to log in manually...');

+ 10 - 9
src/twitter.ts

@@ -163,7 +163,7 @@ export default class {
   private workInterval: number;
   private bot: QQBot;
   private webshotDelay: number;
-  private webshotCookies: Cookies;
+  private webshotCookies: Cookies = [];
   private webshotCookiesLockfile: string;
   private webshot: Webshot;
   private mode: number;
@@ -184,14 +184,15 @@ export default class {
     this.wsUrl = opt.wsUrl;
 
     const cookiesFilePath = path.resolve(this.webshotCookiesLockfile);
-    if (fs.existsSync(cookiesFilePath)) {
-      try {
-        this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8')) as Cookies;
-        logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
-      } catch (err) {
-        logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err);
-        logger.warn('cookies will be saved to this file when needed');
-      }
+    try {
+      this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8')) as Cookies;
+      logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`);
+    } catch (err) {
+      logger.warn(
+        `failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `,
+        (err as Error).message
+      );
+      logger.warn('cookies will be saved to this file when needed');
     }
 
     browserLogin = (page) => {