diff --git a/src/popup/vault/view.component.ts b/src/popup/vault/view.component.ts
index d4fc9bdcae..abc2608bc2 100644
--- a/src/popup/vault/view.component.ts
+++ b/src/popup/vault/view.component.ts
@@ -38,7 +38,9 @@ const BroadcasterSubscriptionId = 'ChildViewComponent';
export class ViewComponent extends BaseViewComponent {
showAttachments = true;
pageDetails: any[] = [];
- inPopout: boolean = false;
+ tab: any;
+ loadPageDetailsTimeout: number;
+ inPopout = false;
constructor(cipherService: CipherService, totpService: TotpService,
tokenService: TokenService, i18nService: I18nService,
@@ -68,6 +70,7 @@ export class ViewComponent extends BaseViewComponent {
queryParamsSub.unsubscribe();
}
});
+
super.ngOnInit();
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
@@ -82,6 +85,13 @@ export class ViewComponent extends BaseViewComponent {
});
}
break;
+ case 'tabChanged':
+ case 'windowChanged':
+ if (this.loadPageDetailsTimeout != null) {
+ window.clearTimeout(this.loadPageDetailsTimeout);
+ }
+ this.loadPageDetailsTimeout = window.setTimeout(() => this.loadPageDetails(), 500);
+ break;
default:
break;
}
@@ -90,8 +100,13 @@ export class ViewComponent extends BaseViewComponent {
}
ngOnDestroy() {
- this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
super.ngOnDestroy();
+ this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
+ }
+
+ async load() {
+ await super.load();
+ await this.loadPageDetails();
}
edit() {
@@ -116,43 +131,40 @@ export class ViewComponent extends BaseViewComponent {
}
async fillCipher() {
- const didAutofill: boolean = await this.doAutofill();
+ const didAutofill = await this.doAutofill();
if (didAutofill) {
this.platformUtilsService.showToast('success', null,
this.i18nService.t('autoFillSuccess'));
- }
+ }
}
async fillCipherAndSave() {
- const didAutofill: boolean = await this.doAutofill();
+ const didAutofill = await this.doAutofill();
if (didAutofill) {
- const tab = await BrowserApi.getTabFromCurrentWindow();
- if (!tab) {
+ if (this.tab == null) {
throw new Error('No tab found.');
}
if (this.cipher.login.uris == null) {
this.cipher.login.uris = [];
} else {
- if (this.cipher.login.uris.some((uri) => uri.uri === tab.url)) {
+ if (this.cipher.login.uris.some((uri) => uri.uri === this.tab.url)) {
this.platformUtilsService.showToast('success', null,
- this.i18nService.t('savedURI'));
+ this.i18nService.t('autoFillSuccessAndSavedUri'));
return;
}
}
- const loginUri: LoginUriView = new LoginUriView();
- loginUri.uri = tab.url;
+ const loginUri = new LoginUriView();
+ loginUri.uri = this.tab.url;
this.cipher.login.uris.push(loginUri);
try {
const cipher: Cipher = await this.cipherService.encrypt(this.cipher);
await this.cipherService.saveWithServer(cipher);
- this.cipher.id = cipher.id;
-
this.platformUtilsService.showToast('success', null,
- this.i18nService.t('savedURI'));
+ this.i18nService.t('autoFillSuccessAndSavedUri'));
this.messagingService.send('editedCipher');
} catch {
this.platformUtilsService.showToast('error', null,
@@ -161,45 +173,6 @@ export class ViewComponent extends BaseViewComponent {
}
}
- async doAutofill() {
- if (this.pageDetails == null || this.pageDetails.length === 0) {
- this.platformUtilsService.showToast('error', null,
- this.i18nService.t('autofillError'));
- return false;
- }
-
- try {
- this.totpCode = await this.autofillService.doAutoFill({
- cipher: this.cipher,
- pageDetails: this.pageDetails,
- doc: window.document,
- });
- if (this.totpCode != null) {
- this.platformUtilsService.copyToClipboard(this.totpCode, { window: window });
- }
- } catch {
- this.platformUtilsService.showToast('error', null,
- this.i18nService.t('autofillError'));
- this.changeDetectorRef.detectChanges();
- return false;
- }
-
- return true;
- }
-
- async load() {
- await super.load();
-
- const tab = await BrowserApi.getTabFromCurrentWindow();
-
- this.pageDetails = [];
- BrowserApi.tabSendMessage(tab, {
- command: 'collectPageDetails',
- tab: tab,
- sender: BroadcasterSubscriptionId,
- });
- }
-
async restore() {
if (!this.cipher.isDeleted) {
return false;
@@ -222,4 +195,43 @@ export class ViewComponent extends BaseViewComponent {
close() {
this.location.back();
}
+
+ private async loadPageDetails() {
+ this.pageDetails = [];
+ this.tab = await BrowserApi.getTabFromCurrentWindow();
+ if (this.tab == null) {
+ return;
+ }
+ BrowserApi.tabSendMessage(this.tab, {
+ command: 'collectPageDetails',
+ tab: this.tab,
+ sender: BroadcasterSubscriptionId,
+ });
+ }
+
+ private async doAutofill() {
+ if (this.pageDetails == null || this.pageDetails.length === 0) {
+ this.platformUtilsService.showToast('error', null,
+ this.i18nService.t('autofillError'));
+ return false;
+ }
+
+ try {
+ this.totpCode = await this.autofillService.doAutoFill({
+ cipher: this.cipher,
+ pageDetails: this.pageDetails,
+ doc: window.document,
+ });
+ if (this.totpCode != null) {
+ this.platformUtilsService.copyToClipboard(this.totpCode, { window: window });
+ }
+ } catch {
+ this.platformUtilsService.showToast('error', null,
+ this.i18nService.t('autofillError'));
+ this.changeDetectorRef.detectChanges();
+ return false;
+ }
+
+ return true;
+ }
}