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;
};
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> {
const canvas = Object.assign(document.createElement("canvas"), {
width: imageFile.width,
@ -73,25 +83,12 @@ async function dataUrlToBlob(dataUrl: string): Promise<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({
db,
headless = true,
headless,
}: {
db: Database;
headless?: boolean;
headless: boolean;
}): Promise<Browser> {
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<Blob> {
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 { 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 `<output_authors_limit> (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|<reader_url_or_id>`;
},
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<Book> = [];
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,

4
package-lock.json generated
View file

@ -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",

View file

@ -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",