Skip to content

Commit 2bf1b52

Browse files
committed
New features & code maintenance
Added Quick Resize feature (issue #23) Added an Update option for backups Fix button spacing in presets area Removed a kludgy setTimeout, current size now updates instantly in popup Better centralized constants & utility functions Improved console logging Prefer const over let
1 parent 2402dcc commit 2bf1b52

File tree

8 files changed

+306
-191
lines changed

8 files changed

+306
-191
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ A simple, customizeable window resizer
66
- Define multiple custom width & height presets in options
77
- Easily switch between them via toolbar menu
88
- Even more quickly switch between them via hotkeys
9+
- Backup & restore with Sync compatiblity
10+
- Compatible with private windows & requires *no browser privileges*
911

1012
Inspired by the old Firefox add-on [Browsizer]
1113

background.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
"use strict";
2-
3-
const CURRENT_STORAGE_VERSION = 2;
4-
51
async function initialize(details)
62
{
7-
console.log("(init) Initializing...");
3+
console.debug("Initializing...");
84

95
// Preset initialization, if needed
10-
let storage = await browser.storage.local.get(["version","presets","options","advanced"]);
6+
const storage = await browser.storage.local.get(["version","presets","options","advanced"]);
117
if(storage.version === undefined)
128
{
139
// Had non-versioned preset storage, so treat it as version 1
@@ -22,7 +18,7 @@ async function initialize(details)
2218

2319
if(storage.version == 1)
2420
{
25-
console.log("(update) Migrating storage from version 1 to 2...");
21+
console.warn("Migrating storage from version 1 to 2...");
2622
storage.version = 2;
2723
}
2824

@@ -36,54 +32,80 @@ async function initialize(details)
3632
}
3733
else
3834
{
39-
console.log("(init) No storage version tag found, initializing...");
35+
console.info("No storage version tag found, initializing...");
4036
await browser.storage.local.set({"version": CURRENT_STORAGE_VERSION});
4137
}
4238

4339
if(!Array.isArray(storage.presets))
4440
{
45-
console.log("(init) No presets found, initializing...");
41+
console.info("No presets found, initializing...");
4642
storage.presets = [];
4743
await browser.storage.local.set({"presets": storage.presets});
4844
}
4945

5046
if(storage.options === undefined)
5147
{
52-
console.log("(init) No options found, initializing...");
48+
console.info("No options found, initializing...");
5349
storage.options = {};
5450
await browser.storage.local.set({"options": storage.options});
5551
}
5652

5753
if(storage.advanced === undefined)
5854
{
59-
console.log("(init) No advanced settings found, initializing...");
55+
console.info("No advanced settings found, initializing...");
6056
storage.advanced = {};
6157
await browser.storage.local.set({"advanced": storage.advanced});
6258
}
6359

64-
console.log("(init) Initialization complete...");
65-
console.log("(init) Storage version: " + storage.version);
66-
console.log("(init) Preset count: " + storage.presets.length);
67-
console.log("(init) Option count: " + Object.keys(storage.options).length);
68-
console.log("(init) Advanced count: " + Object.keys(storage.advanced).length);
60+
applyQuickResizeSetting();
61+
62+
console.debug("Initialization complete...");
63+
console.debug(" Storage version: " + storage.version);
64+
console.debug(" Preset count: " + storage.presets.length);
65+
console.debug(" Option count: " + Object.keys(storage.options).length);
66+
console.debug(" Advanced count: " + Object.keys(storage.advanced).length);
6967
}
7068

69+
// Hotkey handler
7170
async function handleCommand(command)
7271
{
73-
console.log("(background) handling command: " + command);
72+
console.debug("handling command: " + command);
7473

7574
if(command.startsWith(PREFIX_PRESET))
7675
{
77-
let presetNum = parseInt(command.substring(PREFIX_PRESET.length));
78-
let storage = await browser.storage.local.get("presets");
79-
let presets = storage.presets;
76+
const presetNum = parseInt(command.substring(PREFIX_PRESET.length));
77+
const storage = await browser.storage.local.get("presets");
78+
const presets = storage.presets;
8079
if(presetNum <= presets.length)
8180
{
82-
let presetId = presets[presetNum - 1].id;
81+
const presetId = presets[presetNum - 1].id;
8382
applyPreset(presetId);
8483
}
8584
}
8685
}
8786

87+
async function toolbarClickHandler()
88+
{
89+
console.debug("Toolbar clicked");
90+
91+
const storage = await browser.storage.local.get(["presets","options"]);
92+
const presets = storage.presets;
93+
const options = storage.options;
94+
95+
useQuickResizeMode = options.useQuickResizeMode !== undefined && options.useQuickResizeMode;
96+
if(useQuickResizeMode && presets.length > 0)
97+
{
98+
applyPreset(presets[0].id);
99+
}
100+
else
101+
{
102+
browser.browserAction.openPopup();
103+
}
104+
}
105+
88106
browser.runtime.onInstalled.addListener(initialize);
89107
browser.commands.onCommand.addListener(handleCommand);
108+
browser.browserAction.onClicked.addListener(toolbarClickHandler);
109+
110+
const manifest = browser.runtime.getManifest();
111+
console.info(manifest.name + " version " + manifest.version + " by " + manifest.author);

common.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1-
const PREFIX_PRESET = "preset-"; // Must match the one in options.js
1+
"use strict";
2+
3+
const CURRENT_STORAGE_VERSION = 2;
4+
5+
const MSGTYPE_APPLY_PRESET = "applyPreset";
6+
const MSGTYPE_GET_SHORTCUT = "getShortcutKey";
7+
8+
const PREFIX_PRESET = "preset-";
9+
const PREFIX_EDIT = "edit-";
10+
const PREFIX_UPDATE = "update-";
11+
const PREFIX_RESTORE = "restore-";
12+
const PREFIX_REMOVE = "remove-";
13+
const PREFIX_SAVE = "save-";
14+
const PREFIX_CANCEL = "cancel-";
15+
const PREFIX_DOWN = "down-"
16+
const PREFIX_UP = "up-"
17+
18+
const DEFAULT_POPUP = "toolbar_menu.html";
19+
20+
async function applyQuickResizeSetting()
21+
{
22+
const storage = await browser.storage.local.get(["options","presets"]);
23+
const options = storage.options;
24+
const presets = storage.presets;
25+
26+
let newTitle = null; // Says to use default from manifest
27+
let newPopup = ""; // Says to not have one
28+
if(options.useQuickResizeMode && presets.length > 0)
29+
{
30+
newTitle = await browser.browserAction.getTitle({}) + " - Quick Resize mode active, click to apply preset";
31+
}
32+
else
33+
{
34+
newPopup = DEFAULT_POPUP;
35+
}
36+
37+
browser.browserAction.setTitle({ title: newTitle });
38+
browser.browserAction.setPopup({ popup: newPopup });
39+
}
240

341
async function applyPreset(presetId)
442
{
5-
console.log("(common) applying preset: " + presetId);
43+
console.debug("Applying preset: " + presetId);
644

7-
let storage = await browser.storage.local.get("presets");
8-
let presets = storage.presets;
45+
const storage = await browser.storage.local.get("presets");
46+
const presets = storage.presets;
947
for(let i = 0;i < presets.length;i++)
1048
{
1149
if(presets[i].id == presetId)
@@ -18,24 +56,24 @@ async function applyPreset(presetId)
1856

1957
async function resizeWindow(newWidth, newHeight)
2058
{
21-
console.log("(common) resizing to: " + newWidth + "x" + newHeight);
59+
console.debug("Resizing to: " + newWidth + "x" + newHeight);
2260

23-
let updateInfo = {
61+
const updateInfo = {
2462
"width": parseInt(newWidth),
2563
"height": parseInt(newHeight)
2664
};
2765

28-
let currentWindow = await browser.windows.getCurrent();
66+
const currentWindow = await browser.windows.getCurrent();
2967
await browser.windows.update(currentWindow.id, updateInfo);
3068
}
3169

3270
async function getShortcutKey(commandName)
3371
{
34-
console.log("(common) retrieving shortcut for " + commandName);
72+
console.debug("Retrieving shortcut for " + commandName);
3573

3674
let shortcutKey = "(none)";
3775

38-
let commands = await browser.commands.getAll();
76+
const commands = await browser.commands.getAll();
3977
for(let command of commands)
4078
{
4179
if(command.name == commandName)

manifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "WindowSizer",
4-
"version": "1.4.2",
4+
"version": "1.5.0",
55
"author": "Dr. Cheap",
66
"description": "Quickly resize the window to one of your favorite sizes",
77
"homepage_url": "https://github.com/drcheap/WindowSizer",
@@ -30,7 +30,6 @@
3030
"browser_action": {
3131
"browser_style": true,
3232
"default_title": "WindowSizer",
33-
"default_popup": "toolbar_menu.html",
3433
"default_icon": {
3534
"16": "icons/ws_icon_16_dark.png",
3635
"32": "icons/ws_icon_32_dark.png"

options.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ th, td {
3232
margin-top: 0.5em;
3333
}
3434

35-
#currentSize, #getCurrentSize {
35+
#currentSize, #getCurrentSize, #quickResizeInfo {
3636
color: rgb(0, 0, 238);
3737
cursor: pointer;
3838
}
3939

40+
#quickResizeInfo {
41+
cursor: help;
42+
}
43+
4044
.imgDown, .imgUp {
4145
cursor: pointer;
4246
}

options.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<fieldset>
4545
<legend>General Options</legend>
4646
<form>
47+
<input type="checkbox" id="useQuickResizeMode" value="yes"> Use Quick Resize mode for toolbar (<span id="quickResizeInfo">what is this?</span>)<br>
4748
<input type="checkbox" id="keepActionMenuOnClick" value="yes"> Keep toolbar menu open after choosing a preset
4849
</form>
4950
</fieldset>
@@ -90,6 +91,7 @@
9091
</div>
9192
</fieldset>
9293

94+
<script src="common.js"></script>
9395
<script src="options.js"></script>
9496
</body>
9597
</html>

0 commit comments

Comments
 (0)