2018-12-30 23:41:44 +01:00
|
|
|
import Debug from '../../conf/Debug';
|
|
|
|
import BrowserDetect from '../../conf/BrowserDetect';
|
2021-03-06 02:06:46 +01:00
|
|
|
import Logger from '../Logger';
|
|
|
|
import { browser } from 'webextension-polyfill-ts';
|
|
|
|
import Settings from '../Settings';
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2020-04-13 15:20:29 +02:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
2020-12-03 01:16:57 +01:00
|
|
|
console.info("Loading CommsClient");
|
2020-04-13 15:20:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
class CommsClient {
|
2021-03-06 02:06:46 +01:00
|
|
|
commsId: string;
|
|
|
|
|
|
|
|
logger: Logger;
|
|
|
|
settings: any; // sus?
|
|
|
|
|
|
|
|
|
|
|
|
commands: {[x: string]: any[]};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_listener: (m: any) => void;
|
|
|
|
port: any;
|
|
|
|
|
|
|
|
|
2020-03-08 17:13:44 +01:00
|
|
|
constructor(name, logger, commands) {
|
2021-03-06 02:06:46 +01:00
|
|
|
try {
|
2019-09-03 23:01:23 +02:00
|
|
|
this.logger = logger;
|
2020-01-30 01:07:00 +01:00
|
|
|
|
2021-03-06 02:06:46 +01:00
|
|
|
this.port = browser.runtime.connect(null, {name: name});
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2020-01-30 01:07:00 +01:00
|
|
|
this.logger.onLogEnd(
|
2020-01-30 23:17:43 +01:00
|
|
|
(history) => {
|
|
|
|
this.logger.log('info', 'comms', 'Sending logging-stop-and-save to background script ...');
|
|
|
|
try {
|
2020-06-04 22:47:04 +02:00
|
|
|
this.port.postMessage({cmd: 'logging-stop-and-save', host: window.location.hostname, history})
|
2020-01-30 23:17:43 +01:00
|
|
|
} catch (e) {
|
|
|
|
this.logger.log('error', 'comms', 'Failed to send message to background script. Error:', e);
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 01:07:00 +01:00
|
|
|
);
|
|
|
|
|
2020-03-08 17:13:44 +01:00
|
|
|
this._listener = m => this.processReceivedMessage(m);
|
2018-12-30 23:41:44 +01:00
|
|
|
this.port.onMessage.addListener(this._listener);
|
|
|
|
|
|
|
|
this.commsId = (Math.random() * 20).toFixed(0);
|
2020-01-21 00:40:56 +01:00
|
|
|
|
2020-03-08 17:13:44 +01:00
|
|
|
this.commands = commands;
|
2021-03-06 02:06:46 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error("CONSTRUCOTR FAILED:", e)
|
|
|
|
}
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if (!BrowserDetect.edge) { // edge is a very special browser made by outright morons.
|
|
|
|
this.port.onMessage.removeListener(this._listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 00:40:56 +01:00
|
|
|
subscribe(command, callback) {
|
|
|
|
if (!this.commands[command]) {
|
|
|
|
this.commands[command] = [callback];
|
|
|
|
} else {
|
|
|
|
this.commands[command].push(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
processReceivedMessage(message){
|
2019-07-18 21:25:58 +02:00
|
|
|
this.logger.log('info', 'comms', `[CommsClient.js::processMessage] <${this.commsId}> Received message from background script!`, message);
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2020-01-21 00:40:56 +01:00
|
|
|
if (this.commands[message.cmd]) {
|
|
|
|
for (const c of this.commands[message.cmd]) {
|
|
|
|
c(message);
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendMessage_nonpersistent(message){
|
2019-01-18 00:26:15 +01:00
|
|
|
message = JSON.parse(JSON.stringify(message)); // vue quirk. We should really use vue store instead
|
2021-03-06 02:06:46 +01:00
|
|
|
return browser.runtime.sendMessage(null, message, null);
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 02:06:46 +01:00
|
|
|
|
|
|
|
// TODO: sus function — does it get any use?
|
2018-12-30 23:41:44 +01:00
|
|
|
async requestSettings(){
|
2019-07-18 21:25:58 +02:00
|
|
|
this.logger.log('info', 'comms', "%c[CommsClient::requestSettings] sending request for congif!", "background: #11D; color: #aad");
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
var response = await this.sendMessage_nonpersistent({cmd: 'get-config'});
|
2019-07-18 21:25:58 +02:00
|
|
|
|
|
|
|
this.logger.log('info', 'comms', "%c[CommsClient::requestSettings] received settings response!", "background: #11D; color: #aad", response);
|
2018-12-30 23:41:44 +01:00
|
|
|
|
|
|
|
if(! response || response.extensionConf){
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
|
|
|
|
2021-03-06 02:06:46 +01:00
|
|
|
this.settings = {active: JSON.parse(response.extensionConf)};
|
2018-12-30 23:41:44 +01:00
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
|
2019-06-14 02:15:24 +02:00
|
|
|
async sendMessage(message) {
|
2021-03-06 02:06:46 +01:00
|
|
|
return this.sendMessage_nonpersistent(message);
|
2019-06-14 02:15:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
registerVideo(){
|
2019-07-18 21:25:58 +02:00
|
|
|
this.logger.log('info', 'comms', `[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page.");
|
2020-03-08 17:13:44 +01:00
|
|
|
this.port.postMessage({cmd: "has-video"});
|
2019-02-16 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sendPerformanceUpdate(message){
|
|
|
|
this.port.postMessage({cmd: 'performance-update', message: message});
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unregisterVideo(){
|
2019-07-18 21:25:58 +02:00
|
|
|
this.logger.log('info', 'comms', `[CommsClient::unregisterVideo] <${this.commsId}>`, "Unregistering video for current page.");
|
2018-12-30 23:41:44 +01:00
|
|
|
this.port.postMessage({cmd: "noVideo"}); // ayymd
|
|
|
|
}
|
|
|
|
|
|
|
|
announceZoom(scale){
|
|
|
|
this.port.postMessage({cmd: "announce-zoom", zoom: scale});
|
2020-03-09 21:29:00 +01:00
|
|
|
this.registerVideo();
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 01:07:00 +01:00
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:20:29 +02:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
2020-12-03 01:16:57 +01:00
|
|
|
console.info("CommsClient loaded");
|
2020-04-13 15:20:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
export default CommsClient;
|