diff --git a/src/index.js b/src/index.js index 1265a6f..6fbfc5b 100644 --- a/src/index.js +++ b/src/index.js @@ -18,3 +18,7 @@ waitForKeyElements('call-view', (element) => { callNotification.showCallNotifica const callHistory = new CallHistory() // eslint-disable-next-line no-undef waitForKeyElements('.call-history-list call', (element) => { callHistory.showCallHistory(element) }, false) + +const status = new Status() +// eslint-disable-next-line no-undef +waitForKeyElements('#status-change', (element) => { status.showStatus(element) }, false) diff --git a/src/status.ts b/src/status.ts new file mode 100644 index 0000000..68519a3 --- /dev/null +++ b/src/status.ts @@ -0,0 +1,129 @@ +import './status.css'; +import { axios } from './utils'; +import { ZcStatus } from './zc-status'; + +declare function waitForKeyElements(selectorOrFunction: any, callback: any, waitOnce: boolean): any; + +export class Status { + private _user: string; + private _enabled = false; + private _statusOn = 'menuAvailable'; + private _statusOff = 'menuAway'; + private _currentStatus: boolean = undefined; + + public async showStatus(element: HTMLElement) { + this._user = await GM.getValue('tapi-zc-user', ''); + this._enabled = await GM.getValue('tapi-zc-enabled', false); + this._statusOn = await GM.getValue('tapi-zc-on', 'menuAvailable'); + this._statusOff = await GM.getValue('tapi-zc-off', 'menuAvailable'); + console.log('tapi-zc-user', this._user, 'tapi-zc-enabled', this._enabled, 'tapi-zc-on', this._statusOn, 'tapi-zc-off', this._statusOff); + + var div = document.createElement('div'); + div.classList.add('tapi-dropdown'); + + var button = document.createElement('button'); + button.id = 'tapi-zc-button'; + button.classList.add('btn'); + button.classList.add('btn-default'); + button.innerText = 'ZeitConsens'; + button.onclick = () => { + document.getElementById('tapi-zc-dropdown').classList.toggle('show'); + } + div.appendChild(button); + + var html = + '
' + + ' ' + + ' ' + + '
' + + '
' + + ' ' + + ' ' + + '
' + + '
' + + ' ' + + ' ' + + '
' + + '
' + + ' ' + '
'; + + var dropdown = document.createElement('div'); + dropdown.classList.add('tapi-dropdown-content'); + dropdown.classList.add('panel-body'); + dropdown.id = 'tapi-zc-dropdown'; + dropdown.innerHTML = html; + div.appendChild(dropdown); + + element.insertBefore(div, element.firstChild); + + var zcUser = document.getElementById('tapi-zc-user') as HTMLInputElement; + zcUser.value = this._user; + zcUser.onchange = () => { + this._user = zcUser.value; + GM.setValue('tapi-zc-user', this._user); + console.log('tapi-zc-user', this._user); + this._currentStatus = undefined; + } + + var zcEnabled = document.getElementById('tapi-zc-enabled') as HTMLInputElement; + zcEnabled.checked = this._enabled; + zcEnabled.onchange = () => { + this._enabled = zcEnabled.checked; + GM.setValue('tapi-zc-enabled', this._enabled); + console.log('tapi-zc-enabled', this._enabled); + this._currentStatus = undefined; + this.checkStatus(); + } + + var zcOn = document.getElementById('tapi-zc-on') as HTMLSelectElement; + zcOn.value = this._statusOn; + zcOn.onchange = () => { + this._statusOn = zcOn.value; + GM.setValue('tapi-zc-on', this._statusOn); + console.log('tapi-zc-on', this._statusOn); + this._currentStatus = undefined; + } + + var zcOff = document.getElementById('tapi-zc-off') as HTMLSelectElement; + zcOff.value = this._statusOff; + zcOff.onchange = () => { + this._statusOff = zcOff.value; + GM.setValue('tapi-zc-off', this._statusOff); + console.log('tapi-zc-off', this._statusOff); + this._currentStatus = undefined; + } + + this.checkStatus(); + } + + private async checkStatus() { + if (this._enabled) { + var response = await axios.get('http://cpatapi.cpsrvweb2016.cp-austria.at/availability/' + encodeURIComponent(this._user)); + + if (response.status == 200) { + var status = response.data; + if (this._currentStatus !== status.loggedIn) { + this._currentStatus = status.loggedIn; + console.log('New status, loggedIn', this._currentStatus); + (document.getElementsByClassName("current-status")[0] as HTMLAnchorElement).click(); + setTimeout(() => { + var statusId = this._currentStatus ? this._statusOn : this._statusOff; + (document.getElementById(statusId) as HTMLAnchorElement).click(); + }, 1000); + } + } + setTimeout(() => this.checkStatus(), 10000); + } + } +} diff --git a/src/zc-status.ts b/src/zc-status.ts new file mode 100644 index 0000000..df5316c --- /dev/null +++ b/src/zc-status.ts @@ -0,0 +1,4 @@ +export class ZcStatus { + public user: string; + public loggedIn: boolean; +}