-
Notifications
You must be signed in to change notification settings - Fork 8
/
background.js
259 lines (219 loc) · 7.22 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
var window = window ?? self;
// await chrome.scripting.registerContentScripts([
// {
// id: 'inpage',
// matches: ['http://*/*', 'https://*/*'],
// js: ['audible-library-extractor-content-script.js'],
// runAt: 'document_start',
// world: 'MAIN',
// },
// ]);
var domainExtension = false;
var activeIcons = [];
var galleryUrl;
chrome.storage.local.get(['extras']).then(data => {
galleryUrl = data?.extras?.galleryUrl;
});
chrome.runtime.onMessage.addListener(function(msg, sender) {
if ( msg.pageAction == true && !chrome.runtime.lastError ) {
chrome.action.setIcon({
tabId: sender.tab.id,
path: 'assets/icons/16.png'
}).then(function() {
activeIcons.push( sender.tab.id );
// chrome.action.show( sender.tab.id );
});
}
});
// https://developer.chrome.com/extensions/tabs
// https://developer.chrome.com/extensions/tabs#event-onUpdated
// chrome.tabs.onUpdated.addListener(tabId => {
// // Error silencing: sometimes when you close a tab right after its created,
// // the action.show() will throw an error because the tab doesn't exist anymore
// chrome.tabs.get(tabId).then(data => {
// if (!chrome.runtime.lastError) {
// chrome.action.setIcon({
// tabId: tabId,
// path: 'assets/icons-gray/16.png'
// }).then(function() {
// chrome.action.show(tabId);
// });
// }
// });
// });
// Extension icon click action....
// https://developer.chrome.com/extensions/pageAction
// https://developer.chrome.com/extensions/pageAction#event-onClicked
chrome.action.onClicked.addListener(tabId => {
// https://developer.chrome.com/extensions/tabs
// https://developer.chrome.com/extensions/tabs#method-query
chrome.tabs.query({ active: true, currentWindow: true }).then(tabs => {
var tab = tabs[0];
// Sends message to the content script...
// https://developer.chrome.com/apps/messaging#simple
// https://developer.chrome.com/extensions/tabs#method-sendMessage
chrome.tabs.sendMessage(tab.id, {
iconClicked: true,
tab: tab
});
});
});
// LISTENS FOR A MESSAGES form the content script
chrome.runtime.onMessage.addListener( async (message, sender) => {
if (message.action === "refresh") {
chrome.tabs.update( sender.tab.id, { url: message.url });
}
else if (message.action === "newPage") {
chrome.tabs.create({
url: message.url,
active: true,
index: sender.tab.index + 1,
});
}
else if (message.action === "openOutput") {
// Close the tab where extraction started
// setTimeout(function() {
// chrome.tabs.reload( sender.tab.id );
chrome.tabs.update( sender.tab.id, { url: message.url });
// }, 2000);
// Open the output page
chrome.tabs.create({
url: galleryUrl || "./gallery.html",
active: true,
index: sender.tab.index + 1,
});
makeContextMenu();
}
else if (message.action === "openImageEditor") {
// Open the output page
chrome.tabs.create({
url: "./wallpaper-creator.html?src=gallery",
active: true,
index: sender.tab.index + 1,
});
}
else if (message.action === "changeGalleryUrl") {
galleryUrl = message.url;
makeContextMenu();
}
else if ( message.action === 'rebuild-context-menu' ) {
makeContextMenu();
}
});
// CONTEXT MENU
chrome.tabs.onActivated.addListener((activeInfo) => {
if ( !chrome.runtime.lastError ) {
if ( activeIcons.indexOf(activeInfo.tabId) >-1 ) {
// chrome.action.show(tabId);
}
else {
chrome.action.setIcon({
tabId: activeInfo.tabId,
path: 'assets/icons-gray/16.png'
}).then(function() {
// chrome.action.show(activeInfo.tabId);
});
}
}
});
makeContextMenu();
function makeContextMenu() {
// https://developer.chrome.com/apps/storage
// Permission: "storage"
chrome.storage.local.get(['chunks', 'extras']).then(data => {
const dataChunks = data?.chunks || [];
const storageHasData = dataChunks.length > 0;
const libraryExists = dataChunks.lastIndexOf('books') > -1;
const wishlistExists = dataChunks.lastIndexOf('wishlist') > -1;
domainExtension = data?.extras?.['domain-extension'];
data = null;
// https://developer.chrome.com/docs/extensions/reference/contextMenus/
// Permissions: contextMenus
chrome.contextMenus.removeAll();
const audibleLink = {
id: 'ale-to-audible',
title: "1. Audible"+ domainExtension +"/library",
contexts: ["all"]
};
if ( !domainExtension ) {
audibleLink.title = "1. Audible.com/library (placeholder)"
}
chrome.contextMenus.create( audibleLink );
const galleryLink = {
id: 'ale-to-gallery',
title: "2. Extension gallery",
contexts: ["all"]
};
if ( !(libraryExists || wishlistExists) ) {
galleryLink.enabled = false;
galleryLink.title = galleryLink.title + ' ' + '(available after extraction)';
}
chrome.contextMenus.create( galleryLink );
chrome.contextMenus.create({
id: 'separator-1',
type: "separator",
contexts: ["all"]
});
chrome.contextMenus.create({
id: 'ale-to-docs',
title: "3. Documentation",
contexts: ["all"]
});
chrome.contextMenus.create({
id: 'ale-to-github',
title: "4. Github project page",
contexts: ["all"]
});
chrome.contextMenus.create({
id: 'ale-to-github-issues',
title: "5. Github issues",
contexts: ["all"]
});
chrome.contextMenus.create({
id: 'ale-to-github-discussions',
title: "6. Github discussions",
contexts: ["all"]
});
});
}
chrome.contextMenus.onClicked.addListener(contextEvents);
function contextEvents( info, tab ) {
var newTab = {
active: true,
index: tab.index + 1,
openerTabId: tab.id
};
if ( info.menuItemId === 'ale-to-audible' ) {
if ( !domainExtension ) {
chrome.storage.local.get(['extras']).then(data => {
domainExtension = data?.extras?.['domain-extension'];
newTab.url = "https://audible"+ (domainExtension || '.com') +"/library/titles?ipRedirectOverride=true&overrideBaseCountry=true";
chrome.tabs.create(newTab);
});
}
else {
newTab.url = "https://audible"+ domainExtension +"/library/titles?ipRedirectOverride=true&overrideBaseCountry=true";
chrome.tabs.create(newTab);
}
}
else if ( info.menuItemId === 'ale-to-gallery' ) {
newTab.url = galleryUrl || "./gallery.html";
chrome.tabs.create(newTab);
}
else if ( info.menuItemId === 'ale-to-docs' ) {
newTab.url = "https://joonaspaakko.gitbook.io/audible-library-extractor/";
chrome.tabs.create(newTab);
}
else if ( info.menuItemId === 'ale-to-github' ) {
newTab.url = "https://github.com/joonaspaakko/audible-library-extractor";
chrome.tabs.create(newTab);
}
else if ( info.menuItemId === 'ale-to-github-issues' ) {
newTab.url = "https://github.com/joonaspaakko/audible-library-extractor/issues";
chrome.tabs.create(newTab);
}
else if ( info.menuItemId === 'ale-to-github-discussions' ) {
newTab.url = "https://github.com/joonaspaakko/audible-library-extractor/discussions";
chrome.tabs.create(newTab);
}
}