2018-12-30 23:41:44 +01:00
import Debug from '../../conf/Debug' ;
2019-01-02 20:36:00 +01:00
import BrowserDetect from '../../conf/BrowserDetect' ;
2018-05-26 23:08:49 +02:00
class CommsServer {
constructor ( server ) {
this . server = server ;
2018-08-07 23:31:28 +02:00
this . settings = server . settings ;
2018-05-26 23:08:49 +02:00
this . ports = [ ] ;
var ths = this ;
2019-01-20 19:58:26 +01:00
// console.log("[CommsServer::ctor] INIT! are we in ff?", BrowserDetect.firefox, "BrowserDetect says ...", BrowserDetect)
2019-01-02 20:36:00 +01:00
2018-12-30 23:41:44 +01:00
if ( BrowserDetect . firefox ) {
2018-05-26 23:08:49 +02:00
browser . runtime . onConnect . addListener ( p => ths . onConnect ( p ) ) ;
browser . runtime . onMessage . addListener ( m => ths . processReceivedMessage _nonpersistent _ff ( m ) ) ;
} else {
chrome . runtime . onConnect . addListener ( p => ths . onConnect ( p ) ) ;
2019-01-02 20:36:00 +01:00
chrome . runtime . onMessage . addListener ( ( m , sender , callback ) => ths . processReceivedMessage _nonpersistent _chrome ( m , sender , callback ) ) ;
2018-05-26 23:08:49 +02:00
}
}
2019-01-18 00:26:15 +01:00
async toObject ( obj ) {
2019-01-20 19:58:26 +01:00
// console.log("(not actually) CLONING OBJECT", obj);
// try {
// const r = JSON.parse(JSON.stringify(obj));
// return r;
// } catch (e) {
// console.log("ERROR WHILE CLONING", obj);
2019-01-18 00:26:15 +01:00
return obj ;
2019-01-20 19:58:26 +01:00
// }
2019-01-18 00:26:15 +01:00
}
2018-09-20 01:11:18 +02:00
async getCurrentTabHostname ( ) {
const activeTab = await this . _getActiveTab ( ) ;
const url = activeTab [ 0 ] . url ;
var hostname ;
if ( url . indexOf ( "://" ) > - 1 ) { //find & remove protocol (http, ftp, etc.) and get hostname
hostname = url . split ( '/' ) [ 2 ] ;
}
else {
hostname = url . split ( '/' ) [ 0 ] ;
}
hostname = hostname . split ( ':' ) [ 0 ] ; //find & remove port number
hostname = hostname . split ( '?' ) [ 0 ] ; //find & remove "?"
2018-06-27 23:55:37 +02:00
2018-09-20 01:11:18 +02:00
return hostname ;
2018-06-27 23:55:37 +02:00
}
2018-05-26 23:56:50 +02:00
sendToAll ( 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-06-28 23:43:52 +02:00
for ( var p of this . ports ) {
for ( var frame in p ) {
2018-05-26 23:56:50 +02:00
p [ frame ] . postMessage ( message ) ;
}
}
}
2018-07-16 22:30:52 +02:00
async _getActiveTab ( ) {
2018-12-30 23:41:44 +01:00
if ( BrowserDetect . firefox ) {
2018-07-16 22:30:52 +02:00
return await browser . tabs . query ( { currentWindow : true , active : true } ) ;
} else {
return await new Promise ( ( resolve , reject ) => {
2018-09-20 21:45:09 +02:00
chrome . tabs . query ( { lastFocusedWindow : true , active : true } , function ( res ) {
2018-07-16 22:30:52 +02:00
resolve ( res ) ;
} ) ;
} ) ;
2018-05-27 01:29:02 +02:00
}
2018-07-16 22:30:52 +02:00
}
2018-05-27 01:29:02 +02:00
2018-11-21 20:41:15 +01:00
async sendToFrame ( message , tab , frame ) {
2019-01-18 00:26:15 +01:00
message = JSON . parse ( JSON . stringify ( message ) ) ; // vue quirk. We should really use vue store instead
2018-11-21 20:41:15 +01:00
if ( Debug . debug && Debug . comms ) {
console . log ( ` %c[CommsServer::sendToFrame] attempting to send message to tab ${ tab } , frame ${ frame } ` , "background: #dda; color: #11D" , message ) ;
}
if ( isNaN ( tab ) ) {
if ( tab === '__playing' ) {
message [ 'playing' ] = true ;
this . sendToAll ( message ) ;
return ;
} else if ( tab === '__all' ) {
this . sendToAll ( message ) ;
return ;
}
[ tab , frame ] = tab . split ( '-' )
}
if ( Debug . debug && Debug . comms ) {
console . log ( ` %c[CommsServer::sendToFrame] attempting to send message to tab ${ tab } , frame ${ frame } ` , "background: #dda; color: #11D" , message ) ;
}
try {
this . ports [ tab ] [ frame ] . postMessage ( message ) ;
} catch ( e ) {
if ( Debug . debug && Debug . comms ) {
console . log ( ` %c[CommsServer::sendToFrame] Sending message failed. Reason: ` , "background: #dda; color: #11D" , e ) ;
}
}
}
2018-07-16 22:30:52 +02:00
async sendToActive ( 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-07-16 22:30:52 +02:00
if ( Debug . debug && Debug . comms ) {
console . log ( "%c[CommsServer::sendToActive] trying to send a message to active tab. Message:" , "background: #dda; color: #11D" , message ) ;
2018-05-26 23:56:50 +02:00
}
2018-07-16 22:30:52 +02:00
var tabs = await this . _getActiveTab ( ) ;
2018-05-27 01:29:02 +02:00
if ( Debug . debug && Debug . comms ) {
console . log ( "[CommsServer::_sendToActive_ff] currently active tab(s)?" , tabs ) ;
for ( var key in this . ports [ tabs [ 0 ] . id ] ) {
console . log ( "key?" , key , this . ports [ tabs [ 0 ] . id ] ) ;
// this.ports[tabs[0].id][key].postMessage(message);
}
}
for ( var key in this . ports [ tabs [ 0 ] . id ] ) {
2018-05-26 23:56:50 +02:00
this . ports [ tabs [ 0 ] . id ] [ key ] . postMessage ( message ) ;
}
}
2018-05-26 23:08:49 +02:00
onConnect ( port ) {
2018-05-27 01:29:02 +02:00
var ths = this ;
// poseben primer | special case
if ( port . name === 'popup-port' ) {
this . popupPort = port ;
this . popupPort . onMessage . addListener ( ( m , p ) => ths . processReceivedMessage ( m , p ) ) ;
return ;
}
2018-05-26 23:08:49 +02:00
var tabId = port . sender . tab . id ;
2018-05-26 23:56:50 +02:00
var frameId = port . sender . frameId ;
if ( ! this . ports [ tabId ] ) {
this . ports [ tabId ] = { } ;
}
this . ports [ tabId ] [ frameId ] = port ;
this . ports [ tabId ] [ frameId ] . onMessage . addListener ( ( m , p ) => ths . processReceivedMessage ( m , p ) ) ;
this . ports [ tabId ] [ frameId ] . onDisconnect . addListener ( ( p ) => {
delete ths . ports [ p . sender . tab . id ] [ p . sender . frameId ] ;
if ( Object . keys ( ths . ports [ p . sender . tab . id ] ) . length === 0 ) {
ths . ports [ tabId ] = undefined ;
}
} ) ;
2018-05-26 23:08:49 +02:00
}
2018-09-20 01:11:18 +02:00
async processReceivedMessage ( message , port ) {
2018-05-26 23:08:49 +02:00
if ( Debug . debug && Debug . comms ) {
2019-01-02 20:36:00 +01:00
console . log ( "[CommsServer.js::processReceivedMessage] Received message from popup/content script!" , message , "port" , port , "\nsettings and server:" , this . settings , this . server ) ;
}
if ( message . forwardToContentScript ) {
if ( Debug . debug && Debug . comms ) {
console . log ( "[CommsServer.js::processReceivedMessage] Message has 'forward to content script' flag set. Forwarding message as is. Message:" , message ) ;
}
this . sendToFrame ( message , message . targetFrame ) ;
2018-05-26 23:08:49 +02:00
}
2018-09-21 00:26:08 +02:00
if ( message . cmd === 'announce-zoom' ) {
// forward off to the popup, no use for this here
2018-09-23 19:46:40 +02:00
try {
2019-01-20 19:58:26 +01:00
this . popupPort . postMessage ( { cmd : 'set-current-zoom' , zoom : message . zoom } ) ;
2018-09-23 19:46:40 +02:00
} catch ( e ) {
// can't forward stuff to popup if it isn't open
}
2018-09-21 00:26:08 +02:00
} else if ( message . cmd === 'get-current-zoom' ) {
this . sendToActive ( message ) ;
}
if ( message . cmd === 'get-current-site' ) {
2019-01-18 00:26:15 +01:00
console . log ( "CCCCC - ss" ) ;
console . log ( "[find server] set-current-site — getting site" , this . server . getVideoTab ( ) , this . toObject ( this . server . getVideoTab ( ) ) )
2019-01-20 19:58:26 +01:00
port . postMessage ( {
2019-01-18 00:26:15 +01:00
cmd : 'set-current-site' ,
site : this . server . getVideoTab ( ) ,
tabHostname : await this . getCurrentTabHostname ( )
2019-01-20 19:58:26 +01:00
} ) ;
2019-01-18 00:26:15 +01:00
console . log ( "CCCCC -s as" )
2018-09-20 01:11:18 +02:00
}
2018-11-23 00:29:13 +01:00
if ( message . cmd === 'popup-set-selected-tab' ) {
2019-01-18 00:26:15 +01:00
console . log ( "CCCCaa" )
2018-11-23 00:29:13 +01:00
this . server . setSelectedTab ( message . selectedMenu , message . selectedSubitem ) ;
2019-01-18 00:26:15 +01:00
console . log ( "CCCCbb" )
2018-11-22 22:55:22 +01:00
}
2018-09-20 01:11:18 +02:00
2018-05-26 23:08:49 +02:00
if ( message . cmd === 'get-config' ) {
2018-08-21 23:48:47 +02:00
if ( Debug . debug ) {
console . log ( "CommsServer: received get-config. Active settings?" , this . settings . active , "\n(settings:" , this . settings , ")" )
}
2019-01-20 19:58:26 +01:00
port . postMessage (
2019-01-18 00:26:15 +01:00
{ cmd : "set-config" , conf : this . settings . active , site : this . server . currentSite }
2019-01-20 19:58:26 +01:00
) ;
2018-11-07 00:03:06 +01:00
} else if ( message . cmd === 'has-video' ) {
this . server . registerVideo ( port . sender ) ;
} else if ( message . cmd === 'noVideo' ) {
this . server . unregisterVideo ( port . sender ) ;
2019-01-02 20:36:00 +01:00
}
2018-05-26 23:08:49 +02:00
}
processReceivedMessage _nonpersistent _ff ( message , sender ) {
if ( Debug . debug && Debug . comms ) {
2018-05-27 01:29:02 +02:00
console . log ( "%c[CommsServer.js::processMessage_nonpersistent_ff] Received message from background script!" , "background-color: #11D; color: #aad" , message , sender ) ;
2018-05-26 23:08:49 +02:00
}
2019-01-02 20:36:00 +01:00
if ( message . forwardToContentScript ) {
if ( Debug . debug && Debug . comms ) {
console . log ( "[CommsServer.js::processMessage_nonpersistent_ff] Message has 'forward to content script' flag set. Forwarding message as is. Message:" , message ) ;
console . log ( "[CommsServer.js::processMessage_nonpersistent_ff] (btw we probably shouldn't be seeing this. This should prolly happen in persistent connection?" ) ;
}
this . sendToFrame ( message , message . targetFrame ) ;
}
2018-05-26 23:08:49 +02:00
if ( message . cmd === 'get-config' ) {
2018-08-05 23:48:56 +02:00
var ret = { extensionConf : JSON . stringify ( this . settings . active ) } ;
2018-05-26 23:08:49 +02:00
if ( Debug . debug && Debug . comms ) {
2018-05-27 01:29:02 +02:00
console . log ( "%c[CommsServer.js::processMessage_nonpersistent_ff] Returning this:" , "background-color: #11D; color: #aad" , ret ) ;
2018-05-26 23:08:49 +02:00
}
Promise . resolve ( ret ) ;
2018-06-15 00:33:10 +02:00
} else if ( message . cmd === "autoar-enable" ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . mode = "blacklist" ;
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-06-15 00:33:10 +02:00
this . sendToAll ( { cmd : "reload-settings" , sender : "uwbg" } )
if ( Debug . debug ) {
2018-08-05 23:48:56 +02:00
console . log ( "[uw-bg] autoar set to enabled (blacklist). evidenz:" , this . settings . active ) ;
2018-06-15 00:33:10 +02:00
}
} else if ( message . cmd === "autoar-disable" ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . mode = "disabled" ;
2018-06-15 00:33:10 +02:00
if ( message . reason ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . disabledReason = message . reason ;
2018-06-15 00:33:10 +02:00
} else {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . disabledReason = 'User disabled' ;
2018-06-15 00:33:10 +02:00
}
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-08-05 23:48:56 +02:00
this . sendToAll ( { cmd : 'reload-settings' , newConf : this . settings . active } ) ;
2018-06-15 00:33:10 +02:00
if ( Debug . debug ) {
2018-08-05 23:48:56 +02:00
console . log ( "[uw-bg] autoar set to disabled. evidenz:" , this . settings . active ) ;
2018-06-15 00:33:10 +02:00
}
} else if ( message . cmd === "autoar-set-interval" ) {
if ( Debug . debug )
console . log ( "[uw-bg] trying to set new interval for autoAr. New interval is" , message . timeout , "ms" ) ;
// set fairly liberal limit
var timeout = message . timeout < 4 ? 4 : message . timeout ;
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . timer _playing = timeout ;
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-08-05 23:48:56 +02:00
this . sendToAll ( { cmd : 'reload-settings' , newConf : this . settings . active } ) ;
2018-05-26 23:56:50 +02:00
}
2018-05-26 23:08:49 +02:00
}
processReceivedMessage _nonpersistent _chrome ( message , sender , sendResponse ) {
if ( Debug . debug && Debug . comms ) {
console . log ( "[CommsServer.js::processMessage_nonpersistent_chrome] Received message from background script!" , message ) ;
2019-01-02 20:36:00 +01:00
if ( BrowserDetect . firefox ) {
throw "WHAT THE FUCK WHY IS THIS RUNNING, THIS ISNT SUPPOSED TO BE RUNNING RN"
}
2018-05-26 23:08:49 +02:00
}
if ( message . cmd === 'get-config' ) {
2018-09-20 21:45:09 +02:00
sendResponse ( { extensionConf : JSON . stringify ( this . settings . active ) , site : this . getCurrentTabHostname ( ) } ) ;
2018-05-26 23:08:49 +02:00
// return true;
2018-06-15 00:33:10 +02:00
} else if ( message . cmd === "autoar-enable" ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . mode = "blacklist" ;
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-06-15 00:33:10 +02:00
this . sendToAll ( { cmd : "reload-settings" , sender : "uwbg" } )
if ( Debug . debug ) {
2018-08-05 23:48:56 +02:00
console . log ( "[uw-bg] autoar set to enabled (blacklist). evidenz:" , this . settings . active ) ;
2018-06-15 00:33:10 +02:00
}
} else if ( message . cmd === "autoar-disable" ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . mode = "disabled" ;
2018-06-15 00:33:10 +02:00
if ( message . reason ) {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . disabledReason = message . reason ;
2018-06-15 00:33:10 +02:00
} else {
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . disabledReason = 'User disabled' ;
2018-06-15 00:33:10 +02:00
}
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-08-05 23:48:56 +02:00
this . sendToAll ( { cmd : 'reload-settings' , newConf : this . settings . active } ) ;
2018-06-15 00:33:10 +02:00
if ( Debug . debug ) {
2018-08-05 23:48:56 +02:00
console . log ( "[uw-bg] autoar set to disabled. evidenz:" , this . settings . active ) ;
2018-06-15 00:33:10 +02:00
}
} else if ( message . cmd === "autoar-set-interval" ) {
if ( Debug . debug )
console . log ( "[uw-bg] trying to set new interval for autoAr. New interval is" , message . timeout , "ms" ) ;
// set fairly liberal limit
var timeout = message . timeout < 4 ? 4 : message . timeout ;
2018-08-05 23:48:56 +02:00
this . settings . active . arDetect . timer _playing = timeout ;
2018-08-07 23:31:28 +02:00
this . settings . save ( ) ;
2018-08-05 23:48:56 +02:00
this . sendToAll ( { cmd : 'reload-settings' , newConf : this . settings . active } ) ;
2018-05-26 23:08:49 +02:00
}
}
}
2018-12-30 23:41:44 +01:00
export default CommsServer ;