forked from vincent-herlemont/save-to-notion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
80 lines (71 loc) · 1.84 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
async function getApiKey() {
let gettingItem = browser.storage.local.get('notion_api_key');
let notion_api_key = await gettingItem.then((res) => res.notion_api_key);
console.log("notion_api_key",notion_api_key);
return notion_api_key;
}
async function list_databases(sender) {
let notion_api_key = await getApiKey();
let dataResponse = await fetch('https://api.notion.com/v1/databases',{
headers: {
'Authorization': 'Bearer '+notion_api_key,
'Notion-Version': '2021-05-13'
},
referrerPolicy: "no-referrer-when-downgrade",
mode: "same-origin",
}).then(response => response.json());
console.log("data",dataResponse);
await browser.tabs.sendMessage(sender.tab.id,{
type: 'stn_list_databases',
data: dataResponse,
})
console.log("OK !");
}
browser.runtime.onMessage.addListener((data, sender) => {
if (data.type === 'stn_list_databases') {
return list_databases(sender)
}
return false;
});
async function save_to_databases(data,sender) {
console.log("data",data,sender);
let notion_api_key = await getApiKey();
for (id of data.ids) {
let dataResponse = await fetch('https://api.notion.com/v1/pages',{
method: 'POST',
headers: {
'Authorization': 'Bearer '+notion_api_key,
'Notion-Version': '2021-05-13',
'Content-Type': 'application/json'
},
referrerPolicy: "no-referrer-when-downgrade",
mode: "same-origin",
body: JSON.stringify({
parent: {
database_id: id,
},
properties: {
Name: {
title: [
{
text: {
content:data.data.title,
}
}
]
},
Url: {
url: data.data.url
}
}
})
}).then(response => response.json());
console.log(dataResponse);
}
}
browser.runtime.onMessage.addListener((data, sender) => {
if (data.type === 'stn_save_to_databases') {
return save_to_databases(data,sender)
}
return false;
});