Skip to content

Commit

Permalink
WIP(backgroup-script): extension now sends book information to notion…
Browse files Browse the repository at this point in the history
… after open the popup
  • Loading branch information
HenryC-3 committed Mar 2, 2023
1 parent 28ad6d1 commit 8a4eece
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@notionhq/client": "^2.2.3",
"blob-util": "^2.0.2",
"cheerio": "1.0.0-rc.12",
"vue": "^3.2.45",
Expand Down
87 changes: 87 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 103 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -1 +1,103 @@
console.log("this is a background script");
import { Book } from "./types";
// import { dataURLToBlob } from "blob-util";
import { Client } from "@notionhq/client";

const { VITE_NOTION_AUTH_TOKEN: token, VITE_NOTION_DB_ID: db } = import.meta
.env;
const notion = new Client({ auth: token });

chrome.runtime.onMessage.addListener((message: { book: Book }) => {
if (message) {
// const cover = dataURLToBlob(message.book.cover);
addBook(message.book);
}
});

async function addBook(book: Book) {
try {
const response = await notion.pages.create({
parent: { database_id: db },
properties: {
title: {
title: [
{
text: {
content: book.title,
},
},
],
},
Author: {
rich_text: [
{
text: {
content: book.author,
},
},
],
},
Publisher: {
rich_text: [
{
text: {
content: book.publisher,
},
},
],
},
Producer: {
rich_text: [
{
text: {
content: book.producer,
},
},
],
},
Subtitle: {
rich_text: [
{
text: {
content: book.subtitle,
},
},
],
},
PublishDate: {
date: {
start: book.publishDate,
},
},
PageCount: {
number: book.pageCount,
},
Price: {
number: book.price,
},
ISBN: {
number: book.ISBN,
},
Rating: {
number: book.rating,
},
RatingCount: {
number: book.ratingCount,
},
Cover: {
files: [
{
name: `${book.title}-${book.ISBN}`,
external: {
url: book.cover,
},
},
],
},
},
});
console.log(response);
console.log("Success! Entry added.");
} catch (error) {
console.error(error);
}
}
1 change: 1 addition & 0 deletions src/manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const [major, minor, patch, label = "0"] = version
.split(/[.-]/);

export default defineManifest(async (env) => ({
host_permissions: ["https://api.notion.com/v1/*"],
content_scripts: [
{
matches: ["https://book.douban.com/**"],
Expand Down

0 comments on commit 8a4eece

Please sign in to comment.