bitwarden-estensione-browser/src/models/view/loginUriView.ts

98 lines
2.5 KiB
TypeScript
Raw Normal View History

import { UriMatchType } from '../../enums/uriMatchType';
import { View } from './view';
import { LoginUri } from '../domain/loginUri';
2018-04-23 19:03:47 +02:00
import { Utils } from '../../misc/utils';
2018-04-19 20:40:42 +02:00
2018-11-02 13:39:09 +01:00
const CanLaunchWhitelist = [
'https://',
'http://',
'ssh://',
'ftp://',
'sftp://',
'irc://',
'chrome://',
];
export class LoginUriView implements View {
match: UriMatchType = null;
// tslint:disable
2019-01-25 15:30:21 +01:00
private _uri: string = null;
private _domain: string = null;
private _hostname: string = null;
private _canLaunch: boolean = null;
// tslint:enable
constructor(u?: LoginUri) {
if (!u) {
return;
}
this.match = u.match;
}
get uri(): string {
return this._uri;
}
set uri(value: string) {
this._uri = value;
this._domain = null;
2018-11-02 18:03:00 +01:00
this._canLaunch = null;
}
get domain(): string {
if (this._domain == null && this.uri != null) {
2018-10-14 04:21:54 +02:00
this._domain = Utils.getDomain(this.uri);
if (this._domain === '') {
this._domain = null;
}
}
return this._domain;
}
2018-04-19 20:40:42 +02:00
get hostname(): string {
if (this._hostname == null && this.uri != null) {
2018-04-23 19:03:47 +02:00
this._hostname = Utils.getHostname(this.uri);
2018-04-19 20:40:42 +02:00
if (this._hostname === '') {
this._hostname = null;
}
}
return this._hostname;
}
get hostnameOrUri(): string {
return this.hostname != null ? this.hostname : this.uri;
}
get isWebsite(): boolean {
return this.uri != null && (this.uri.indexOf('http://') === 0 || this.uri.indexOf('https://') === 0 ||
(this.uri.indexOf('://') < 0 && Utils.tldEndingRegex.test(this.uri)));
}
get canLaunch(): boolean {
2018-11-02 18:03:00 +01:00
if (this._canLaunch != null) {
return this._canLaunch;
2018-11-02 13:39:09 +01:00
}
2019-02-08 14:03:12 +01:00
if (this.uri != null && this.match !== UriMatchType.RegularExpression) {
const uri = this.launchUri;
2018-11-02 18:03:00 +01:00
for (let i = 0; i < CanLaunchWhitelist.length; i++) {
if (uri.indexOf(CanLaunchWhitelist[i]) === 0) {
2018-11-02 18:03:00 +01:00
this._canLaunch = true;
return this._canLaunch;
}
2018-11-02 13:39:09 +01:00
}
}
2018-11-02 18:03:00 +01:00
this._canLaunch = false;
return this._canLaunch;
}
get launchUri(): string {
return this.uri.indexOf('://') < 0 && Utils.tldEndingRegex.test(this.uri) ? ('http://' + this.uri) : this.uri;
}
}