no-headless

This commit is contained in:
Nebel 2024-06-30 23:43:40 +09:00
parent 3dfe483973
commit 0d280b26fd
Signed by: nebel
GPG key ID: 79807D08C6EF6460
4 changed files with 33 additions and 29 deletions

View file

@ -12,6 +12,16 @@ export type ImageFile = {
height?: number; height?: number;
}; };
export type Browser = {
loadBrowserContext(platform: TPlatform): Promise<Playwright.BrowserContext>;
saveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise<void>;
newContext(): Promise<Playwright.BrowserContext>;
close(): Promise<void>;
drawImage(pageOrFrame: PageOrFrame, imageFile: ImageFile): Promise<Blob>;
};
export type BrowserContext = Playwright.BrowserContext;
async function drawImage(imageFile: ImageFile): Promise<string> { async function drawImage(imageFile: ImageFile): Promise<string> {
const canvas = Object.assign(document.createElement("canvas"), { const canvas = Object.assign(document.createElement("canvas"), {
width: imageFile.width, width: imageFile.width,
@ -73,25 +83,12 @@ async function dataUrlToBlob(dataUrl: string): Promise<Blob> {
return await res.blob(); return await res.blob();
} }
export type Browser = {
loadBrowserContext(platform: TPlatform): Promise<Playwright.BrowserContext>;
saveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise<void>;
newContext(): Promise<Playwright.BrowserContext>;
close(): Promise<void>;
drawImage(
pageOrFrame: Playwright.Page | Playwright.Frame,
imageFile: ImageFile,
): Promise<Blob>;
};
export type BrowserContext = Playwright.BrowserContext;
export async function createBrowser({ export async function createBrowser({
db, db,
headless = true, headless,
}: { }: {
db: Database; db: Database;
headless?: boolean; headless: boolean;
}): Promise<Browser> { }): Promise<Browser> {
const { userAgent } = devices["Desktop Chrome"]; const { userAgent } = devices["Desktop Chrome"];
const browser = await chromium.launch({ const browser = await chromium.launch({
@ -129,7 +126,7 @@ export async function createBrowser({
close: () => browser.close(), close: () => browser.close(),
async drawImage( async drawImage(
pageOrFrame: Playwright.Page | Playwright.Frame, pageOrFrame: PageOrFrame,
imageFile: ImageFile, imageFile: ImageFile,
): Promise<Blob> { ): Promise<Blob> {
if (Array.isArray(imageFile.blocks) && imageFile.blocks.length > 0) { if (Array.isArray(imageFile.blocks) && imageFile.blocks.length > 0) {

27
main.ts
View file

@ -4,9 +4,9 @@ import path from "node:path";
import util from "node:util"; import util from "node:util";
import { createBrowser } from "./browser"; import { createBrowser } from "./browser";
import { createDatabase } from "./database"; import { createDatabase } from "./database";
import { type Book, createLibrary } from "./library"; import { createLibrary, type Book } from "./library";
import { type TPlatform, createPlatform, platforms } from "./platform";
import * as pkg from "./package.json"; import * as pkg from "./package.json";
import { createPlatform, platforms, type TPlatform } from "./platform";
const options = { const options = {
db: { db: {
@ -30,6 +30,10 @@ const options = {
return `<output_authors_limit> (default: ${this.default})`; return `<output_authors_limit> (default: ${this.default})`;
}, },
}, },
"no-headless": {
type: "boolean",
default: false,
},
login: { login: {
type: "string", type: "string",
toString() { toString() {
@ -53,8 +57,9 @@ const options = {
return [...Object.keys(platforms)].join("|"); return [...Object.keys(platforms)].join("|");
}, },
async run() { async run() {
const db = await createDatabase(args.values.db!); const db = await createDatabase(args.values.db!),
const browser = await createBrowser({ db }); headless = !args.values["no-headless"];
const browser = await createBrowser({ db, headless });
const platform = createPlatform({ const platform = createPlatform({
platform: args.values.logout as TPlatform, platform: args.values.logout as TPlatform,
db, db,
@ -128,9 +133,10 @@ const options = {
return [...Object.keys(platforms)].join("|"); return [...Object.keys(platforms)].join("|");
}, },
async run() { async run() {
const db = await createDatabase(args.values.db!); const db = await createDatabase(args.values.db!),
const library = createLibrary(db); library = createLibrary(db),
const browser = await createBrowser({ db }); headless = !args.values["no-headless"];
const browser = await createBrowser({ db, headless });
const platform = createPlatform({ const platform = createPlatform({
platform: args.values.pull as TPlatform, platform: args.values.pull as TPlatform,
db, db,
@ -150,8 +156,9 @@ const options = {
return `all|<reader_url_or_id>`; return `all|<reader_url_or_id>`;
}, },
async run() { async run() {
const db = await createDatabase(args.values.db!); const db = await createDatabase(args.values.db!),
const library = createLibrary(db); library = createLibrary(db),
headless = !args.values["no-headless"];
const books: Array<Book> = []; const books: Array<Book> = [];
if (args.values.download === "all") { if (args.values.download === "all") {
@ -171,7 +178,7 @@ const options = {
} }
for (const book of books) { for (const book of books) {
const browser = await createBrowser({ db }); const browser = await createBrowser({ db, headless });
const platform = createPlatform({ const platform = createPlatform({
platform: book.platform, platform: book.platform,
db, db,

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.5.1", "version": "1.6.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.5.1", "version": "1.6.0",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"fflate": "^0.8.1", "fflate": "^0.8.1",

View file

@ -1,6 +1,6 @@
{ {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.5.1", "version": "1.6.0",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"type": "module", "type": "module",
"bin": "bin/run.js", "bin": "bin/run.js",