2018-12-30 23:41:44 +01:00
import Debug from '../../conf/Debug' ;
import BrowserDetect from '../../conf/BrowserDetect' ;
class CommsClient {
2019-07-18 21:25:58 +02:00
constructor ( name , settings , logger ) {
2019-09-03 23:01:23 +02:00
this . logger = logger ;
2020-01-30 01:07:00 +01:00
2018-12-30 23:41:44 +01:00
if ( BrowserDetect . firefox ) {
this . port = browser . runtime . connect ( { name : name } ) ;
} else if ( BrowserDetect . chrome ) {
this . port = chrome . runtime . connect ( { name : name } ) ;
} else if ( BrowserDetect . edge ) {
this . port = browser . runtime . connect ( { name : name } )
}
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 {
this . port . postMessage ( { cmd : 'logging-stop-and-save' , host : window . location . host , history } )
} catch ( e ) {
this . logger . log ( 'error' , 'comms' , 'Failed to send message to background script. Error:' , e ) ;
}
}
2020-01-30 01:07:00 +01:00
) ;
2018-12-30 23:41:44 +01:00
var ths = this ;
this . _listener = m => ths . processReceivedMessage ( m ) ;
this . port . onMessage . addListener ( this . _listener ) ;
this . settings = settings ;
this . pageInfo = undefined ;
this . commsId = ( Math . random ( ) * 20 ) . toFixed ( 0 ) ;
2020-01-21 00:40:56 +01:00
this . commands = {
'get-current-zoom' : [ ( ) => this . pageInfo . requestCurrentZoom ( ) ] ,
'set-ar' : [ ( message ) => this . pageInfo . setAr ( { type : message . arg , ratio : message . customArg } , message . playing ) ] ,
'set-alignment' : [ ( message ) => {
this . pageInfo . setVideoAlignment ( message . arg , message . playing ) ;
this . pageInfo . restoreAr ( ) ;
} ] ,
'set-stretch' : [ ( message ) => this . pageInfo . setStretchMode ( message . arg , message . playing , message . customArg ) ] ,
'set-keyboard' : [ ( message ) => this . pageInfo . setKeyboardShortcutsEnabled ( message . arg ) ] ,
'autoar-start' : [ ( message ) => {
if ( message . enabled !== false ) {
this . pageInfo . initArDetection ( message . playing ) ;
this . pageInfo . startArDetection ( message . playing ) ;
} else {
this . pageInfo . stopArDetection ( message . playing ) ;
}
} ] ,
'pause-processing' : [ ( message ) => this . pageInfo . pauseProcessing ( message . playing ) ] ,
'resume-processing' : [ ( message ) => this . pageInfo . resumeProcessing ( message . autoArStatus , message . playing ) ] ,
'set-zoom' : [ ( message ) => this . pageInfo . setZoom ( message . arg , true , message . playing ) ] ,
'change-zoom' : [ ( message ) => this . pageInfo . zoomStep ( message . arg , message . playing ) ] ,
'mark-player' : [ ( message ) => this . pageInfo . markPlayer ( message . name , message . color ) ] ,
'unmark-player' : [ ( ) => this . pageInfo . unmarkPlayer ( ) ] ,
'autoar-set-manual-tick' : [ ( message ) => this . pageInfo . setManualTick ( message . arg ) ] ,
'autoar-tick' : [ ( ) => this . pageInfo . tick ( ) ] ,
'set-ar-persistence' : [ ( ) => this . pageInfo . setArPersistence ( message . arg ) ] ,
} ;
2018-12-30 23:41:44 +01:00
}
destroy ( ) {
this . pageInfo = null ;
this . settings = null ;
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
setPageInfo ( pageInfo ) {
this . pageInfo = pageInfo ;
2020-02-03 22:13:03 +01:00
this . logger . log ( 'info' , 'debug' , ` [CommsClient::setPageInfo] < ${ this . commsId } > ` , "setting pageinfo" ) ;
2018-12-30 23:41:44 +01:00
var ths = this ;
this . _listener = m => ths . processReceivedMessage ( m ) ;
if ( ! BrowserDetect . edge ) {
this . port . onMessage . removeListener ( this . _listener ) ;
}
this . port . onMessage . addListener ( this . _listener ) ;
}
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
if ( ! this . pageInfo || ! this . settings . active ) {
2019-07-18 21:25:58 +02:00
this . logger . log ( 'info' , 'comms' , ` [CommsClient.js::processMessage] < ${ this . commsId } > this.pageInfo (or settings) not defined. Extension is probably disabled for this site. \n pageInfo: ` , this . pageInfo ,
"\nsettings.active:" , this . settings . active ,
"\nnobj:" , this
) ;
2018-12-30 23:41:44 +01:00
return ;
}
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 sleep ( n ) {
return new Promise ( ( resolve , reject ) => setTimeout ( resolve , n ) ) ;
}
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
2018-12-30 23:41:44 +01:00
if ( BrowserDetect . firefox ) {
return browser . runtime . sendMessage ( message )
} else {
return new Promise ( ( resolve , reject ) => {
try {
if ( BrowserDetect . edge ) {
browser . runtime . sendMessage ( message , function ( response ) {
var r = response ;
resolve ( r ) ;
} ) ;
} else {
chrome . runtime . sendMessage ( message , function ( response ) {
// Chrome/js shittiness mitigation — remove this line and an empty array will be returned
var r = response ;
resolve ( r ) ;
2019-05-09 21:07:40 +02:00
return true ;
2018-12-30 23:41:44 +01:00
} ) ;
}
}
catch ( e ) {
reject ( e ) ;
}
2019-05-09 21:07:40 +02:00
return true ;
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 ) ;
}
this . settings . active = JSON . parse ( response . extensionConf ) ;
return Promise . resolve ( true ) ;
}
2019-06-14 02:15:24 +02:00
async sendMessage ( message ) {
await this . sendMessage _nonpersistent ( message ) ;
}
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." ) ;
2018-12-31 03:34:26 +01:00
if ( this . pageInfo ) {
2019-07-03 21:55:08 +02:00
if ( this . pageInfo . hasVideo ( ) ) {
2018-12-31 03:34:26 +01:00
this . port . postMessage ( { cmd : "has-video" } ) ;
}
} else {
// this.port.postMessage({cmd: "has-video"});
2018-12-30 23:41:44 +01:00
}
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 } ) ;
this . registerVideo ( )
}
2020-01-30 01:07:00 +01:00
2018-12-30 23:41:44 +01:00
}
export default CommsClient ;