-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
168 lines (139 loc) · 4.51 KB
/
main.ts
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
/*
Copyright 2020 Daniel Betz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const updatingKey = "updating";
const defaultSettings: IPresets = {
"preset-1": {
width: 800,
height: 600,
x: 0,
y: 0,
restorePosition: false,
},
"preset-2": {
width: 1024,
height: 768,
x: 0,
y: 0,
restorePosition: false,
},
"preset-3": {
width: 1280,
height: 720,
x: 0,
y: 0,
restorePosition: false,
},
"preset-4": {
width: 768,
height: 1024,
x: 0,
y: 0,
restorePosition: false,
},
};
// Handle user command
function handleCommand(command: string): void {
browser.storage.local.get(presetsKey)
.then((x) => getNewState(command, x))
.then((newState) =>
browser.windows.getCurrent()
.then((w) => updateWindow(w, newState)), () => { /* ignore */ });
}
// Handle message from popup
function handleMessage(message: unknown): void {
if (typeof message !== "string") {
return;
}
handleCommand(message);
}
// Handle new windows
function handleWindowCreated(window: browser.windows.Window): void {
browser.storage.local.get([presetsKey, handleNewWindowsKey]).then((result) => {
const applyToNewWindows = result[handleNewWindowsKey] as boolean;
if (!applyToNewWindows) {
return undefined;
}
const presets = JSON.parse(result[presetsKey] as string) as IPresets;
const startupPreset = getStartupPreset(presets);
const newState = startupPreset ? getWindowUpdateInfo(startupPreset) : undefined;
return newState;
}).then(newState => updateWindow(window, newState), () => { /* ignore */ });
}
function getNewState(presetName: string, settings: browser.storage.StorageObject): IWindowUpdateInfo | undefined {
if (!settings.presets) {
return undefined;
}
if (!isPresetName(presetName)) {
return undefined;
}
const presets = JSON.parse(settings[presetsKey] as string) as IPresets;
const preset = presets[presetName];
const newState = getWindowUpdateInfo(preset);
return newState;
}
function getWindowUpdateInfo(preset: IPreset): IWindowUpdateInfo {
const newState: IWindowUpdateInfo = {
width: preset.width,
height: preset.height,
};
if (preset.restorePosition) {
newState.left = preset.x;
newState.top = preset.y;
}
return newState;
}
function updateWindow(window: browser.windows.Window, newState?: IWindowUpdateInfo): void {
if (!window.id || !newState) {
return;
}
browser.windows.update(window.id, newState).catch(() => { /* ignore */ });
}
function handleUpdate(): void {
browser.storage.local.set({ [updatingKey]: true }).then(() => {
browser.runtime.reload();
}).catch(() => { /* ignore */ });
}
function getStartupPreset(presets: IPresets): IPreset | undefined {
for (const presetName of presetNames) {
const preset = presets[presetName];
if (preset.restoreOnStart) {
return preset;
}
}
return undefined;
}
// Initialize
async function init(): Promise<void> {
browser.commands.onCommand.addListener(handleCommand);
browser.runtime.onMessage.addListener(handleMessage);
browser.runtime.onUpdateAvailable.addListener(handleUpdate);
browser.windows.onCreated.addListener(handleWindowCreated);
const result = await browser.storage.local.get([presetsKey, updatingKey]);
let savedPresets = result[presetsKey];
if (typeof savedPresets !== "string") {
savedPresets = JSON.stringify(defaultSettings);
await browser.storage.local.set({ [presetsKey]: savedPresets });
}
await browser.storage.local.set({ [updatingKey]: false });
if (result[updatingKey] === true) {
return;
}
const presets = JSON.parse(savedPresets) as IPresets;
const startupPreset = getStartupPreset(presets);
if (startupPreset) {
const newState = getWindowUpdateInfo(startupPreset);
const window = await browser.windows.getCurrent();
updateWindow(window, newState);
}
}
init().catch(() => { /* ignore */ });