2023-01-31 22:08:37 +01:00
|
|
|
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
2023-01-07 01:31:32 +01:00
|
|
|
|
2023-01-31 22:08:37 +01:00
|
|
|
import AutofillPageDetails from "../models/autofill-page-details";
|
2022-08-10 03:30:26 +02:00
|
|
|
import { AutofillService } from "../services/abstractions/autofill.service";
|
|
|
|
|
2023-01-07 01:31:32 +01:00
|
|
|
export class AutofillTabCommand {
|
2022-08-10 03:30:26 +02:00
|
|
|
constructor(private autofillService: AutofillService) {}
|
|
|
|
|
2023-01-07 01:31:32 +01:00
|
|
|
async doAutofillTabCommand(tab: chrome.tabs.Tab) {
|
2022-08-10 03:30:26 +02:00
|
|
|
if (!tab.id) {
|
|
|
|
throw new Error("Tab does not have an id, cannot complete autofill.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const details = await this.collectPageDetails(tab.id);
|
|
|
|
await this.autofillService.doAutoFillOnTab(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
frameId: 0,
|
|
|
|
tab: tab,
|
|
|
|
details: details,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
tab,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-07 01:31:32 +01:00
|
|
|
async doAutofillTabWithCipherCommand(tab: chrome.tabs.Tab, cipher: CipherView) {
|
|
|
|
if (!tab.id) {
|
|
|
|
throw new Error("Tab does not have an id, cannot complete autofill.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const details = await this.collectPageDetails(tab.id);
|
|
|
|
await this.autofillService.doAutoFill({
|
|
|
|
tab: tab,
|
|
|
|
cipher: cipher,
|
|
|
|
pageDetails: [
|
|
|
|
{
|
|
|
|
frameId: 0,
|
|
|
|
tab: tab,
|
|
|
|
details: details,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
skipLastUsed: false,
|
|
|
|
skipUsernameOnlyFill: false,
|
|
|
|
onlyEmptyFields: false,
|
|
|
|
onlyVisibleFields: false,
|
|
|
|
fillNewPassword: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-10 03:30:26 +02:00
|
|
|
private async collectPageDetails(tabId: number): Promise<AutofillPageDetails> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.tabs.sendMessage(
|
|
|
|
tabId,
|
|
|
|
{
|
|
|
|
command: "collectPageDetailsImmediately",
|
|
|
|
},
|
|
|
|
(response: AutofillPageDetails) => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
reject(chrome.runtime.lastError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|