Skip to content

Commit

Permalink
fix: avoid "Sorry, we’re unable to display this type of content."
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Feb 2, 2022
1 parent d4b75a8 commit 1281f1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -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}\"",
Expand Down Expand Up @@ -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"
}
}
10 changes: 6 additions & 4 deletions src/parse.ts
@@ -1,5 +1,3 @@
import { DOMWindow } from "jsdom";

const assertOk = (ok: unknown, message: string) => {
if (!ok) {
throw new Error(message);
Expand All @@ -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<HTMLDivElement>("#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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/node.ts → 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: ", "");
Expand All @@ -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));
})();

0 comments on commit 1281f1b

Please sign in to comment.