Skip to content

Commit 8a4eece

Browse files
committed
WIP(backgroup-script): extension now sends book information to notion after open the popup
1 parent 28ad6d1 commit 8a4eece

File tree

4 files changed

+192
-1
lines changed

4 files changed

+192
-1
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12+
"@notionhq/client": "^2.2.3",
1213
"blob-util": "^2.0.2",
1314
"cheerio": "1.0.0-rc.12",
1415
"vue": "^3.2.45",

Diff for: pnpm-lock.yaml

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/background.ts

+103-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,103 @@
1-
console.log("this is a background script");
1+
import { Book } from "./types";
2+
// import { dataURLToBlob } from "blob-util";
3+
import { Client } from "@notionhq/client";
4+
5+
const { VITE_NOTION_AUTH_TOKEN: token, VITE_NOTION_DB_ID: db } = import.meta
6+
.env;
7+
const notion = new Client({ auth: token });
8+
9+
chrome.runtime.onMessage.addListener((message: { book: Book }) => {
10+
if (message) {
11+
// const cover = dataURLToBlob(message.book.cover);
12+
addBook(message.book);
13+
}
14+
});
15+
16+
async function addBook(book: Book) {
17+
try {
18+
const response = await notion.pages.create({
19+
parent: { database_id: db },
20+
properties: {
21+
title: {
22+
title: [
23+
{
24+
text: {
25+
content: book.title,
26+
},
27+
},
28+
],
29+
},
30+
Author: {
31+
rich_text: [
32+
{
33+
text: {
34+
content: book.author,
35+
},
36+
},
37+
],
38+
},
39+
Publisher: {
40+
rich_text: [
41+
{
42+
text: {
43+
content: book.publisher,
44+
},
45+
},
46+
],
47+
},
48+
Producer: {
49+
rich_text: [
50+
{
51+
text: {
52+
content: book.producer,
53+
},
54+
},
55+
],
56+
},
57+
Subtitle: {
58+
rich_text: [
59+
{
60+
text: {
61+
content: book.subtitle,
62+
},
63+
},
64+
],
65+
},
66+
PublishDate: {
67+
date: {
68+
start: book.publishDate,
69+
},
70+
},
71+
PageCount: {
72+
number: book.pageCount,
73+
},
74+
Price: {
75+
number: book.price,
76+
},
77+
ISBN: {
78+
number: book.ISBN,
79+
},
80+
Rating: {
81+
number: book.rating,
82+
},
83+
RatingCount: {
84+
number: book.ratingCount,
85+
},
86+
Cover: {
87+
files: [
88+
{
89+
name: `${book.title}-${book.ISBN}`,
90+
external: {
91+
url: book.cover,
92+
},
93+
},
94+
],
95+
},
96+
},
97+
});
98+
console.log(response);
99+
console.log("Success! Entry added.");
100+
} catch (error) {
101+
console.error(error);
102+
}
103+
}

Diff for: src/manifest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const [major, minor, patch, label = "0"] = version
1010
.split(/[.-]/);
1111

1212
export default defineManifest(async (env) => ({
13+
host_permissions: ["https://api.notion.com/v1/*"],
1314
content_scripts: [
1415
{
1516
matches: ["https://book.douban.com/**"],

0 commit comments

Comments
 (0)