bitwarden-estensione-browser/src/popup/vault/current-tab.component.ts

189 lines
6.0 KiB
TypeScript
Raw Normal View History

2018-04-05 04:59:42 +02:00
import {
2018-04-06 21:33:20 +02:00
ChangeDetectorRef,
2018-04-05 04:59:42 +02:00
Component,
NgZone,
OnDestroy,
OnInit,
} from '@angular/core';
import { Router } from '@angular/router';
2018-04-06 20:03:35 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { BrowserApi } from '../../browser/browserApi';
2018-04-06 21:33:20 +02:00
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
2018-04-06 20:03:35 +02:00
import { CipherType } from 'jslib/enums/cipherType';
import { CipherView } from 'jslib/models/view/cipherView';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { AutofillService } from '../../services/abstractions/autofill.service';
import { PopupUtilsService } from '../services/popup-utils.service';
2018-04-06 21:33:20 +02:00
import { setTimeout } from 'timers';
const BroadcasterSubscriptionId = 'CurrentTabComponent';
2018-04-06 20:03:35 +02:00
2018-04-05 04:59:42 +02:00
@Component({
2018-04-06 20:03:35 +02:00
selector: 'app-current-tab',
templateUrl: 'current-tab.component.html',
2018-04-05 04:59:42 +02:00
})
2018-04-06 21:33:20 +02:00
export class CurrentTabComponent implements OnInit, OnDestroy {
2018-04-06 20:03:35 +02:00
pageDetails: any[] = [];
2018-04-06 20:18:28 +02:00
cardCiphers: CipherView[];
identityCiphers: CipherView[];
loginCiphers: CipherView[];
2018-04-06 20:03:35 +02:00
url: string;
domain: string;
2018-04-09 16:50:28 +02:00
searchText: string;
2018-04-06 20:03:35 +02:00
canAutofill = false;
inSidebar = false;
showPopout = true;
disableSearch = false;
loaded = false;
constructor(private platformUtilsService: PlatformUtilsService, private cipherService: CipherService,
private popupUtilsService: PopupUtilsService, private autofillService: AutofillService,
private analytics: Angulartics2, private toasterService: ToasterService,
2018-04-06 21:33:20 +02:00
private i18nService: I18nService, private router: Router,
private ngZone: NgZone, private broadcasterService: BroadcasterService,
private changeDetectorRef: ChangeDetectorRef) {
2018-04-06 20:03:35 +02:00
this.inSidebar = popupUtilsService.inSidebar(window);
this.showPopout = !this.inSidebar && !platformUtilsService.isSafari();
this.disableSearch = platformUtilsService.isEdge();
}
ngOnInit() {
2018-04-06 21:33:20 +02:00
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'syncCompleted':
if (this.loaded) {
setTimeout(() => {
this.load();
}, 500);
}
break;
case 'collectPageDetailsResponse':
if (message.sender === BroadcasterSubscriptionId) {
this.pageDetails.push({
frameId: message.webExtSender.frameId,
tab: message.tab,
details: message.details,
});
}
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
2018-04-06 20:03:35 +02:00
this.load();
}
2018-04-06 21:33:20 +02:00
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
}
2018-04-06 20:03:35 +02:00
async refresh() {
await this.load();
}
addCipher() {
2018-04-10 06:04:49 +02:00
this.router.navigate(['/add-cipher'], { queryParams: { name: this.domain, uri: this.url } });
2018-04-06 20:03:35 +02:00
}
2018-04-05 04:59:42 +02:00
2018-04-06 20:03:35 +02:00
viewCipher(cipher: CipherView) {
this.router.navigate(['/view-cipher'], { queryParams: { cipherId: cipher.id } });
2018-04-05 04:59:42 +02:00
}
2018-04-06 20:03:35 +02:00
async fillCipher(cipher: CipherView) {
if (!this.canAutofill) {
this.analytics.eventTrack.next({ action: 'Autofilled Error' });
this.toasterService.popAsync('error', null, this.i18nService.t('autofillError'));
return;
}
try {
const totpCode = await this.autofillService.doAutoFill({
cipher: cipher,
pageDetails: this.pageDetails,
fromBackground: false,
doc: window.document,
});
this.analytics.eventTrack.next({ action: 'Autofilled' });
if (totpCode != null && this.platformUtilsService.isFirefox()) {
this.platformUtilsService.copyToClipboard(totpCode, { doc: window.document });
}
if (this.popupUtilsService.inPopup(window)) {
BrowserApi.closePopup(window);
}
} catch {
this.analytics.eventTrack.next({ action: 'Autofilled Error' });
this.toasterService.popAsync('error', null, this.i18nService.t('autofillError'));
}
}
searchVault() {
2018-04-09 16:50:28 +02:00
this.router.navigate(['/tabs/vault'], { queryParams: { searchText: this.searchText } });
2018-04-06 20:03:35 +02:00
}
private async load() {
const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab) {
this.url = tab.url;
} else {
this.loaded = true;
return;
}
this.domain = this.platformUtilsService.getDomain(this.url);
BrowserApi.tabSendMessage(tab, {
command: 'collectPageDetails',
tab: tab,
2018-04-06 21:33:20 +02:00
sender: BroadcasterSubscriptionId,
2018-04-06 20:03:35 +02:00
}).then(() => {
this.canAutofill = true;
});
const ciphers = await this.cipherService.getAllDecryptedForUrl(this.url, [
CipherType.Card,
CipherType.Identity,
]);
2018-04-06 20:18:28 +02:00
this.loginCiphers = [];
this.cardCiphers = [];
this.identityCiphers = [];
2018-04-06 20:03:35 +02:00
ciphers.forEach((c) => {
switch (c.type) {
case CipherType.Login:
this.loginCiphers.push(c);
break;
case CipherType.Card:
this.cardCiphers.push(c);
break;
case CipherType.Identity:
this.identityCiphers.push(c);
break;
default:
break;
}
});
this.loaded = true;
}
2018-04-05 04:59:42 +02:00
}