From 0d280b26fd5bf7deb60b191106425a64dd4e33e3 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Sun, 30 Jun 2024 23:43:40 +0900 Subject: [PATCH] no-headless --- browser.ts | 29 +++++++++++++---------------- main.ts | 27 +++++++++++++++++---------- package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/browser.ts b/browser.ts index 18f0eef..2bba1bb 100644 --- a/browser.ts +++ b/browser.ts @@ -12,6 +12,16 @@ export type ImageFile = { height?: number; }; +export type Browser = { + loadBrowserContext(platform: TPlatform): Promise; + saveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise; + newContext(): Promise; + close(): Promise; + drawImage(pageOrFrame: PageOrFrame, imageFile: ImageFile): Promise; +}; + +export type BrowserContext = Playwright.BrowserContext; + async function drawImage(imageFile: ImageFile): Promise { const canvas = Object.assign(document.createElement("canvas"), { width: imageFile.width, @@ -73,25 +83,12 @@ async function dataUrlToBlob(dataUrl: string): Promise { return await res.blob(); } -export type Browser = { - loadBrowserContext(platform: TPlatform): Promise; - saveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise; - newContext(): Promise; - close(): Promise; - drawImage( - pageOrFrame: Playwright.Page | Playwright.Frame, - imageFile: ImageFile, - ): Promise; -}; - -export type BrowserContext = Playwright.BrowserContext; - export async function createBrowser({ db, - headless = true, + headless, }: { db: Database; - headless?: boolean; + headless: boolean; }): Promise { const { userAgent } = devices["Desktop Chrome"]; const browser = await chromium.launch({ @@ -129,7 +126,7 @@ export async function createBrowser({ close: () => browser.close(), async drawImage( - pageOrFrame: Playwright.Page | Playwright.Frame, + pageOrFrame: PageOrFrame, imageFile: ImageFile, ): Promise { if (Array.isArray(imageFile.blocks) && imageFile.blocks.length > 0) { diff --git a/main.ts b/main.ts index 49623a9..1285441 100644 --- a/main.ts +++ b/main.ts @@ -4,9 +4,9 @@ import path from "node:path"; import util from "node:util"; import { createBrowser } from "./browser"; import { createDatabase } from "./database"; -import { type Book, createLibrary } from "./library"; -import { type TPlatform, createPlatform, platforms } from "./platform"; +import { createLibrary, type Book } from "./library"; import * as pkg from "./package.json"; +import { createPlatform, platforms, type TPlatform } from "./platform"; const options = { db: { @@ -30,6 +30,10 @@ const options = { return ` (default: ${this.default})`; }, }, + "no-headless": { + type: "boolean", + default: false, + }, login: { type: "string", toString() { @@ -53,8 +57,9 @@ const options = { return [...Object.keys(platforms)].join("|"); }, async run() { - const db = await createDatabase(args.values.db!); - const browser = await createBrowser({ db }); + const db = await createDatabase(args.values.db!), + headless = !args.values["no-headless"]; + const browser = await createBrowser({ db, headless }); const platform = createPlatform({ platform: args.values.logout as TPlatform, db, @@ -128,9 +133,10 @@ const options = { return [...Object.keys(platforms)].join("|"); }, async run() { - const db = await createDatabase(args.values.db!); - const library = createLibrary(db); - const browser = await createBrowser({ db }); + const db = await createDatabase(args.values.db!), + library = createLibrary(db), + headless = !args.values["no-headless"]; + const browser = await createBrowser({ db, headless }); const platform = createPlatform({ platform: args.values.pull as TPlatform, db, @@ -150,8 +156,9 @@ const options = { return `all|`; }, async run() { - const db = await createDatabase(args.values.db!); - const library = createLibrary(db); + const db = await createDatabase(args.values.db!), + library = createLibrary(db), + headless = !args.values["no-headless"]; const books: Array = []; if (args.values.download === "all") { @@ -171,7 +178,7 @@ const options = { } for (const book of books) { - const browser = await createBrowser({ db }); + const browser = await createBrowser({ db, headless }); const platform = createPlatform({ platform: book.platform, db, diff --git a/package-lock.json b/package-lock.json index 3aa3455..bd78de8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fogtype/gadl", - "version": "1.5.1", + "version": "1.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@fogtype/gadl", - "version": "1.5.1", + "version": "1.6.0", "license": "AGPL-3.0", "dependencies": { "fflate": "^0.8.1", diff --git a/package.json b/package.json index 9ab2a7a..6aae56f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fogtype/gadl", - "version": "1.5.1", + "version": "1.6.0", "license": "AGPL-3.0", "type": "module", "bin": "bin/run.js",