-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolbar_menu.js
78 lines (63 loc) · 2.28 KB
/
toolbar_menu.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
async function updateMenuWithPresets()
{
let list = document.querySelector("#presetsList");
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
for(let i = 0;i < presets.length;i++)
{
let position = i + 1;
console.log("(menu) Add item " + position + ": " + presets[i].name);
let linkText = presets[i].width + "x" + presets[i].height + " " + presets[i].name;
let numberText = position + ": ";
let shortcutKey = await getShortcutKey(PREFIX_PRESET + position);
let tr = document.createElement("tr");
let tdN = document.createElement("td");
tr.className = "presetIndex";
tdN.appendChild(document.createTextNode(numberText));
tr.appendChild(tdN);
let tdL = document.createElement("td");
let a = document.createElement("a");
a.href = "#";
a.id = PREFIX_PRESET + presets[i].id;
a.title = shortcutKey;
a.appendChild(document.createTextNode(linkText));
tdL.appendChild(a);
tr.appendChild(tdL);
list.appendChild(tr);
}
}
async function doMenuItemClick(e)
{
console.log("(menu) item clicked");
e.preventDefault();
let storage = await browser.storage.local.get("options");
let options = storage.options;
let keepActionMenuOnClick = storage.options.keepActionMenuOnClick !== undefined && storage.options.keepActionMenuOnClick;
let myId = e.target.id;
if(myId === "customize")
{
browser.runtime.openOptionsPage();
keepActionMenuOnClick = false; // Force menu closed in this case
}
else if(myId.startsWith(PREFIX_PRESET))
{
let presetId = myId.substring(PREFIX_PRESET.length);
await applyPreset(presetId);
}
if(keepActionMenuOnClick)
{
showCurrentSize();
}
else
{
window.close();
}
}
async function showCurrentSize()
{
let currentWindow = await browser.windows.getCurrent();
document.querySelector("#currentSize").textContent = currentWindow.width + "x" + currentWindow.height;
}
document.addEventListener("DOMContentLoaded", updateMenuWithPresets);
document.addEventListener("click", doMenuItemClick);
window.onload = () => setTimeout(showCurrentSize,1000); // This is such a kludge, but there seems to be no other event to latch on to that gives correct results