From 1281f1b614f6d1bd5bb579778ec087df59ce6054 Mon Sep 17 00:00:00 2001 From: azu Date: Wed, 2 Feb 2022 21:13:11 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20avoid=20"Sorry,=20we=E2=80=99re=20unable?= =?UTF-8?q?=20to=20display=20this=20type=20of=20content."?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 6 +++--- src/parse.ts | 10 ++++++---- {src => test}/node.ts | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) rename {src => test}/node.ts (78%) diff --git a/package.json b/package.json index e8394cb..994a276 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "src/" ], "scripts": { - "main": "ts-node src/node.ts", + "main": "ts-node test/node.ts", "build": "tsc -p . && tsc -p ./tsconfig.module.json", "clean": "rimraf lib/ module/", "format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"", @@ -57,10 +57,10 @@ "rimraf": "^3.0.2", "ts-node": "^10.4.0", "ts-node-test-register": "^10.0.0", - "typescript": "^4.5.4" + "typescript": "^4.5.4", + "jsdom": "^19.0.0" }, "dependencies": { - "jsdom": "^19.0.0", "markdown-function": "^1.1.1" } } diff --git a/src/parse.ts b/src/parse.ts index 124d84a..74d508d 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,5 +1,3 @@ -import { DOMWindow } from "jsdom"; - const assertOk = (ok: unknown, message: string) => { if (!ok) { throw new Error(message); @@ -18,7 +16,7 @@ export type ParseResult = { url: string; annotations: Annotation[]; }; -export const parsePage = (window: DOMWindow) => { +export const parsePage = (window: Window) => { const pages = window.document.querySelectorAll("#a-page"); const page = pages[pages.length - 1]; // select child #a-page if nested #a-page const title = page.querySelector("h3.kp-notebook-metadata") as HTMLHeadingElement; @@ -32,7 +30,11 @@ export const parsePage = (window: DOMWindow) => { assertOk(annotationNodes.length > 0, "annotations not found"); const annotations: Annotation[] = Array.from(annotationNodes) .filter((annotation) => { - return annotation.getAttribute("id") !== "empty-annotations-pane"; + return ( + annotation.getAttribute("id") !== "empty-annotations-pane" && + // Sorry, we’re unable to display this type of content. + annotation.querySelector(".kp-notebook-highlight-empty-text") === null + ); }) .map((annotation) => { const noteNode = annotation.querySelector(`[id="note"]`) as HTMLSpanElement; diff --git a/src/node.ts b/test/node.ts similarity index 78% rename from src/node.ts rename to test/node.ts index 5ee75e8..9ef1e93 100644 --- a/src/node.ts +++ b/test/node.ts @@ -1,7 +1,7 @@ import { JSDOM, CookieJar } from "jsdom"; import * as fs from "fs/promises"; import path from "path"; -import { parsePage } from "./parse"; +import { parsePage } from "../src"; (async function () { const cookieString = (await fs.readFile(path.join(__dirname, "../.cookie"), "utf-8")).replace("^Cookie: ", ""); @@ -10,8 +10,8 @@ import { parsePage } from "./parse"; for (const cookie of cookies) { await cookieJar.setCookie(cookie, "https://read.amazon.co.jp"); } - const { window } = await JSDOM.fromURL("https://read.amazon.co.jp/notebook?asin=B07TWP8JPL&contentLimitState=&", { + const { window } = await JSDOM.fromURL("https://read.amazon.co.jp/notebook?asin=B0957B1LD5&contentLimitState=&", { cookieJar }); - console.log(parsePage(window)); + console.log(parsePage(window as any as Window)); })();