-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
192 lines (172 loc) · 7.72 KB
/
scripts.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
(function () {
"use strict";
const getElement = (id) => document.getElementById(id);
const storage = {
key: "LUA_Settings",
get: () => localStorage.getItem(storage.key),
set: (data) => localStorage.setItem(storage.key, data)
};
const button = {
save: () => {
if (confirm("Save settings?")) {
if (settings.parse()) {
storage.set(getElement("editor").value);
}
}
},
load: () => {
if (confirm("Load saved settings?")) {
getElement("editor").value = storage.get();
settings.parse();
}
},
default: () => {
if (confirm("Load default settings?")) {
storage.set(defaults);
getElement("editor").value = defaults;
settings.parse();
}
}
};
const menu = {
id: {}, // track the ID of submenus
clear: () => {
chrome.contextMenus.removeAll();
menu.id = {};
},
link: (address, title, parent) => {
chrome.contextMenus.create({
contexts: ["selection"],
parentId: menu.id[parent],
title: title,
onclick: (info) => chrome.tabs.create({ url: address.replace("%s", info.selectionText.trim()) })
});
},
separator: (parent) => {
chrome.contextMenus.create({
contexts: ["selection"],
parentId: menu.id[parent],
type: "separator"
});
},
submenu: (title, parent) => {
menu.id[title] = chrome.contextMenus.create({
contexts: ["selection"],
parentId: menu.id[parent],
title: title
});
}
};
const settings = {
parse: () => {
try {
menu.clear();
let text = getElement("editor").value;
if (!text || settings._parse(JSON.parse(text))) {
return true;
}
} catch (e) {
alert(`${e.name}: ${e.message}`);
}
return false;
},
_parse: (rows, parent) => {
for (let row of rows) {
let error = false;
switch (row.type) {
case undefined: // default to "link" type if not specified
case "link":
error = !row.title ? "Title required for links." : !row.address ? "Address required for links." : false;
if (error) {
throw { name: "Missing Property", message: `${error}\n${JSON.stringify(row)}` };
}
menu.link(row.address, row.title, parent);
break;
case "menu":
error = !row.title ? "Title required for menus." : !row.entries ? "One or more entries required for menus." : false;
if (error) {
throw { name: "Missing Property", message: `${error}\n${JSON.stringify(row)}` };
}
menu.submenu(row.title, parent);
settings._parse(row.entries, row.title);
break;
case "separator":
menu.separator(parent);
break;
default:
throw { name: "Unknown Type", message: `Valid types are link, menu, and separator.\n${JSON.stringify(row)}` };
}
}
}
};
const defaults = `[
{ "title": "DuckDuckGo", "address": "https://duckduckgo.com/?q=%s" },
{ "title": "Google", "address": "http://www.google.com/search?q=%s" },
{ "title": "Bing", "address": "http://www.bing.com/search?q=%s" },
{ "title": "Yahoo!", "address": "http://search.yahoo.com/search?p=%s" },
{ "type": "separator" },
{ "title": "Wikipedia", "address": "http://www.wikipedia.org/wiki/%s" },
{ "title": "Dictionary.com", "address": "http://dictionary.reference.com/browse/%s" },
{ "title": "Urban Dictionary", "address": "http://www.urbandictionary.com/define.php?term=%s" },
{ "type": "separator" },
{ "title": "Amazon", "address": "http://www.amazon.com/s/field-keywords=%s" },
{ "title": "eBay", "address": "http://www.ebay.com/sch/i.html?_nkw=%s" },
{ "type": "separator" },
{ "title": "YouTube", "address": "http://www.youtube.com/results?search_query=%s" },
{ "type": "separator" },
{ "type": "menu", "title": "Currency", "entries":
[
{ "title": "CAD -> USD", "address": "http://www.google.com/search?q=%s+CAD+to+USD" },
{ "title": "CNY -> USD", "address": "http://www.google.com/search?q=%s+CNY+to+USD" },
{ "title": "EUR -> USD", "address": "http://www.google.com/search?q=%s+EUR+to+USD" },
{ "title": "GBP -> USD", "address": "http://www.google.com/search?q=%s+GBP+to+USD" },
{ "title": "JPY -> USD", "address": "http://www.google.com/search?q=%s+JPY+to+USD" },
{ "title": "KRW -> USD", "address": "http://www.google.com/search?q=%s+KRW+to+USD" },
{ "title": "USD -> CAD", "address": "http://www.google.com/search?q=%s+USD+to+CAD" },
{ "title": "USD -> CNY", "address": "http://www.google.com/search?q=%s+USD+to+CNY" },
{ "title": "USD -> EUR", "address": "http://www.google.com/search?q=%s+USD+to+EUR" },
{ "title": "USD -> GBP", "address": "http://www.google.com/search?q=%s+USD+to+GBP" },
{ "title": "USD -> JPY", "address": "http://www.google.com/search?q=%s+USD+to+JPY" },
{ "title": "USD -> KRW", "address": "http://www.google.com/search?q=%s+USD+to+KRW" }
]
},
{ "type": "separator" },
{ "type": "menu", "title": "Translate", "entries":
[
{ "title": "CN -> US", "address": "http://translate.google.com/#zh-CN|en|%s" },
{ "title": "JP -> US", "address": "http://translate.google.com/#ja|en|%s" },
{ "title": "KR -> US", "address": "http://translate.google.com/#ko|en|%s" },
{ "title": "US -> CN", "address": "http://translate.google.com/#en|zh-CN|%s" },
{ "title": "US -> JP", "address": "http://translate.google.com/#en|ja|%s" },
{ "title": "US -> KR", "address": "http://translate.google.com/#en|ko|%s" }
]
}
]`.replace(/\n {4}/g, "\n");
// initialization when extension is installed
if (storage.get() === null) {
storage.set(defaults);
settings._parse(JSON.parse(defaults));
} else {
// initialization when chrome starts
if (!Object.keys(menu.id).length) {
settings._parse(JSON.parse(storage.get()));
menu.id._initialized_ = true;
}
}
// options page
document.onreadystatechange = () => {
if (document.title.includes("Lookup Assistant") && document.readyState !== "loading") {
getElement("editor").value = storage.get();
settings.parse();
for (const key of Object.keys(button)) {
getElement(key).onclick = button[key];
}
window.onbeforeunload = (event) => {
if (getElement("editor").value !== storage.get()) {
event.preventDefault();
event.returnValue = "There are unsaved changes!";
}
};
}
};
}());