2018-01-26 00:09:08 +01:00
|
|
|
|
var BgVars = {
|
|
|
|
|
arIsActive: true,
|
2018-02-02 00:21:29 +01:00
|
|
|
|
hasVideos: false,
|
|
|
|
|
currentSite: ""
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-26 23:08:49 +02:00
|
|
|
|
class UWServer {
|
|
|
|
|
constructor() {
|
|
|
|
|
this.ports = [];
|
|
|
|
|
|
|
|
|
|
this.setup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setup() {
|
|
|
|
|
await Settings.init();
|
|
|
|
|
this.comms = new CommsServer(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var server = new UWServer();
|
|
|
|
|
|
2018-02-02 00:21:29 +01:00
|
|
|
|
function extractHostname(url){
|
|
|
|
|
// extract hostname
|
|
|
|
|
if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname
|
|
|
|
|
hostname = url.split('/')[2];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
hostname = url.split('/')[0];
|
|
|
|
|
}
|
2018-01-26 00:09:08 +01:00
|
|
|
|
|
2018-02-02 00:21:29 +01:00
|
|
|
|
hostname = hostname.split(':')[0]; //find & remove port number
|
|
|
|
|
hostname = hostname.split('?')[0]; //find & remove "?"
|
|
|
|
|
|
|
|
|
|
return hostname;
|
2018-01-26 00:09:08 +01:00
|
|
|
|
}
|
2018-01-24 23:15:54 +01:00
|
|
|
|
|
2017-12-29 23:34:40 +01:00
|
|
|
|
async function main(){
|
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg::main] setting up background script");
|
|
|
|
|
|
2018-05-26 23:08:49 +02:00
|
|
|
|
|
2018-02-01 00:26:16 +01:00
|
|
|
|
Keybinds.keybinds = await Keybinds.fetch();
|
|
|
|
|
|
2017-12-29 23:34:40 +01:00
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
// Poslušalci za dogodke | event listeners here
|
|
|
|
|
// {===]///[-------------------------------------]\\\[===}
|
2018-01-08 22:48:45 +01:00
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
browser.runtime.onMessage.addListener(_uwbg_rcvmsg);
|
2018-01-24 23:15:54 +01:00
|
|
|
|
browser.tabs.onActivated.addListener(_uwbg_onTabSwitched);
|
|
|
|
|
|
2017-12-29 23:34:40 +01:00
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg::main] listeners registered");
|
2018-02-02 00:21:29 +01:00
|
|
|
|
|
|
|
|
|
// add currentSite
|
|
|
|
|
var tabs = await Comms.getActiveTab();
|
|
|
|
|
BgVars.currentSite = extractHostname(tabs[0].url);
|
|
|
|
|
|
2018-02-06 00:36:48 +01:00
|
|
|
|
//
|
|
|
|
|
setInterval(_uwbg_check4videos, 5000);
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 23:15:54 +01:00
|
|
|
|
async function _uwbg_onTabSwitched(activeInfo){
|
2018-01-27 20:13:23 +01:00
|
|
|
|
BgVars.hasVideos = false;
|
2017-12-29 23:34:40 +01:00
|
|
|
|
if(Debug.debug)
|
2018-01-24 23:15:54 +01:00
|
|
|
|
console.log("[uw-bg::onTabSwitched] TAB CHANGED, GETTING INFO FROM MAIN TAB");
|
|
|
|
|
|
2018-02-02 00:21:29 +01:00
|
|
|
|
|
2018-01-24 23:15:54 +01:00
|
|
|
|
var tabId = activeInfo.tabId; // just for readability
|
|
|
|
|
|
2018-02-02 00:21:29 +01:00
|
|
|
|
var tab = await browser.tabs.get(tabId);
|
|
|
|
|
|
|
|
|
|
BgVars.currentSite = extractHostname(tab.url);
|
|
|
|
|
|
|
|
|
|
// this can fail. This might also not return a promise? Check that.
|
2018-01-26 00:09:08 +01:00
|
|
|
|
var videoFrameList = await Comms.sendToEach({"cmd":"has-videos"}, tabId);
|
|
|
|
|
|
2018-01-27 20:13:23 +01:00
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg::onTabSwitched] got list of frames and whether they have videos", videoFrameList);
|
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
// Pogledamo, če kateri od okvirjev vsebuje video. Da omogočimo pojavno okno je zadosti že
|
|
|
|
|
// en okvir z videom.
|
|
|
|
|
// <===[///]----------------------------[\\\]===>
|
|
|
|
|
// Check if any frame has a video in it. To enable the popup there only needs to be at least one,
|
|
|
|
|
// but the popup controls all frames.
|
2018-05-05 23:06:39 +02:00
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
var hasVideos = false;
|
|
|
|
|
for(frame of videoFrameList){
|
|
|
|
|
hasVideos |= frame.response.hasVideos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BgVars.hasVideos = hasVideos;
|
|
|
|
|
|
|
|
|
|
Settings.reload();
|
|
|
|
|
// todo: change extension icon depending on whether there's a video on the page or not
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-04 17:39:26 +01:00
|
|
|
|
async function _uwbg_check4videos(){
|
|
|
|
|
if(BgVars.hasVideos)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var videoFrameList = Comms.sendToEach({"cmd":"has-videos"});
|
|
|
|
|
|
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg::check4videos] got updated list of frames and whether they have videos", videoFrameList);
|
|
|
|
|
|
|
|
|
|
var hasVideos = false;
|
|
|
|
|
for(frame of videoFrameList){
|
|
|
|
|
hasVideos |= frame.response.hasVideos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BgVars.hasVideos = hasVideos;
|
|
|
|
|
}
|
2018-01-26 00:09:08 +01:00
|
|
|
|
|
|
|
|
|
async function _uwbg_registerVideo(tabId){
|
|
|
|
|
var tabs = await Comms.getActiveTab();
|
|
|
|
|
|
|
|
|
|
// če ukaz pride iz zavihka, na katerem se trenunto ne nahajamo, potem se za zahtevo ne brigamo
|
|
|
|
|
// if command originated from a tab that's _not_ currently active, we ignore the request
|
|
|
|
|
if(tabId != tabs[0].id){
|
|
|
|
|
if(Debug.debug){
|
|
|
|
|
console.log("[uw-bg::_uwbg_registerVideo] request didn't come from currently active tab, ignoring");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-06 00:36:48 +01:00
|
|
|
|
if(Debug.debug){
|
|
|
|
|
console.log("%c[uw-bg::_uwbg_registerVideo] request came from currently active tab!", "color: #afd, background: #000");
|
|
|
|
|
}
|
2018-01-26 00:09:08 +01:00
|
|
|
|
BgVars.hasVideos = true;
|
|
|
|
|
|
|
|
|
|
// todo: change extension icon depending on whether there's a video on the page or not
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
function _uwbg_rcvmsg(message, sender, sendResponse){
|
2017-12-29 23:34:40 +01:00
|
|
|
|
if(Debug.debug){
|
2018-01-26 00:09:08 +01:00
|
|
|
|
console.log("[uw-bg::_uwbg_rcvmsg] received message", message, "from sender", sender);
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
2017-12-30 02:58:24 +01:00
|
|
|
|
|
2017-12-29 23:34:40 +01:00
|
|
|
|
message.sender = "uwbg";
|
2017-12-30 02:58:24 +01:00
|
|
|
|
message.receiver = "uw";
|
2017-12-29 23:34:40 +01:00
|
|
|
|
|
2018-01-10 23:16:07 +01:00
|
|
|
|
if(message.cmd == "has-videos"){
|
|
|
|
|
if(Debug.debug){
|
2018-01-26 00:09:08 +01:00
|
|
|
|
console.log("[uw-bg::_uwbg_rcvmsg] does this tab or any of its subframes have videos?", BgVars.hasVideos );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var res = {response: {hasVideos: BgVars.hasVideos}};
|
|
|
|
|
if(BrowserDetect.firefox){
|
|
|
|
|
return Promise.resolve(res);
|
2018-01-08 22:48:45 +01:00
|
|
|
|
}
|
2018-01-26 00:09:08 +01:00
|
|
|
|
sendResponse(res);
|
|
|
|
|
return true;
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
2018-01-10 23:16:07 +01:00
|
|
|
|
|
2017-12-30 18:36:08 +01:00
|
|
|
|
if(message.cmd == "get-config"){
|
|
|
|
|
var config = {};
|
2018-03-13 23:55:38 +01:00
|
|
|
|
config.videoAlignment = ExtensionConf.miscFullscreenSettings.videoFloat;
|
2017-12-30 18:36:08 +01:00
|
|
|
|
config.arConf = {};
|
2018-03-13 23:55:38 +01:00
|
|
|
|
config.arConf.enabled_global = ExtensionConf.arDetect.enabled == "blacklist";
|
2018-03-11 18:00:47 +01:00
|
|
|
|
|
|
|
|
|
config.site = {};
|
|
|
|
|
config.site.status = SitesConf.getSiteStatus(BgVars.currentSite);
|
|
|
|
|
config.site.arStatus = SitesConf.getArStatus(BgVars.currentSite);
|
|
|
|
|
|
2018-03-13 23:55:38 +01:00
|
|
|
|
config.mode = ExtensionConf.extensionMode;
|
|
|
|
|
config.arMode = ExtensionConf.arDetect.mode;
|
2018-05-05 23:06:39 +02:00
|
|
|
|
config.arDisabledReason = ExtensionConf.arDetect.disabledReason;
|
2018-03-13 23:55:38 +01:00
|
|
|
|
config.arTimerPlaying = ExtensionConf.arDetect.timer_playing;
|
2017-12-30 18:36:08 +01:00
|
|
|
|
|
2018-01-02 03:36:29 +01:00
|
|
|
|
if(Debug.debug)
|
2018-02-01 00:26:16 +01:00
|
|
|
|
console.log("[uw-bg::_uwbg_rcvmsg] Keybinds.getKeybinds() returned this:", Keybinds.getKeybinds());
|
2018-01-02 03:36:29 +01:00
|
|
|
|
|
2018-02-01 00:26:16 +01:00
|
|
|
|
config.keyboardShortcuts = Keybinds.getKeybinds();
|
2017-12-30 18:36:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// predvidevajmo, da je enako. Če je drugače, bomo popravili ko dobimo odgovor
|
|
|
|
|
// assume current is same as global & change that when you get response from content script
|
2018-03-13 23:55:38 +01:00
|
|
|
|
config.arConf.enabled_current = ExtensionConf.arDetect.enabled == "blacklist";
|
2018-01-08 22:48:45 +01:00
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
var res = {response: config}
|
2018-03-14 21:19:15 +01:00
|
|
|
|
|
|
|
|
|
if(Debug.debug){
|
|
|
|
|
console.log("[uw-bg::_uwbg_rcvmsg] get-config: returning this to popup script:", res);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
if(BrowserDetect.firefox){
|
|
|
|
|
return Promise.resolve(res);
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
2018-03-14 21:19:15 +01:00
|
|
|
|
sendResponse(res);
|
2018-01-26 00:09:08 +01:00
|
|
|
|
return true;
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 00:09:08 +01:00
|
|
|
|
if(message.cmd == "register-video"){
|
|
|
|
|
// dobili smo sporočilce, ki pravi: "hej jaz imam video, naredi cahen" — ampak preden naredimo cahen,
|
|
|
|
|
// se je potrebno prepričati, da je sporočilce prišlo iz pravilnega zavihka. Trenutno odprt zavihek
|
|
|
|
|
// lahko dobimo to. ID zavihka, iz katerega je prišlo sporočilo, se skriva v sender.tab.id
|
|
|
|
|
// ~<><\\\][=================][///><>~
|
|
|
|
|
// we got a message that says: "hey I have a video, make a mark or something" — but before we do the
|
|
|
|
|
// mark, we should check if the message has truly arrived from currently active tab. We can get the
|
|
|
|
|
// id of currently active tab here. ID of the sender tab is ‘hidden’ in sender.tab.id.
|
|
|
|
|
|
|
|
|
|
_uwbg_registerVideo(sender.tab.id);
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
2018-02-02 00:21:29 +01:00
|
|
|
|
|
|
|
|
|
else if(message.cmd == "uw-enabled-for-site"){
|
|
|
|
|
|
2018-03-11 18:00:47 +01:00
|
|
|
|
var mode = SitesConf.getSiteStatus(BgVars.currentSite);
|
2018-02-02 00:21:29 +01:00
|
|
|
|
|
|
|
|
|
if(BrowserDetect.usebrowser == "firefox")
|
|
|
|
|
return Promise.resolve({response: mode});
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
sendResponse({response: mode});
|
|
|
|
|
}
|
|
|
|
|
catch(chromeIsShitError){};
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if(message.cmd == "enable-for-site"){
|
2018-03-11 18:00:47 +01:00
|
|
|
|
SitesConf.updateSite(BgVars.currentSite, {status: message.option, statusEmbedded: message.option});
|
2018-02-02 00:21:29 +01:00
|
|
|
|
}
|
2018-03-13 00:36:10 +01:00
|
|
|
|
else if(message.cmd == "enable-autoar"){
|
2018-03-13 23:55:38 +01:00
|
|
|
|
ExtensionConf.arDetect.mode = "blacklist";
|
|
|
|
|
Settings.save(ExtensionConf);
|
|
|
|
|
// Comms.sendToAll({cmd: "reload-settings", sender: "uwbg"})
|
|
|
|
|
if(Debug.debug){
|
|
|
|
|
console.log("[uw-bg] autoar set to enabled (blacklist). evidenz:", ExtensionConf);
|
|
|
|
|
}
|
2018-03-13 00:36:10 +01:00
|
|
|
|
}
|
|
|
|
|
else if(message.cmd == "disable-autoar"){
|
2018-03-13 23:55:38 +01:00
|
|
|
|
ExtensionConf.arDetect.mode = "disabled";
|
2018-05-04 00:27:40 +02:00
|
|
|
|
if(message.reason){
|
|
|
|
|
ExtensionConf.arDetect.disabledReason = message.reason;
|
|
|
|
|
} else {
|
|
|
|
|
ExtensionConf.arDetect.disabledReason = '';
|
|
|
|
|
}
|
2018-03-13 23:55:38 +01:00
|
|
|
|
Settings.save(ExtensionConf);
|
|
|
|
|
// Comms.sendToAll({cmd: "reload-settings", sender: "uwbg"});
|
|
|
|
|
if(Debug.debug){
|
|
|
|
|
console.log("[uw-bg] autoar set to disabled. evidenz:", ExtensionConf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(message.cmd == "gib-settings"){
|
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg] we got asked for settings. Returning this:", ExtensionConf);
|
|
|
|
|
|
|
|
|
|
if(BrowserDetect.usebrowser == "firefox")
|
|
|
|
|
return Promise.resolve({response: ExtensionConf});
|
|
|
|
|
|
|
|
|
|
try{
|
2018-03-14 21:19:15 +01:00
|
|
|
|
sendResponse({response: ExtensionConf});
|
2018-03-13 23:55:38 +01:00
|
|
|
|
}
|
|
|
|
|
catch(chromeIsShitError){};
|
|
|
|
|
|
|
|
|
|
return true;
|
2018-03-13 00:36:10 +01:00
|
|
|
|
}
|
|
|
|
|
else if(message.cmd = "autoar-set-timer-playing"){
|
2018-03-13 23:55:38 +01:00
|
|
|
|
|
|
|
|
|
if(Debug.debug)
|
|
|
|
|
console.log("[uw-bg] trying to set new interval for autoAr. New interval is",message.timeout,"ms");
|
|
|
|
|
|
2018-03-13 00:36:10 +01:00
|
|
|
|
var timeout = message.timeout;
|
|
|
|
|
|
|
|
|
|
if(timeout < 1)
|
|
|
|
|
timeout = 1;
|
|
|
|
|
if(timeout > 999)
|
|
|
|
|
timeout = 999;
|
|
|
|
|
|
2018-03-13 23:55:38 +01:00
|
|
|
|
ExtensionConf.arDetect.timer_playing = timeout;
|
|
|
|
|
Settings.save(ExtensionConf);
|
|
|
|
|
Comms.sendToAll({cmd: "update-settings", sender: "uwbg", newConf: ExtensionConf});
|
2018-03-13 00:36:10 +01:00
|
|
|
|
}
|
2018-02-02 00:21:29 +01:00
|
|
|
|
|
2017-12-29 23:34:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-05-24 20:50:37 +02:00
|
|
|
|
// main();
|