From 77408cdb73ecc0508a82e53ac63c1c235af78457 Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Fri, 18 Mar 2022 14:25:51 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD=20=D1=80=D0=B5=D0=B7=D0=BE=D0=BB?= =?UTF-8?q?=D0=B2=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- popup/src/api/DomainResolver.ts | 42 +++++++++++++++++++++++++++++++++ popup/src/api/HttpClient.ts | 11 +++++++++ 2 files changed, 53 insertions(+) create mode 100644 popup/src/api/DomainResolver.ts diff --git a/popup/src/api/DomainResolver.ts b/popup/src/api/DomainResolver.ts new file mode 100644 index 0000000..1a58258 --- /dev/null +++ b/popup/src/api/DomainResolver.ts @@ -0,0 +1,42 @@ +export class DomainResolver { + resolve (): Promise { + const promise = new Promise((resolve) => { + if (typeof chrome === 'undefined') { + resolve(null) + return + } + if (typeof chrome.tabs === 'undefined') { + resolve(null) + return + } + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + for (const tab of tabs) { + let url = tab.url || '' + const parts = url.match(/http:\/\/.*?\.(.*?)\..*?/) || [] + const nickname = parts[1] || null + const isPlacetgroup = url.indexOf('laen') > -1 || + url.indexOf('smsmoney') > -1 || + url.indexOf('smsraha') > -1 || + url.indexOf('placetgroup') > -1 + const project = isPlacetgroup ? 'placetgroup' : 'nordecum' + const domain = isPlacetgroup ? 'com' : 'lt' + if (nickname && url.indexOf('.sv') > -1) { + url = project + '.' + nickname + '.sv' + } else { + url = project + '.' + nickname + '.' + domain + } + resolve(url) + return + } + }) + }) + // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // @ts-ignore + return promise.then((url) => { + console.log('url', url) + return url + }) + } +} + +export default new DomainResolver() diff --git a/popup/src/api/HttpClient.ts b/popup/src/api/HttpClient.ts index e6c3405..09a47ed 100644 --- a/popup/src/api/HttpClient.ts +++ b/popup/src/api/HttpClient.ts @@ -2,6 +2,7 @@ import { SchemaInterface } from './SchemaInterface' import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios' import stubResolver from '@/stubs/StubResolver' +import domainResolver from '@/api/DomainResolver' export abstract class Handler { abstract handle: any @@ -42,7 +43,17 @@ export class HttpClient { this.baseUrl = baseUrl } + async syncBaseUrlWithStand () { + await domainResolver + .resolve() + .then((url) => { + this.baseUrl = url || this.baseUrl + }) + } + send (schema: SchemaInterface, data: any | null = null): Promise { + this.syncBaseUrlWithStand() + let url = schema.url const preparedData = data for (const key in data) {