Skip to content

Commit 995a4c3

Browse files
authored
Autosubmit forms by faking an Enter keypress (#55)
1 parent 64a8cdd commit 995a4c3

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/background.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,33 @@ async function dispatchFocusOrSubmit(settings, request, allFrames, allowForeign)
240240
foreignFills: settings.foreignFills[settings.host] || {}
241241
});
242242

243-
await chrome.tabs.executeScript(settings.tab.id, {
243+
let perFrameResults = await chrome.tabs.executeScript(settings.tab.id, {
244244
allFrames: allFrames,
245245
code: `window.browserpass.focusOrSubmit(${JSON.stringify(request)});`
246246
});
247+
248+
// if necessary, dispatch Enter keypress to autosubmit the form
249+
// currently only works on Chromium and requires debugger permission
250+
try {
251+
for (let frame of perFrameResults) {
252+
if (frame.needPressEnter) {
253+
chrome.debugger.attach({ tabId: settings.tab.id }, "1.2");
254+
for (let type of ["keyDown", "keyUp"]) {
255+
chrome.debugger.sendCommand(
256+
{ tabId: settings.tab.id },
257+
"Input.dispatchKeyEvent",
258+
{
259+
type: type,
260+
windowsVirtualKeyCode: 13,
261+
nativeVirtualKeyCode: 13
262+
}
263+
);
264+
}
265+
chrome.debugger.detach({ tabId: settings.tab.id });
266+
break;
267+
}
268+
}
269+
} catch (e) {}
247270
}
248271

249272
/**

src/inject.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,13 @@
136136
* @since 3.0.0
137137
*
138138
* @param object request Form fill request
139-
* @return void
139+
* @return object result of focusing or submitting a form
140140
*/
141141
function focusOrSubmit(request) {
142+
var result = {
143+
needPressEnter: false
144+
};
145+
142146
// get the login form
143147
var loginForm = form();
144148

@@ -167,6 +171,10 @@
167171
}
168172
} else {
169173
// There is no submit button.
174+
if (request.autoSubmit) {
175+
// signal background script that we want it to press Enter for us
176+
result.needPressEnter = true;
177+
}
170178
// We need to keep focus somewhere within the form, so that Enter hopefully submits the form.
171179
var password = find(PASSWORD_FIELDS, loginForm);
172180
if (password) {
@@ -179,6 +187,8 @@
179187
}
180188
}
181189
}
190+
191+
return result;
182192
}
183193

184194
/**

src/manifest-chromium.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"open_in_tab": false
2424
},
2525
"permissions": [
26+
"debugger",
2627
"activeTab",
2728
"tabs",
2829
"clipboardWrite",

0 commit comments

Comments
 (0)