-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
189 lines (160 loc) · 5.02 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
const browser = chrome;
/**
* This returns a sorted list of all of the current keys in the dictionary from local storage
*/
async function getDictKeys() {
let dict = (await browser.storage.local.get('dictionary').catch((e) => console.error(e))).dictionary;
if (!dict) {
return [];
}
sortedDictKeys = Object.keys(dict).toSorted();
return sortedDictKeys;
}
/**
* This gets the keys and values!
* @returns The current state of the dictionary from local storage
*/
async function getKeysAndVals() {
let dict = (await browser.storage.local.get('dictionary').catch((e) => console.error(e))).dictionary;
return dict;
}
async function deleteDictKey(key) {
console.log(`in deleteDictKey with key: ${key}`)
let dict = (await browser.storage.local.get('dictionary').catch((e) => console.error(e)));
console.log(`dict is ${JSON.stringify(dict)}`);
try{
delete dict.dictionary[key];
}
catch (error) {
console.error(error);}
console.log(`after deleting: dict is ${JSON.stringify(dict)}`);
browser.storage.local.set(dict);
}
async function logKeyValuePair(keyValue) {
let dict = await browser.storage.local.get('dictionary');
if (!dict.dictionary) {
dict = {dictionary: {}};
}
dict.dictionary[keyValue.key] = keyValue.value;
await browser.storage.local.set(dict);
}
async function getValueFomKey(key) {
dict = (await browser.storage.local.get('dictionary')).dictionary
return dict[key] ?? "no such key";
}
/**
* This function opens a popup
* Waits for the popup to give a response
* And then logs that value where appropriate
* @returns
*/
async function storePopupContent() {
await browser.action.openPopup();
//get our keys:
const keys = await getDictKeys();
// wait a little bit for the popup to open
await new Promise(resolve => setTimeout(resolve, 50));
browser.runtime.sendMessage( {
command: "getInput", keys: keys
})}
// send message to popup
async function getPopupContent() {
await browser.action.openPopup();
//wait a little bit for popup to load:
await new Promise(resolve => setTimeout(resolve, 50));
//get our keys:
const keys = await getDictKeys();
browser.runtime.sendMessage( {
command: "getOutput",
keys: keys
});
}
//keyboard shortcuts listener
browser.commands.onCommand.addListener(async (command) => {
if (command === "store-content") {
storePopupContent();
}
if (command === "get-content") {
getPopupContent();
}
if (command == "show-popup") {
await browser.action.openPopup();
dict = await browser.storage.local.get('dictionary');
console.log("in print-dictionary");
dictText = JSON.stringify(dict);
console.log(`Dict is: \n ${dictText}`);
keys = await getDictKeys();
console.log(`Done with keys ${keys}`);
}
});
// message listener from popup
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.command) {
case "inputResult":
logKeyValuePair(message.result);
break;
case "outputResult":
getValueFomKey(message.key).then(pasteValue =>{
browser.runtime.sendMessage({
command: "addValueToClipboard",
value: pasteValue,
close: message.close ?? null,
});
});
break;;
case "getKeys":
console.log("in getKeys")
getDictKeys().then(result => {
sendResponse(result);
});
return true;
case "getKeysAndVals":
getKeysAndVals().then(result => {
sendResponse(result);
});
return true;
case "getUpdateResult":
//console.log("In getUpdateResult");
getValueFomKey(message.key).then(pasteValue => {
browser.runtime.sendMessage({
command: "updateValueInPopup",
value: pasteValue,
key: message.key
})});
break;
case "deleteKey":
deleteDictKey(message.key);
break;
default:
console.error('Unknown command:', message.command);
break;
}
});
browser.runtime.onInstalled.addListener(() => {
browser.contextMenus.removeAll();
// Add things to the context menu!
browser.contextMenus.create( {
id: "add-value",
title: "Add value",
contexts: ["all"],
type: "normal",
parentId: null
});
browser.contextMenus.create( {
id: "get-value",
title: "Get value",
contexts: ["all"],
type: "normal",
parentId: null
});
})
browser.contextMenus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) {
case "add-value":
storePopupContent();
break;
case "get-value":
getPopupContent();
break;
}
})