10 |
11 |
Extension Options
12 |
13 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/options/options.js:
--------------------------------------------------------------------------------
1 | const DEFAULT_LANGUAGE = 'en',
2 | DEFAULT_TRIGGER_KEY = 'none',
3 | IS_HISTORY_ENABLED_BY_DEFAULT = true,
4 |
5 | SAVE_STATUS = document.querySelector("#save-status"),
6 |
7 | SAVE_OPTIONS_BUTTON = document.querySelector("#save-btn"),
8 | RESET_OPTIONS_BUTTON = document.querySelector("#reset-btn"),
9 |
10 | CLEAR_HISTORY_BUTTON = document.querySelector("#clear-history-btn"),
11 | DOWNLOAD_HISTORY_BUTTON = document.querySelector("#download-history-btn"),
12 |
13 | OS_MAC = 'mac',
14 |
15 | KEY_COMMAND = 'Command',
16 | KEY_META = 'meta';
17 |
18 |
19 |
20 | function saveOptions(e) {
21 | browser.storage.local.set({
22 | language: document.querySelector("#language-selector").value,
23 | interaction: {
24 | dblClick: {
25 | key: document.querySelector("#popup-dblclick-key").value
26 | }
27 | },
28 | history: {
29 | enabled: document.querySelector("#store-history-checkbox").checked
30 | }
31 | }).then(showSaveStatusAnimation);
32 |
33 | e.preventDefault();
34 | }
35 |
36 | function restoreOptions() {
37 | let storageItem = browser.storage.local.get();
38 |
39 | storageItem.then((results) => {
40 | let language = results.language,
41 | interaction = results.interaction || {},
42 | history = results.history || { enabled: IS_HISTORY_ENABLED_BY_DEFAULT },
43 | definitions = results.definitions || {};
44 |
45 | // language
46 | document.querySelector("#language-selector").value = language || DEFAULT_LANGUAGE;
47 |
48 | // interaction
49 | // document.querySelector("#popup-dblclick-checkbox").checked = interaction.dblClick.enabled;
50 | document.querySelector("#popup-dblclick-key").value = (interaction.dblClick && interaction.dblClick.key) || DEFAULT_TRIGGER_KEY;
51 |
52 | // document.querySelector("#popup-select-checkbox").checked = interaction.select.enabled;
53 | // document.querySelector("#popup-select-key").value = interaction.select.key;
54 |
55 | // history
56 | document.querySelector("#store-history-checkbox").checked = history.enabled;
57 | document.querySelector("#num-words-in-history").innerText = Object.keys(definitions).length;
58 | });
59 | }
60 |
61 | function downloadHistory (e) {
62 | let fileContent = ""
63 | storageItem = browser.storage.local.get("definitions"),
64 | anchorTag = document.querySelector("#download-history-link");
65 |
66 | storageItem.then((results) => {
67 | let definitions = results.definitions || {};
68 |
69 | for (definition in definitions) {
70 | if (!definitions.hasOwnProperty(definition)) { return; }
71 |
72 | fileContent += definition;
73 | fileContent += "\t";
74 | fileContent += "\t";
75 | fileContent += definitions[definition];
76 | fileContent += "\n";
77 | }
78 |
79 | anchorTag.href = window.URL.createObjectURL(new Blob([fileContent],{
80 | type: "text/plain"
81 | }));
82 |
83 | anchorTag.dispatchEvent(new MouseEvent('click'));
84 | });
85 |
86 | e.preventDefault();
87 | }
88 |
89 | function resetOptions (e) {
90 | browser.storage.local.set({
91 | language: DEFAULT_LANGUAGE,
92 | interaction: {
93 | dblClick: {
94 | key: DEFAULT_TRIGGER_KEY
95 | }
96 | },
97 | history: {
98 | enabled: IS_HISTORY_ENABLED_BY_DEFAULT
99 | }
100 | }).then(restoreOptions);
101 |
102 | e.preventDefault();
103 | }
104 |
105 | function clearHistory(e) {
106 | browser.storage.local.set({ definitions: {} });
107 |
108 | e.preventDefault();
109 | }
110 |
111 | function showSaveStatusAnimation () {
112 | SAVE_STATUS.style.setProperty("-webkit-transition", "opacity 0s ease-out");
113 | SAVE_STATUS.style.opacity = 1;
114 | window.setTimeout(function() {
115 | SAVE_STATUS.style.setProperty("-webkit-transition", "opacity 0.4s ease-out");
116 | SAVE_STATUS.style.opacity = 0
117 | }, 1500);
118 | }
119 |
120 | document.addEventListener('DOMContentLoaded', restoreOptions);
121 |
122 | CLEAR_HISTORY_BUTTON.addEventListener("click", clearHistory);
123 | DOWNLOAD_HISTORY_BUTTON.addEventListener("click", downloadHistory);
124 |
125 | SAVE_OPTIONS_BUTTON.addEventListener("click", saveOptions);
126 | RESET_OPTIONS_BUTTON.addEventListener("click", resetOptions);
127 |
128 | if (window.navigator.platform.toLowerCase().includes(OS_MAC)) {
129 | document.getElementById("popup-dblclick-key-ctrl").textContent = KEY_COMMAND;
130 | document.getElementById("popup-dblclick-key-ctrl").value = KEY_META;
131 | }
132 |
--------------------------------------------------------------------------------