2020-08-31 09:14:50 +02:00
|
|
|
import { Component, NgZone, OnDestroy, OnInit } from "@angular/core";
|
2021-03-02 19:31:52 +01:00
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
import { Router } from "@angular/router";
|
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { ConstantsService } from "jslib-common/services/constants.service";
|
2021-03-02 19:31:52 +01:00
|
|
|
|
2021-12-06 12:21:07 +01:00
|
|
|
import { BroadcasterService } from "jslib-common/abstractions/broadcaster.service";
|
2021-06-07 19:25:37 +02:00
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
|
|
import { StorageService } from "jslib-common/abstractions/storage.service";
|
2021-03-02 19:31:52 +01:00
|
|
|
|
2021-01-13 09:40:10 +01:00
|
|
|
import { BrowserApi } from "../../browser/browserApi";
|
2021-03-02 19:31:52 +01:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
2020-08-31 09:14:50 +02:00
|
|
|
|
|
|
|
interface ExcludedDomain {
|
2021-01-14 09:04:30 +01:00
|
|
|
uri: string;
|
|
|
|
showCurrentUris: boolean;
|
2020-08-31 09:14:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const BroadcasterSubscriptionId = "excludedDomains";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-excluded-domains",
|
|
|
|
templateUrl: "excluded-domains.component.html",
|
|
|
|
})
|
|
|
|
export class ExcludedDomainsComponent implements OnInit, OnDestroy {
|
|
|
|
excludedDomains: ExcludedDomain[] = [];
|
|
|
|
currentUris: string[];
|
|
|
|
loadCurrentUrisTimeout: number;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
constructor(
|
|
|
|
private storageService: StorageService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private router: Router,
|
|
|
|
private broadcasterService: BroadcasterService,
|
|
|
|
private ngZone: NgZone,
|
|
|
|
private platformUtilsService: PlatformUtilsService
|
2021-12-21 15:43:35 +01:00
|
|
|
) {}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
const savedDomains = await this.storageService.get<any>(ConstantsService.neverDomainsKey);
|
|
|
|
if (savedDomains) {
|
|
|
|
for (const uri of Object.keys(savedDomains)) {
|
2021-01-14 09:04:30 +01:00
|
|
|
this.excludedDomains.push({ uri: uri, showCurrentUris: false });
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-08-31 09:14:50 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 09:04:30 +01:00
|
|
|
await this.loadCurrentUris();
|
2020-08-31 09:14:50 +02:00
|
|
|
|
|
|
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
2021-01-14 09:04:30 +01:00
|
|
|
switch (message.command) {
|
2020-08-31 09:14:50 +02:00
|
|
|
case "tabChanged":
|
2021-01-14 09:04:30 +01:00
|
|
|
case "windowChanged":
|
|
|
|
if (this.loadCurrentUrisTimeout != null) {
|
|
|
|
window.clearTimeout(this.loadCurrentUrisTimeout);
|
2020-08-31 09:14:50 +02:00
|
|
|
}
|
2021-01-14 09:04:30 +01:00
|
|
|
this.loadCurrentUrisTimeout = window.setTimeout(
|
|
|
|
async () => await this.loadCurrentUris(),
|
2021-12-21 15:43:35 +01:00
|
|
|
500
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-31 09:14:50 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
async addUri() {
|
|
|
|
this.excludedDomains.push({ uri: "", showCurrentUris: false });
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
async removeUri(i: number) {
|
|
|
|
this.excludedDomains.splice(i, 1);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
async submit() {
|
|
|
|
const savedDomains: { [name: string]: null } = {};
|
|
|
|
for (const domain of this.excludedDomains) {
|
|
|
|
if (domain.uri && domain.uri !== "") {
|
2021-03-02 19:31:52 +01:00
|
|
|
const validDomain = Utils.getHostname(domain.uri);
|
2020-08-31 09:14:50 +02:00
|
|
|
if (!validDomain) {
|
2021-03-02 19:31:52 +01:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-21 15:43:35 +01:00
|
|
|
"error",
|
2020-08-31 09:14:50 +02:00
|
|
|
null,
|
|
|
|
this.i18nService.t("excludedDomainsInvalidDomain", domain.uri)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-14 09:04:30 +01:00
|
|
|
savedDomains[validDomain] = null;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 09:14:50 +02:00
|
|
|
await this.storageService.save(ConstantsService.neverDomainsKey, savedDomains);
|
|
|
|
this.router.navigate(["/tabs/settings"]);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
trackByFunction(index: number, item: any) {
|
|
|
|
return index;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
toggleUriInput(domain: ExcludedDomain) {
|
|
|
|
domain.showCurrentUris = !domain.showCurrentUris;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:14:50 +02:00
|
|
|
async loadCurrentUris() {
|
|
|
|
const tabs = await BrowserApi.tabsQuery({ windowType: "normal" });
|
|
|
|
if (tabs) {
|
2021-03-02 19:31:52 +01:00
|
|
|
const uriSet = new Set(tabs.map((tab) => Utils.getHostname(tab.url)));
|
2020-08-31 09:14:50 +02:00
|
|
|
uriSet.delete(null);
|
2021-01-14 09:04:30 +01:00
|
|
|
this.currentUris = Array.from(uriSet);
|
2020-08-31 09:14:50 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-01-13 09:40:10 +01:00
|
|
|
}
|