-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
68 lines (55 loc) · 2.25 KB
/
content.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
const storage = chrome.storage.sync;
function getMeta(name, context = document) {
return Array.from(context.getElementsByTagName('meta'))
.filter(tag => tag.name == name)
.shift();
}
function getElement(name, context = document) {
return context.getElementsByClassName(name)[0];
}
function getNow() {
let today = new Date();
return `${today.getFullYear()}/${(today.getMonth()+1).toString().padStart(2, '0')}/${(today.getDate()).toString().padStart(2, '0')}`;
}
const arxiv_id = getMeta('citation_arxiv_id').content;
storage.get([arxiv_id], items => {
const target = getElement('full-text').getElementsByTagName('UL')[0];
const button = document.createElement('a');
const wrapper = document.createElement('li');
button.href = '#';
button.onclick = () => {
const title = getElement('title').innerText;
const authors = Array.from(getElement('authors').getElementsByTagName('a'))
.map(tag => tag.innerText);
const abstract = getElement('abstract').innerText;
const subject = getElement('subjects').innerText;
const category = getElement('subheader').innerText.split(' > ');
const pdf_url = getMeta('citation_pdf_url').content;
const upload_date = getMeta('citation_date').content;
const save_date = getNow();
storage.set({
[arxiv_id]: JSON.stringify({
title, authors, abstract, subject, category,
arxiv_id, pdf_url, upload_date, save_date,
})
}, () => {
button.innerHTML = `Keeped (${save_date})`;
});
storage.get(['id_list'], items => {
const id_list = items.id_list || [];
if (!id_list.includes(arxiv_id)) {
id_list.push(arxiv_id);
storage.set({ id_list }, () => {
button.innerHTML = `Keeped (${save_date})`;
});
}
});
};
button.appendChild(document.createTextNode('Keep'));
wrapper.appendChild(button);
target.insertBefore(wrapper, target.childNodes[2]);
if (arxiv_id in items) {
const item = JSON.parse(items[arxiv_id]);
button.innerHTML = `Keeped (${item.save_date})`;
}
});