mirror of
https://github.com/misskey-dev/summerflare.git
synced 2025-07-03 09:30:00 +09:00
chore: init
This commit is contained in:
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@ -0,0 +1,9 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
130
.gitignore
vendored
Normal file
130
.gitignore
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
.temp
|
||||
.cache
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Summerflare
|
||||
|
||||
[**Summ**aly](https://github.com/syuilo/summaly) ov**er** the Cloud**flare**
|
13
package.json
Normal file
13
package.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"packageManager": "pnpm@8.3.1",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20230511.0",
|
||||
"vitest": "^0.31.0",
|
||||
"wrangler": "^2.20.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"hono": "^3.1.8",
|
||||
"html-entities": "^2.3.3",
|
||||
"summaly": "^2.7.0"
|
||||
}
|
||||
}
|
2355
pnpm-lock.yaml
generated
Normal file
2355
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
35
src/index.ts
Normal file
35
src/index.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { Hono } from "hono";
|
||||
import summary from "./summary";
|
||||
|
||||
export interface Env {
|
||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||
// MY_KV_NAMESPACE: KVNamespace;
|
||||
//
|
||||
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
||||
// MY_DURABLE_OBJECT: DurableObjectNamespace;
|
||||
//
|
||||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||
// MY_BUCKET: R2Bucket;
|
||||
//
|
||||
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
|
||||
// MY_SERVICE: Fetcher;
|
||||
}
|
||||
|
||||
const app = new Hono<Env>();
|
||||
|
||||
app.get("/url", async (context) => {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(context.req.query("url")!);
|
||||
} catch (e) {
|
||||
return context.json({ error: "Invalid URL" }, 400);
|
||||
}
|
||||
const response = await fetch(url);
|
||||
const rewriter = new HTMLRewriter();
|
||||
const summarized = summary(url, rewriter);
|
||||
const reader = rewriter.transform(response).body!.getReader();
|
||||
while (!(await reader.read()).done);
|
||||
return context.json(await summarized);
|
||||
});
|
||||
|
||||
export default app;
|
49
src/summary/amazon/description.ts
Normal file
49
src/summary/amazon/description.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { decode } from "html-entities";
|
||||
import clip from "summaly/built/utils/clip";
|
||||
import { BufferedTextHandler, assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getDescription(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 3, // 0-7
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on(
|
||||
"#productDescription",
|
||||
new BufferedTextHandler((text) => {
|
||||
assign(result, 7, decode(text));
|
||||
})
|
||||
);
|
||||
html.on('meta[property="og:description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content && clip(result.content, 300));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
67
src/summary/amazon/image.ts
Normal file
67
src/summary/amazon/image.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { assign, toAbsoluteURL } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getImage(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 4, // 0-15
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on("#landingImage", {
|
||||
element(element) {
|
||||
const content = element.getAttribute("src");
|
||||
if (content) {
|
||||
assign(result, 15, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[property="og:image"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 7, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:image"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 6, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="image_src"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 5, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="apple-touch-icon"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 4, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="apple-touch-icon image_src"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(
|
||||
result.content ? toAbsoluteURL(result.content, url.href) : null
|
||||
);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
72
src/summary/amazon/index.ts
Normal file
72
src/summary/amazon/index.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import cleanupTitle from "summaly/built/utils/cleanup-title";
|
||||
import getCard from "../general/card";
|
||||
import getDescription from "./description";
|
||||
import getFavicon from "../general/favicon";
|
||||
import getImage from "./image";
|
||||
import getPlayerUrlCommon from "../general/playerUrlCommon";
|
||||
import getPlayerUrlGeneral from "../general/playerUrlGeneral";
|
||||
import getPlayerUrlHeight from "../general/playerUrlHeight";
|
||||
import getPlayerUrlWidth from "../general/playerUrlWidth";
|
||||
import getSiteName from "../general/siteName";
|
||||
import getTitle from "./title";
|
||||
import getSensitive from "../general/sensitive";
|
||||
|
||||
export default function amazon(url: URL, html: HTMLRewriter) {
|
||||
const card = getCard(url, html);
|
||||
const title = getTitle(url, html);
|
||||
const image = getImage(url, html);
|
||||
const player = Promise.all([
|
||||
card,
|
||||
getPlayerUrlGeneral(url, html),
|
||||
getPlayerUrlCommon(url, html),
|
||||
getPlayerUrlWidth(url, html),
|
||||
getPlayerUrlHeight(url, html),
|
||||
]).then(([card, general, common, width, height]) => {
|
||||
const url = (card !== "summary_large_image" && general) || common;
|
||||
if (url !== null && width !== null && height !== null) {
|
||||
return {
|
||||
url,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
url: null,
|
||||
width: null,
|
||||
height: null,
|
||||
};
|
||||
}
|
||||
});
|
||||
const description = getDescription(url, html);
|
||||
const siteName = getSiteName(url, html);
|
||||
const favicon = getFavicon(url, html);
|
||||
const sensitive = getSensitive(url, html);
|
||||
|
||||
return Promise.all([
|
||||
title,
|
||||
image,
|
||||
player,
|
||||
description,
|
||||
siteName,
|
||||
favicon,
|
||||
sensitive,
|
||||
]).then(
|
||||
([title, image, player, description, siteName, favicon, sensitive]) => {
|
||||
if (title === null) {
|
||||
return null;
|
||||
}
|
||||
if (siteName !== null) {
|
||||
title = cleanupTitle(title, siteName);
|
||||
}
|
||||
return {
|
||||
title,
|
||||
image,
|
||||
description: title === description ? null : description,
|
||||
player,
|
||||
sitename: siteName,
|
||||
icon: favicon,
|
||||
sensitive,
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
47
src/summary/amazon/title.ts
Normal file
47
src/summary/amazon/title.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { decode } from "html-entities";
|
||||
import clip from "summaly/built/utils/clip";
|
||||
import { BufferedTextHandler, assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getTitle(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 3, // 0-7
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on(
|
||||
"#title",
|
||||
new BufferedTextHandler((text) => {
|
||||
assign(result, 7, decode(text));
|
||||
})
|
||||
);
|
||||
html.on('meta[property="og:title"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:title"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on(
|
||||
"title",
|
||||
new BufferedTextHandler((text) => {
|
||||
assign(result, 1, decode(text));
|
||||
})
|
||||
);
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content && clip(result.content, 100));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
34
src/summary/common.ts
Normal file
34
src/summary/common.ts
Normal file
@ -0,0 +1,34 @@
|
||||
export interface PrioritizedReference<T> {
|
||||
bits: number;
|
||||
priority: number;
|
||||
content: T;
|
||||
}
|
||||
|
||||
export function assign<T>(
|
||||
target: PrioritizedReference<T>,
|
||||
priority: PrioritizedReference<T>["priority"],
|
||||
content: PrioritizedReference<T>["content"]
|
||||
): void {
|
||||
if (target.priority <= priority) {
|
||||
target.priority = priority;
|
||||
target.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
export function toAbsoluteURL(url: string, base: string) {
|
||||
if (/^https?:\/\//.test(url)) {
|
||||
return url;
|
||||
} else {
|
||||
return new URL(url, base).href;
|
||||
}
|
||||
}
|
||||
|
||||
export class BufferedTextHandler {
|
||||
private buffer = "";
|
||||
|
||||
constructor(private readonly callback: (text: string) => void) {}
|
||||
|
||||
text(text: Text) {
|
||||
this.callback((this.buffer += text.text));
|
||||
}
|
||||
}
|
25
src/summary/general/card.ts
Normal file
25
src/summary/general/card.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getCard(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 1, // 0-1
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="twitter:card"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
42
src/summary/general/description.ts
Normal file
42
src/summary/general/description.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import clip from "summaly/built/utils/clip";
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getDescription(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="og:description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="description"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content && clip(result.content, 300));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
33
src/summary/general/favicon.ts
Normal file
33
src/summary/general/favicon.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { assign, toAbsoluteURL } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getFavicon(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: "/favicon.ico",
|
||||
};
|
||||
html.on('link[rel="shortcut icon"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="icon"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(toAbsoluteURL(result.content, url.href));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
59
src/summary/general/image.ts
Normal file
59
src/summary/general/image.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { assign, toAbsoluteURL } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getImage(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 3, // 0-7
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="og:image"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 7, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:image"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 6, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="image_src"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 5, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="apple-touch-icon"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 4, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('link[rel="apple-touch-icon image_src"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("href");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(
|
||||
result.content ? toAbsoluteURL(result.content, url.href) : null
|
||||
);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
72
src/summary/general/index.ts
Normal file
72
src/summary/general/index.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import cleanupTitle from "summaly/built/utils/cleanup-title";
|
||||
import getCard from "./card";
|
||||
import getDescription from "./description";
|
||||
import getFavicon from "./favicon";
|
||||
import getImage from "./image";
|
||||
import getPlayerUrlCommon from "./playerUrlCommon";
|
||||
import getPlayerUrlGeneral from "./playerUrlGeneral";
|
||||
import getPlayerUrlHeight from "./playerUrlHeight";
|
||||
import getPlayerUrlWidth from "./playerUrlWidth";
|
||||
import getSiteName from "./siteName";
|
||||
import getTitle from "./title";
|
||||
import getSensitive from "./sensitive";
|
||||
|
||||
export default function general(url: URL, html: HTMLRewriter) {
|
||||
const card = getCard(url, html);
|
||||
const title = getTitle(url, html);
|
||||
const image = getImage(url, html);
|
||||
const player = Promise.all([
|
||||
card,
|
||||
getPlayerUrlGeneral(url, html),
|
||||
getPlayerUrlCommon(url, html),
|
||||
getPlayerUrlWidth(url, html),
|
||||
getPlayerUrlHeight(url, html),
|
||||
]).then(([card, general, common, width, height]) => {
|
||||
const url = (card !== "summary_large_image" && general) || common;
|
||||
if (url !== null && width !== null && height !== null) {
|
||||
return {
|
||||
url,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
url: null,
|
||||
width: null,
|
||||
height: null,
|
||||
};
|
||||
}
|
||||
});
|
||||
const description = getDescription(url, html);
|
||||
const siteName = getSiteName(url, html);
|
||||
const favicon = getFavicon(url, html);
|
||||
const sensitive = getSensitive(url, html);
|
||||
|
||||
return Promise.all([
|
||||
title,
|
||||
image,
|
||||
player,
|
||||
description,
|
||||
siteName,
|
||||
favicon,
|
||||
sensitive,
|
||||
]).then(
|
||||
([title, image, player, description, siteName, favicon, sensitive]) => {
|
||||
if (title === null) {
|
||||
return null;
|
||||
}
|
||||
if (siteName !== null) {
|
||||
title = cleanupTitle(title, siteName);
|
||||
}
|
||||
return {
|
||||
title,
|
||||
image,
|
||||
description: title === description ? null : description,
|
||||
player,
|
||||
sitename: siteName,
|
||||
icon: favicon,
|
||||
sensitive,
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
43
src/summary/general/playerUrlCommon.ts
Normal file
43
src/summary/general/playerUrlCommon.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { assign, toAbsoluteURL } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getPlayerUrlCommon(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="og:video"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[property="og:video:secure_url"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[property="og:video:url"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(
|
||||
result.content ? toAbsoluteURL(result.content, url.href) : null
|
||||
);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
35
src/summary/general/playerUrlGeneral.ts
Normal file
35
src/summary/general/playerUrlGeneral.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { assign, toAbsoluteURL } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getPlayerUrlGeneral(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="twitter:player"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:player"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(
|
||||
result.content ? toAbsoluteURL(result.content, url.href) : null
|
||||
);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
42
src/summary/general/playerUrlHeight.ts
Normal file
42
src/summary/general/playerUrlHeight.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getPlayerUrlHeight(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="twitter:player:height"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:player:height"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[property="og:video:height"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<number | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
const content = parseInt(result.content!, 10);
|
||||
resolve(Number.isNaN(content) ? null : content);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
42
src/summary/general/playerUrlWidth.ts
Normal file
42
src/summary/general/playerUrlWidth.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getPlayerUrlWidth(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="twitter:player:width"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:player:width"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[property="og:video:width"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 1, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<number | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
const content = parseInt(result.content!, 10);
|
||||
resolve(Number.isNaN(content) ? null : content);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
22
src/summary/general/sensitive.ts
Normal file
22
src/summary/general/sensitive.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getSensitive(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<boolean> = {
|
||||
bits: 1, // 0-1
|
||||
priority: 0,
|
||||
content: false,
|
||||
};
|
||||
html.on('.tweet[data-possibly-sensitive="true"]', {
|
||||
element() {
|
||||
assign(result, 1, true);
|
||||
},
|
||||
});
|
||||
return new Promise<boolean>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
33
src/summary/general/siteName.ts
Normal file
33
src/summary/general/siteName.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getSiteName(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: url.hostname,
|
||||
};
|
||||
html.on('meta[property="og:site_name"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="application-name"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
41
src/summary/general/title.ts
Normal file
41
src/summary/general/title.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { decode } from "html-entities";
|
||||
import clip from "summaly/built/utils/clip";
|
||||
import { BufferedTextHandler, assign } from "../common";
|
||||
import type { PrioritizedReference } from "../common";
|
||||
|
||||
export default function getTitle(url: URL, html: HTMLRewriter) {
|
||||
const result: PrioritizedReference<string | null> = {
|
||||
bits: 2, // 0-3
|
||||
priority: 0,
|
||||
content: null,
|
||||
};
|
||||
html.on('meta[property="og:title"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 3, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on('meta[name="twitter:title"]', {
|
||||
element(element) {
|
||||
const content = element.getAttribute("content");
|
||||
if (content) {
|
||||
assign(result, 2, content);
|
||||
}
|
||||
},
|
||||
});
|
||||
html.on(
|
||||
"title",
|
||||
new BufferedTextHandler((text) => {
|
||||
assign(result, 1, decode(text));
|
||||
})
|
||||
);
|
||||
return new Promise<string | null>((resolve) => {
|
||||
html.onDocument({
|
||||
end() {
|
||||
resolve(result.content && clip(result.content, 100));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
28
src/summary/index.ts
Normal file
28
src/summary/index.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import amazon from "./amazon";
|
||||
import general from "./general";
|
||||
import wikipedia from "./wikipedia";
|
||||
|
||||
export default function summary(url: URL, html: HTMLRewriter) {
|
||||
if (
|
||||
url.hostname === "www.amazon.com" ||
|
||||
url.hostname === "www.amazon.co.jp" ||
|
||||
url.hostname === "www.amazon.ca" ||
|
||||
url.hostname === "www.amazon.com.br" ||
|
||||
url.hostname === "www.amazon.com.mx" ||
|
||||
url.hostname === "www.amazon.co.uk" ||
|
||||
url.hostname === "www.amazon.de" ||
|
||||
url.hostname === "www.amazon.fr" ||
|
||||
url.hostname === "www.amazon.it" ||
|
||||
url.hostname === "www.amazon.es" ||
|
||||
url.hostname === "www.amazon.nl" ||
|
||||
url.hostname === "www.amazon.cn" ||
|
||||
url.hostname === "www.amazon.in" ||
|
||||
url.hostname === "www.amazon.au"
|
||||
) {
|
||||
return amazon(url, html);
|
||||
}
|
||||
if (`.${url.hostname}`.endsWith(".wikipedia.org")) {
|
||||
return wikipedia(url, html);
|
||||
}
|
||||
return general(url, html);
|
||||
}
|
23
src/summary/wikipedia/index.ts
Normal file
23
src/summary/wikipedia/index.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import clip from "summaly/built/utils/clip";
|
||||
|
||||
export default async function wikipedia(url: URL, html: HTMLRewriter) {
|
||||
const lang = url.hostname.split(".")[0];
|
||||
const title = url.pathname.split("/")[2];
|
||||
const response = await fetch(
|
||||
`https://${lang}.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=${title}`
|
||||
);
|
||||
const json = await response.json<any>();
|
||||
const info = json.query.pages[Object.keys(json.query.pages)[0]];
|
||||
return {
|
||||
title: info.title,
|
||||
icon: "https://wikipedia.org/static/favicon/wikipedia.ico",
|
||||
description: clip(info.extract, 300),
|
||||
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
|
||||
player: {
|
||||
url: null,
|
||||
width: null,
|
||||
height: null,
|
||||
},
|
||||
sitename: "Wikipedia",
|
||||
};
|
||||
}
|
105
tsconfig.json
Normal file
105
tsconfig.json
Normal file
@ -0,0 +1,105 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||
|
||||
/* Language and Environment */
|
||||
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
||||
"lib": [
|
||||
"es2021"
|
||||
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
|
||||
"jsx": "react" /* Specify what JSX code is generated. */,
|
||||
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
||||
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
|
||||
/* Modules */
|
||||
"module": "es2022" /* Specify what module code is generated. */,
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||
"types": [
|
||||
"@cloudflare/workers-types"
|
||||
] /* Specify type package names to be included without being referenced in a source file. */,
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
"resolveJsonModule": true /* Enable importing .json files */,
|
||||
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
||||
|
||||
/* JavaScript Support */
|
||||
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
|
||||
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
||||
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
||||
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
||||
// "removeComments": true, /* Disable emitting comments. */
|
||||
"noEmit": true /* Disable emitting files from a compilation. */,
|
||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||
|
||||
/* Interop Constraints */
|
||||
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
|
||||
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
|
||||
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
||||
|
||||
/* Type Checking */
|
||||
"strict": true /* Enable all strict type-checking options. */,
|
||||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
||||
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
||||
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
||||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
||||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||
|
||||
/* Completeness */
|
||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
}
|
||||
}
|
3
wrangler.toml
Normal file
3
wrangler.toml
Normal file
@ -0,0 +1,3 @@
|
||||
name = "summerflare"
|
||||
main = "src/index.ts"
|
||||
compatibility_date = "2023-05-13"
|
Reference in New Issue
Block a user