generated from JL978/chrome-extension-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
340 lines (271 loc) · 8.42 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ widgetEnabled: false });
});
// const API_URL = "http://tt-notion.fly.dev"
const API_URL = "http://localhost:8080"
const isCorrectTikTokURL = (url) => {
const urlRegex = /https:\/\/www.tiktok.com\/@(.*)\/video\/(.*)/;
const test = urlRegex.test(url);
return test;
}
const getVidInfo = (url) => {
if (!isCorrectTikTokURL(url)) return null;
const channel = url.match(/@(.*)\/video/)[1];
const videoId = url.split("/").pop();
return { channel, videoId };
}
const createWidgetWithEmptyData = () => {
let container = document.querySelector(".WidgetContainer");
if (!container) {
container = document.createElement("div");
container.id = "tiktok-notion"
container.classList.add("WidgetContainer");
document.body.appendChild(container);
}
container.innerHTML = "";
const createButton = document.createElement("button");
createButton.innerHTML = "Create New Notes";
createButton.classList.add("TTNotionButton");
const listener = () => {
createButton.disabled = true;
createButton.innerHTML = "Creating...";
// send to background script
chrome.runtime.sendMessage({ type: "createPage" }, () => {
createButton.removeEventListener("click", listener);
})
}
createButton.addEventListener("click", listener);
container.appendChild(createButton);
}
// This is injected into the page so it won't have access to any global variables in this file
// TODO: move this to a separate file
const createWidget = (tags, videoData, API_URL) => {
const TEXT_COLORS = {
default: "#32302c",
gray: "#32302c",
brown: "#442a1e",
orange: "#49290e",
yellow: "#402c1b",
green: "#1c3829",
blue: "#183347",
purple: "#412454",
pink: "#4c2337",
red: "#5d1715",
}
const TAG_COLORS = {
default: "rgba(227, 226, 224, 0.5)",
gray: "rgb(227, 226, 224)",
brown: "rgb(238, 224, 218)",
orange: "rgb(250, 222, 201)",
yellow: "rgb(253, 236, 200)",
green: "rgb(219, 237, 219)",
blue: "rgb(211, 229, 239)",
purple: "rgb(232, 222, 238)",
pink: "rgb(245, 224, 233)",
red: "rgb(255, 226, 221)",
}
let container = document.querySelector(".WidgetContainer");
if (!container) {
container = document.createElement("div");
container.id = "tiktok-notion"
container.classList.add("WidgetContainer");
document.body.appendChild(container)
} else {
container.innerHTML = "";
}
const notesTitle = document.createElement("h1");
notesTitle.innerHTML = "Notes";
const notesField = document.createElement("textarea");
notesField.id = "tiktok-notes";
notesField.classList.add("NotesField");
notesField.placeholder = "Thoughts on the video";
const notes = videoData?.results[0]?.properties?.Notes?.rich_text[0]?.plain_text || "";
if (notes) {
notes.replace(/\n/g, "<br>");
notesField.value = notes;
}
notesField.addEventListener("input", (e) => {
const notesContent = e.target.value;
console.log(notesContent, notes)
if (notesContent !== notes) {
button.disabled = false;
} else {
button.disabled = true;
}
})
const tagsTitle = document.createElement("h1");
tagsTitle.innerHTML = "Tags";
// multi select tags
const tagsContainer = document.createElement("div");
tagsContainer.classList.add("TagsContainer");
const selectedTags = videoData?.results[0]?.properties?.Tags?.multi_select;
tags.forEach(tag => {
const tagContainer = document.createElement("label");
tagContainer.style.border = `1px solid ${TEXT_COLORS[tag.color]}`;
tagContainer.style.color = TEXT_COLORS[tag.color];
tagContainer.innerHTML = tag.name
const tagCheckbox = document.createElement("input");
tagCheckbox.type = "checkbox";
const isSelected = selectedTags?.find(selectedTag => selectedTag.id === tag.id);
if (isSelected) {
tagCheckbox.checked = true;
tagContainer.style.backgroundColor = TAG_COLORS[tag.color];
}
tagCheckbox.addEventListener("change", () => {
if (tagCheckbox.checked) {
tagContainer.style.backgroundColor = TAG_COLORS[tag.color];
} else {
tagContainer.style.backgroundColor = "transparent";
}
const tags = Array.from(tagsContainer.children).filter(child => child.children[0]?.checked);
const tagIds = new Set(tags.map(tag => tag.children[0].id))
const selectedTagsIds = new Set(selectedTags?.map(tag => tag.id) || [])
//compare contents of tagIds and selectedTagsIds
if (tagIds.size === selectedTagsIds.size && Array.from(tagIds).every(tagId => selectedTagsIds.has(tagId))) {
button.disabled = true;
} else {
button.disabled = false;
}
})
tagContainer.appendChild(tagCheckbox);
tagsContainer.appendChild(tagContainer);
});
const button = document.createElement("button");
button.innerHTML = "Update";
button.disabled = true;
button.classList.add("TTNotionButton");
button.addEventListener("click", async () => {
// get selected tags name
const tags = Array.from(tagsContainer.children)
.filter(child => child.children[0]?.checked)
.map(child => ({
name: child.childNodes[0]?.nodeValue
}));
const notes = notesField.value;
// convert notes back to plain text
notes.replace(/<br>/g, "\n");
const data = {
tags,
notes,
pageId: videoData.results[0]?.id,
}
button.innerHTML = "Updating...";
button.disabled = true;
const res = await fetch(API_URL + "/updateDb", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const updatedVideoData = await res.json();
if (!updatedVideoData.error) {
button.innerHTML = "Update";
}
console.log(updatedVideoData);
})
container.appendChild(tagsTitle);
container.appendChild(tagsContainer);
container.appendChild(notesTitle);
container.appendChild(notesField);
container.appendChild(button);
}
const getDb = async () => {
const res = await fetch(API_URL + "/db");
const data = await res.json();
return data;
}
const getVideoData = async (url) => {
const res = await fetch(API_URL + "/queryDb" + `?url=${url}`)
const data = await res.json();
return data;
}
const removeWidget = (tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const oldWidget = document.getElementById("tiktok-notion");
if (oldWidget) {
oldWidget.remove();
}
},
});
}
const handleWidget = async (widgetEnabled, tab) => {
chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: ["./widget.css"],
})
if (widgetEnabled) {
const url = tab.url;
if (!url?.includes("tiktok")) return;
try {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const oldWidget = document.getElementById("tiktok-notion");
if (oldWidget) {
oldWidget.innerHTML = "Loading...";
}
}
})
const db = await getDb();
const tags = db.properties.Tags.multi_select.options
const videoData = await getVideoData(url);
const urlDoesNotExist = videoData.results.length === 0;
// add items on screen
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: urlDoesNotExist ? createWidgetWithEmptyData : createWidget,
args: [tags, videoData, API_URL]
});
// check if video is already in database
} catch (err) {
console.log(err);
}
} else {
removeWidget(tab);
}
}
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.type === "createPage") {
const res = await fetch(API_URL + "/addToDb", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
url: sender.tab.url,
}),
})
const data = await res.json();
sendResponse(data);
handleWidget(true, sender.tab)
}
});
chrome.action.onClicked.addListener(function (tab) {
chrome.storage.sync.get("widgetEnabled", async (data) => {
const widgetEnabled = !data.widgetEnabled;
chrome.storage.sync.set({ widgetEnabled });
handleWidget(widgetEnabled, tab);
})
});
const tabUpdateHandler = (tab, newTab) => {
if (!isCorrectTikTokURL(tab.url)) return;
chrome.storage.sync.get("widgetEnabled", async (data) => {
const widgetEnabled = data.widgetEnabled;
if (newTab && widgetEnabled) return;
handleWidget(widgetEnabled, tab);
});
}
// when url in current tab changes
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== "complete") return;
tabUpdateHandler(tab, false);
});
// when new tab is selected
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
tabUpdateHandler(tab, true);
});
});