-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.js
158 lines (128 loc) · 6.74 KB
/
options.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {populateTemplate} from "./template.js";
import {DEFAULT_OPTIONS as DEFAULT_OPTIONS_GR} from "./goodreads/consts.js";
import {DEFAULT_OPTIONS as DEFAULT_OPTIONS_YT} from "./youtube/consts.js";
function getFakeBook() {
let book = new Object();
book.short_title = "Good Omens";
book.full_title = "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch\n";
book.formatted_authors = "[[Terry Pratchett]], [[Neil Gaiman]]";
book.publication_year = "2006";
book.cover_image_url = "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1615552073l/12067.jpg";
book.cover_image = `![](${book.cover_image_url})`;
book.abstract = "‘Armageddon only happens once, you know. They don’t let you go around again until you get it right.’\nPeople have been predicting the end of the world almost from its very beginning, so it’s only natural to be sceptical when a new date is set for Judgement Day. But what if, for once, the predictions are right, and the apocalypse really is due to arrive next Saturday, just af ...more\n"
book.series = "Prophecies #3";
book.rating = "4.24";
book.rating_count = 620758;
book.total_pages = "491 pages";
book.isbn13 = "9780060853983";
book.item_url = "https://www.goodreads.com/book/show/12067.Good_Omens";
return book;
}
function getFakeVideo() {
let video = new Object();
video.short_title = "Coffee";
video.full_title = "Coffee: The Greatest Addiction Ever";
video.channel = "CGP Grey";
return video;
}
function onNoteTitleInputChangeGoodreads() {
let note_title = document.getElementById('note_title').value;
let preview = document.getElementById('note_title_preview');
preview.value = populateTemplate(note_title, getFakeBook());
}
function onNoteContentInputChangeGoodreads() {
let note_content = document.getElementById('note_content').value;
let preview = document.getElementById('note_content_preview');
preview.value = populateTemplate(note_content, getFakeBook());
}
function onNoteTitleInputChangeYouTube() {
let note_title = document.getElementById('note_title_yt').value;
let preview = document.getElementById('note_title_preview_yt');
preview.value = populateTemplate(note_title, getFakeVideo());
}
function onNoteContentInputChangeYouTube() {
let note_content = document.getElementById('note_content_yt').value;
let preview = document.getElementById('note_content_preview_yt');
preview.value = populateTemplate(note_content, getFakeVideo());
}
// Saves options to chrome.storage
function saveOptionsToStorage() {
let vault = document.getElementById('vault').value;
let file_location = document.getElementById('file_location').value;
let note_title = document.getElementById('note_title').value;
let note_content = document.getElementById('note_content').value;
let vault_yt = document.getElementById('vault-yt').value;
let file_location_yt = document.getElementById('file_location_yt').value;
let note_title_yt = document.getElementById('note_title_yt').value;
let note_content_yt = document.getElementById('note_content_yt').value;
let keys_to_set = {
vault, file_location, note_title, note_content, vault_yt, file_location_yt, note_title_yt, note_content_yt
};
chrome.storage.sync.set(keys_to_set, function() {
// Update status to let user know options were saved.
let status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
function setFormValues(items) {
document.getElementById('vault').value = items.vault;
document.getElementById('file_location').value = items.file_location;
document.getElementById('note_title').value = items.note_title;
document.getElementById('note_content').value = items.note_content;
document.getElementById('vault-yt').value = items.vault_yt;
document.getElementById('file_location_yt').value = items.file_location_yt;
document.getElementById('note_title_yt').value = items.note_title_yt;
document.getElementById('note_content_yt').value = items.note_content_yt;
// Trigger the previews, since changing the values in code doesn't seem to
// trigger the event listeners).
onNoteTitleInputChangeGoodreads();
onNoteContentInputChangeGoodreads();
onNoteTitleInputChangeYouTube();
onNoteContentInputChangeYouTube();
}
function getOptionDefaults() {
return {
...DEFAULT_OPTIONS_GR,
...DEFAULT_OPTIONS_YT,
}
}
function restoreOptionsFromDefaults() {
setFormValues(getOptionDefaults());
}
function restoreOptionsFromStorage() {
chrome.storage.sync.get(getOptionDefaults(), setFormValues);
}
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('save').addEventListener('click', saveOptionsToStorage);
document.getElementById('restore_defaults').addEventListener('click', restoreOptionsFromDefaults);
let note_title_elm_gr = document.getElementById('note_title');
note_title_elm_gr.addEventListener('input', onNoteTitleInputChangeGoodreads);
let note_content_elm_gr = document.getElementById('note_content');
note_content_elm_gr.addEventListener('input', onNoteContentInputChangeGoodreads);
let note_title_elm_yt = document.getElementById('note_title_yt');
note_title_elm_yt.addEventListener('input', onNoteTitleInputChangeYouTube);
let note_content_elm_yt = document.getElementById('note_content_yt');
note_content_elm_yt.addEventListener('input', onNoteContentInputChangeYouTube());
restoreOptionsFromStorage();
document.querySelectorAll(".booksidian_template").forEach(function(elm) {
elm.innerHTML = `<code>${populateTemplate(elm.textContent, getFakeBook())}</code>`;
})
document.querySelectorAll(".booksidian_template_yt").forEach(function(elm) {
elm.innerHTML = `<code>${populateTemplate(elm.textContent, getFakeVideo())}</code>`;
})
const note_content_preview_elm_gr = document.getElementById("note_content_preview");
const resizeObserverGoodreads = new ResizeObserver(() => {
note_content_preview_elm_gr.style.height = note_content_elm_gr.offsetHeight + "px";
note_content_preview_elm_gr.style.width = note_content_elm_gr.offsetWidth + "px";
});
resizeObserverGoodreads.observe(note_content_elm_gr);
const note_content_preview_elm_yt = document.getElementById("note_content_preview_yt");
const resizeObserverYouTube = new ResizeObserver(() => {
note_content_preview_elm_yt.style.height = note_content_elm_yt.offsetHeight + "px";
note_content_preview_elm_yt.style.width = note_content_elm_yt.offsetWidth + "px";
});
resizeObserverYouTube.observe(note_content_elm_yt);
});