Skip to content

Commit 8fd08cb

Browse files
committed
Merge branch 'features/keybindings' of https://github.com/artemisbot/CyberChef into artemisbot-features/keybindings
2 parents 7a2b75c + 8b30fdf commit 8fd08cb

File tree

4 files changed

+246
-24
lines changed

4 files changed

+246
-24
lines changed

src/web/BindingsWaiter.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/**
2+
* Waiter to handle keybindings to CyberChef functions (i.e. Bake, Step, Save, Load etc.)
3+
*
4+
* @author Matt C [matt@artemisbot.uk]
5+
* @copyright Crown Copyright 2016
6+
* @license Apache-2.0
7+
*
8+
* @constructor
9+
* @param {App} app - The main view object for CyberChef.
10+
* @param {Manager} manager - The CyberChef event manager.
11+
*/
12+
const BindingsWaiter = function (app, manager) {
13+
this.app = app;
14+
this.manager = manager;
15+
};
16+
17+
18+
/**
19+
* Handler for all keydown events
20+
* Checks whether valid keyboard shortcut has been instated
21+
*
22+
* @fires Manager#statechange
23+
* @param {event} e
24+
*/
25+
BindingsWaiter.prototype.parseInput = function(e) {
26+
let modKey = e.altKey;
27+
if (this.app.options.useMetaKey) modKey = e.metaKey;
28+
if (e.ctrlKey && modKey) {
29+
let elem;
30+
switch (e.code) {
31+
case "KeyF":
32+
e.preventDefault();
33+
document.getElementById("search").focus(); // Focus search
34+
break;
35+
case "KeyI":
36+
e.preventDefault();
37+
document.getElementById("input-text").focus(); // Focus input
38+
break;
39+
case "KeyO":
40+
e.preventDefault();
41+
document.getElementById("output-text").focus(); // Focus output
42+
break;
43+
case "Period":
44+
try {
45+
elem = document.activeElement.closest(".operation");
46+
if (elem.parentNode.lastChild === elem) {
47+
elem.parentNode.firstChild.querySelectorAll(".arg")[0].focus(); // if operation is last in recipe, loop around to the top operation's first argument
48+
} else {
49+
elem.nextSibling.querySelectorAll(".arg")[0].focus(); //focus first argument of next operation
50+
}
51+
} catch (e) {
52+
// do nothing, just don't throw an error
53+
}
54+
break;
55+
case "KeyB":
56+
try {
57+
elem = document.activeElement.closest(".operation").querySelectorAll(".breakpoint")[0];
58+
if (elem.getAttribute("break") === "false") {
59+
elem.setAttribute("break", "true"); // add break point if not already enabled
60+
elem.classList.add("breakpoint-selected");
61+
} else {
62+
elem.setAttribute("break", "false"); // remove break point if already enabled
63+
elem.classList.remove("breakpoint-selected");
64+
}
65+
window.dispatchEvent(this.manager.statechange);
66+
} catch (e) {
67+
// do nothing, just don't throw an error
68+
}
69+
break;
70+
case "KeyD":
71+
try {
72+
elem = document.activeElement.closest(".operation").querySelectorAll(".disable-icon")[0];
73+
if (elem.getAttribute("disabled") === "false") {
74+
elem.setAttribute("disabled", "true"); // disable operation if enabled
75+
elem.classList.add("disable-elem-selected");
76+
elem.parentNode.parentNode.classList.add("disabled");
77+
} else {
78+
elem.setAttribute("disabled", "false"); // enable operation if disabled
79+
elem.classList.remove("disable-elem-selected");
80+
elem.parentNode.parentNode.classList.remove("disabled");
81+
}
82+
this.app.progress = 0;
83+
window.dispatchEvent(this.manager.statechange);
84+
} catch (e) {
85+
// do nothing, just don't throw an error
86+
}
87+
break;
88+
case "Space":
89+
this.app.bake(); // bake the recipe
90+
break;
91+
case "Quote":
92+
this.app.bake(true); // step through the recipe
93+
break;
94+
case "KeyC":
95+
this.manager.recipe.clearRecipe(); // clear recipe
96+
break;
97+
case "KeyS":
98+
this.manager.output.saveClick(); // save output to file
99+
break;
100+
case "KeyL":
101+
this.manager.controls.loadClick(); // load a recipe
102+
break;
103+
case "KeyM":
104+
this.manager.controls.switchClick(); // switch input & output
105+
break;
106+
default:
107+
if (e.code.match(/Digit[0-9]/g)) {
108+
e.preventDefault();
109+
try {
110+
document.querySelector(`li:nth-child(${e.code.substr(-1)}) .arg`).focus(); // select the first argument of the operation corresponding to the number pressed
111+
} catch (e) {
112+
// do nothing, just don't throw an error
113+
}
114+
}
115+
break;
116+
}
117+
}
118+
};
119+
120+
/**
121+
* Updates keybinding list when metaKey option is toggled
122+
*
123+
*/
124+
BindingsWaiter.prototype.updateKeybList = function() {
125+
let modWinLin = "Alt";
126+
let modMac = "Opt";
127+
if (this.app.options.useMetaKey) {
128+
modWinLin = "Win";
129+
modMac = "Cmd";
130+
}
131+
document.getElementById("keybList").innerHTML = `
132+
<tr>
133+
<td><b>Command</b></td>
134+
<td><b>Shortcut (Win/Linux)</b></td>
135+
<td><b>Shortcut (Mac)</b></td>
136+
</tr>
137+
<tr>
138+
<td>Place cursor in search field</td>
139+
<td>Ctrl+${modWinLin}+f</td>
140+
<td>Ctrl+${modMac}+f</td>
141+
<tr>
142+
<td>Place cursor in input box</td>
143+
<td>Ctrl+${modWinLin}+i</td>
144+
<td>Ctrl+${modMac}+i</td>
145+
</tr>
146+
<tr>
147+
<td>Place cursor in output box</td>
148+
<td>Ctrl+${modWinLin}+o</td>
149+
<td>Ctrl+${modMac}+o</td>
150+
</tr>
151+
<tr>
152+
<td>Place cursor in first argument field<br>of the next operation in the recipe</td>
153+
<td>Ctrl+${modWinLin}+.</td>
154+
<td>Ctrl+${modMac}+.</td>
155+
</tr>
156+
<tr>
157+
<td>Place cursor in first argument field<br>of the nth operation in the recipe</td>
158+
<td>Ctrl+${modWinLin}+[1-9]</td>
159+
<td>Ctrl+${modMac}+[1-9]</td>
160+
</tr>
161+
<tr>
162+
<td>Disable current operation</td>
163+
<td>Ctrl+${modWinLin}+d</td>
164+
<td>Ctrl+${modMac}+d</td>
165+
</tr>
166+
<tr>
167+
<td>Set/clear breakpoint</td>
168+
<td>Ctrl+${modWinLin}+b</td>
169+
<td>Ctrl+${modMac}+b</td>
170+
</tr>
171+
<tr>
172+
<td>Bake</td>
173+
<td>Ctrl+${modWinLin}+Space</td>
174+
<td>Ctrl+${modMac}+Space</td>
175+
</tr>
176+
<tr>
177+
<td>Step</td>
178+
<td>Ctrl+${modWinLin}+'</td>
179+
<td>Ctrl+${modMac}+'</td>
180+
</tr>
181+
<tr>
182+
<td>Clear recipe</td>
183+
<td>Ctrl+${modWinLin}+c</td>
184+
<td>Ctrl+${modMac}+c</td>
185+
</tr>
186+
<tr>
187+
<td>Save to file</td>
188+
<td>Ctrl+${modWinLin}+s</td>
189+
<td>Ctrl+${modMac}+s</td>
190+
</tr>
191+
<tr>
192+
<td>Load recipe</td>
193+
<td>Ctrl+${modWinLin}+l</td>
194+
<td>Ctrl+${modMac}+l</td>
195+
</tr>
196+
<tr>
197+
<td>Move output to input</td>
198+
<td>Ctrl+${modWinLin}+m</td>
199+
<td>Ctrl+${modMac}+m</td>
200+
</tr>
201+
`;
202+
};
203+
204+
export default BindingsWaiter;

src/web/Manager.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import OutputWaiter from "./OutputWaiter.js";
88
import OptionsWaiter from "./OptionsWaiter.js";
99
import HighlighterWaiter from "./HighlighterWaiter.js";
1010
import SeasonalWaiter from "./SeasonalWaiter.js";
11+
import BindingsWaiter from "./BindingsWaiter.js";
1112

1213

1314
/**
@@ -60,6 +61,7 @@ const Manager = function(app) {
6061
this.options = new OptionsWaiter(this.app);
6162
this.highlighter = new HighlighterWaiter(this.app, this);
6263
this.seasonal = new SeasonalWaiter(this.app, this);
64+
this.bindings = new BindingsWaiter(this.app, this);
6365

6466
// Object to store dynamic handlers to fire on elements that may not exist yet
6567
this.dynamicHandlers = {};
@@ -76,6 +78,7 @@ Manager.prototype.setup = function() {
7678
this.recipe.initialiseOperationDragNDrop();
7779
this.controls.autoBakeChange();
7880
this.seasonal.load();
81+
this.bindings.updateKeybList();
7982
};
8083

8184

@@ -89,7 +92,6 @@ Manager.prototype.initialiseEventListeners = function() {
8992
window.addEventListener("focus", this.window.windowFocus.bind(this.window));
9093
window.addEventListener("statechange", this.app.stateChange.bind(this.app));
9194
window.addEventListener("popstate", this.app.popState.bind(this.app));
92-
9395
// Controls
9496
document.getElementById("bake").addEventListener("click", this.controls.bakeClick.bind(this.controls));
9597
document.getElementById("auto-bake").addEventListener("change", this.controls.autoBakeChange.bind(this.controls));
@@ -165,6 +167,10 @@ Manager.prototype.initialiseEventListeners = function() {
165167
this.addDynamicListener(".option-item select", "change", this.options.selectChange, this.options);
166168
document.getElementById("theme").addEventListener("change", this.options.themeChange.bind(this.options));
167169

170+
//Keybindings
171+
window.addEventListener("keydown", this.bindings.parseInput.bind(this.bindings));
172+
$(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox#useMetaKey", this.bindings.updateKeybList.bind(this.bindings));
173+
168174
// Misc
169175
document.getElementById("alert-close").addEventListener("click", this.app.alertCloseClick.bind(this.app));
170176
};

0 commit comments

Comments
 (0)