-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
169 lines (158 loc) · 5.36 KB
/
menu.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
/* eslint no-console: "off" */
const menuId = "ptw";
console.log("removing context menus");
browser.contextMenus.removeAll()
.then(() => {
console.log("creating context menu");
browser.contextMenus.create({
"id": menuId,
"title": "Add to PTW list",
"contexts": ["link"],
});
})
.catch((e) => {
console.log("error while deleting context menus", e);
throw e;
})
.then(async () => {
const intervalId = setInterval(async () => {
// eslint-disable-next-line no-undef
if (optionsLock) {
return;
}
clearInterval(intervalId);
let optionsWithVersioning = {};
let options = {};
try {
// eslint-disable-next-line no-undef
optionsWithVersioning = await browser.storage.sync.get();
// eslint-disable-next-line no-undef
options = optionsWithVersioning[optionsVersion];
} catch (e) {
console.log("error while getting options", e);
throw e;
}
let optionsWithVersioningLocal = {};
let optionsLocal = {};
try {
// eslint-disable-next-line no-undef
optionsWithVersioningLocal = await browser.storage.local.get();
// eslint-disable-next-line no-undef
optionsLocal = optionsWithVersioningLocal[optionsVersion];
} catch (e) {
console.log("error while getting options", e);
throw e;
}
browser.storage.onChanged.addListener((changes, areaName) => {
console.log("option was changed", changes);
switch (areaName) {
case "sync":
Object.entries(changes).forEach((change) => {
optionsWithVersioning[change[0]] = change[1].newValue;
});
// eslint-disable-next-line no-undef
options = optionsWithVersioning[optionsVersion];
break;
case "local":
Object.entries(changes).forEach((change) => {
optionsWithVersioningLocal[change[0]] = change[1].newValue;
});
// eslint-disable-next-line no-undef
optionsLocal = optionsWithVersioningLocal[optionsVersion];
break;
default:
console.log("Storage change in unknown location");
break;
}
});
const Sites = Object.freeze({
"mal": Symbol("MyAnimeList"),
"anilist": Symbol("AniList"),
"kitsu": Symbol("Kitsu"),
});
const validateAndMineURL = (url, notIgnoring) => {
const matchFuncs = {
"mal": matchOnMAL, // eslint-disable-line no-undef
"anilist": matchOnAniList, // eslint-disable-line no-undef
"kitsu": matchOnKitsu, // eslint-disable-line no-undef
};
// eslint-disable-next-line no-restricted-syntax
for (const site of Object.keys(matchFuncs)) {
const match = matchFuncs[site](url);
if (match) {
match.source = Sites[site];
match.notIgnoring = notIgnoring[site];
return match;
}
}
return false;
};
const createNotification = (notification) => {
if (options.checkbox.extension_displayNotifications) {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("./icons/icon_48.png"),
"title": notification.title,
"message": notification.message,
});
}
};
browser.contextMenus.onClicked.addListener(async (info, tab) => {
console.log("hello");
if (info.menuItemId === menuId) {
console.log(`Link URL: ${info.linkUrl}`);
console.log(`Tab URL: ${tab.url}`);
const notIgnoring = {
"mal": options.checkbox.mal_mal,
"anilist": options.checkbox.anilist_anilist,
"kitsu": options.checkbox.kitsu_kitsu,
};
const urlData = validateAndMineURL(info.linkUrl, notIgnoring);
if (urlData && !urlData.notIgnoring) {
console.log("Match ignored");
createNotification({
"title": "Ignoring list site",
"message": `PTW Extender is currently set to ignore ${urlData.source
.toString().slice(7, -1)}`,
});
} else if (urlData && urlData.source === Sites.mal) {
// eslint-disable-next-line no-undef
createNotification(await handleMAL(tab, urlData, {
"prettifyCommentsBox": options.checkbox.extension_prettifyCommentsBox,
"autosubmit": options.checkbox.mal_autosubmit,
"behaviorPostAutosubmit": options.radio.mal_behaviorPostAutosubmit,
"priority": options.radio.mal_priority,
"tags": options.textarea.mal_tags,
}));
} else if (urlData && urlData.source === Sites.anilist) {
// eslint-disable-next-line no-undef
createNotification(await handleAniList(tab, urlData, {
"accessToken": optionsLocal.authentication.anilist.accessToken,
"private": options.checkbox.anilist_private,
"hiddenFromStatusLists": options.checkbox.anilist_hiddenFromStatusLists,
"customListsAnime": options.multipleCheckbox.anilist_customListsAnime,
"customListsManga": options.multipleCheckbox.anilist_customListsManga,
}));
} else if (urlData && urlData.source === Sites.kitsu) {
// eslint-disable-next-line no-undef
createNotification(await handleKitsu(tab, urlData, {
"accessToken": optionsLocal.authentication.kitsu.accessToken,
"private": options.checkbox.kitsu_private,
}));
} else {
console.log("Match fail");
createNotification({
"title": "Unknown list site",
"message": "PTW Extender isn't compatible with that site",
});
}
console.log("world");
}
});
}, 50);
});
browser.runtime.onInstalled.addListener((details) => {
if (["install", "update"].includes(details.reason)) {
browser.runtime.openOptionsPage();
}
});