-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
64 lines (57 loc) · 1.7 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
let redirect_link = {};
// get all quick-links from storage
// store in `redirect_link` variable
chrome.storage.sync.get(['redirect_link'], (result) => {
if (result.redirect_link !== undefined) {
redirect_link = result.redirect_link;
}
});
// event listener for omnibox input
// redirect to quick-link on enter
chrome.omnibox.onInputEntered.addListener((text) => {
if (redirect_link[text] !== undefined) {
chrome.tabs.update({ url: redirect_link[text] });
} else {
chrome.tabs.update({ url: "./error.html" });
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
try {
// create a new quick-link item
if (message.type === 'form_submission') {
const data = message.data;
const key = data[0];
const url = data[1];
redirect_link[key] = url;
chrome.storage.sync.set({ redirect_link });
sendResponse({
success: true,
message: "Q-link created successfully!"
});
}
// update a quick-link item
else if (message.type === 'edit_submission') {
delete redirect_link[message.old_keyword];
redirect_link[message.new_keyword] = message.new_url;
chrome.storage.sync.set({ redirect_link });
sendResponse({
success: true,
message: "Q-link updated successfully!"
});
}
// delete a quick-link item
else if (message.type === 'delete_link') {
delete redirect_link[message.key];
chrome.storage.sync.set({ redirect_link });
sendResponse({
success: true,
message: "Q-link deleted successfully!"
});
}
} catch (error) {
sendResponse({
success: false,
message: "Error: " + error.message
});
}
});