-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
83 lines (70 loc) · 3.31 KB
/
popup.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
81
82
83
// Entrypoint file. Runs first when the extension is clicked
const getCurrentTabURL = async () => {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab.url;
};
const bind_add_paper_to_notion_button = async (notionInstance) => {
document.getElementById("arxiv_button").addEventListener("click", async () => {
const url = await getCurrentTabURL();
try {
document.getElementById("arxiv_button").classList.add("is-loading");
const metadata = await getMetadataFromArxivURL(url);
const token = notionInstance.authToken;
const databases = notionInstance.databases;
const selectedDatabaseName = document.getElementById("databases_select").value;
let selectedDatabase = databases[0];
for (const db of databases) {
console.log(db);
if (db["title"][0]["plain_text"] == selectedDatabaseName) {
selectedDatabase = db;
break;
}
}
console.log(token, metadata, databases);
const properties = notionInstance.getDatabaseProperties(selectedDatabase);
await notionInstance.writePaperMetadataToDatabase(properties, metadata, token);
document.getElementById("arxiv_button").classList.remove("is-loading");
document.getElementById("arxiv_button").innerHTML = '<i class="fas fa-check-circle"></i>';
setTimeout(() => {
document.getElementById("arxiv_button").innerHTML = "Add Paper";
}, 700);
} catch (e) {
console.log(e);
window.alert("This page is not an arxiv page");
}
});
};
const bind_authenticate_notion_button = () => {
document.getElementById("notion_auth_button").addEventListener("click", function () {
var newURL =
"https://api.notion.com/v1/oauth/authorize?client_id=951744be-9fe3-4fc4-9901-7877db5a67f0&redirect_uri=https%3A%2F%2Farxivtonotion.github.io%2F&response_type=code";
chrome.tabs.create({ url: newURL });
});
};
document.addEventListener("DOMContentLoaded", async () => {
bind_authenticate_notion_button();
const url = await getCurrentTabURL();
if (isArxivURL(url)) {
document.getElementById("valid_arxiv_url_tag").style.display = "";
} else {
document.getElementById("invalid_arxiv_url_tag").style.display = "";
}
try {
let n = new Notion();
await n.init();
document.getElementById("notion_authenticated_tag").style.display = "";
document.getElementById("authenticating_loading_button").style.display = "none";
bind_add_paper_to_notion_button(n);
console.log(n.databases);
let databasesSelect = document.getElementById("databases_select");
databasesSelect.innerHTML = "";
n.databases.forEach((db) => {
let node = document.createElement("option");
node.appendChild(document.createTextNode(db["title"][0]["plain_text"]));
databasesSelect.appendChild(node);
});
} catch (err) {
document.getElementById("notion_unauthenticated_tag").style.display = "";
}
});