-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
72 lines (64 loc) · 1.96 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
import { getValueFromStorage, getVideoId, setValueToStorage } from "./utils.js";
const YT_PREFIX = "YT-";
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (
changeInfo.status === "complete" &&
tab.url &&
tab.url.includes("youtube.com/watch")
) {
const videoId = getVideoId(tab);
try {
const result = await getValueFromStorage(YT_PREFIX, videoId, null);
if (!result || result.isTitlePause == null) {
setValueToStorage(
{ ...result, isTitlePause: false },
YT_PREFIX,
videoId
);
}
chrome.tabs.sendMessage(tabId, {
type: "NEW",
videoId: videoId,
settings: {
isTitlePause: result ? result.isTitlePause : false,
},
});
} catch (error) {
// An error occurred
console.error(error.message);
}
}
});
chrome.storage.onChanged.addListener(function (changes, areaName) {
if (areaName === "local" && !!changes) {
// Send a message to the options page
chrome.runtime.sendMessage({ type: "refreshOptionsPage" });
}
});
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "saveToRaven",
title: "Save to Raven",
contexts: ["selection"],
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
console.log("info", info);
console.log("tab", tab);
switch (info.menuItemId) {
case "saveToRaven":
const res = await chrome.tabs.sendMessage(
tab.id,
{
type: "SAVE_TO_RAVEN",
selectionText: info.selectionText,
},
(res) => console.log("received reply", res)
);
console.log("got res", res);
}
});
// chrome.storage.onChanged.addListener(() => {
// chrome.runtime.sendMessage({ type: "LOAD_BOOKMARKS" });
// });
//TODO: add icon credit <a target="_blank" href="https://icons8.com/icon/yFBJCjFJpLXw/save">Save</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>