From 551bee17248a5fc7989f825f6d627fb2a01b7aa5 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Wed, 24 Jan 2018 23:15:54 +0100 Subject: [PATCH] reintroducing background script, which will keep track of frames and videos in currently opened tab (not yet implemented) --- js/lib/Comms.js | 60 +++++++++++++++++++++++++++++++++++++++++- js/modules/PageInfo.js | 5 ++-- js/uw-bg.js | 22 +++++++--------- js/uw.js | 25 +++++++++++++----- manifest-chrome.json | 6 ++--- manifest.json | 28 +++++++++++++++++++- res/popup/js/popup.js | 22 +++++++++------- 7 files changed, 134 insertions(+), 34 deletions(-) diff --git a/js/lib/Comms.js b/js/lib/Comms.js index 8bab757..8a477bf 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -75,8 +75,66 @@ var _com_sendMessageRuntime = async function(message){ } } +// pošlje sporočilce vsem okvirjem v trenutno odprtem zavihku. Vrne tisti odgovor od tistega okvira, ki prispe prvi. +// sends a message to all frames in the currently opened tab. Returns the response of a frame that replied first +var _com_sendToAllFrames = async function(message) { + if(Debug.debug) + console.log("[Comms::_com_sendToAllFrames] sending message to all frames of currenntly active tab"); + + var tabs = await browser.tabs.query({currentWindow: true, active: true}); + + if(Debug.debug) + console.log("[Comms::_com_sendToAllFrames] trying to send message", message, " to tab ", tabs[0], ". (all tabs:", tabs,")"); + + var response = await browser.tabs.sendMessage(tabs[0].id, message); + console.log("[Comms::_com_sendToAllFrames] response is this:",response); + return response; + +// if(BrowserDetect.firefox){ +// return +// } +} + +// pošlje sporočilce vsem okvirjem v trenutno odprtem zavihku in vrne _vse_ odgovore +// sends a message to all frames in currently opened tab and returns all responses +var _com_sendToEachFrame = async function(message) { + if(Debug.debug) + console.log("[Comms::_com_sendToEveryFrame] sending message to every frames of currenntly active tab"); + + try{ + var tabs = await browser.tabs.query({currentWindow: true, active: true}); + var frames = await browser.webNavigation.getAllFrames({tabId: tabs[0].id}); + + if(Debug.debug) + console.log("[Comms::_com_sendToEveryFrame] we have this many frames:", frames.length, "||| tabs:",tabs,"frames:",frames); + + + // pošlji sporočilce vsakemu okvirju, potisni obljubo v tabelo + // send message to every frame, push promise to array + var promises = []; + for(var frame in frames){ + promises.push(browser.tabs.sendMessage(tabs[0].id, message, {frameId: frame.frameId})); + } + + // počakajmo, da so obljube izpolnjene + // wait for all promises to be kept + var responses = Promise.all(promises); + + if(Debug.debug) + console.log("[Comms::_com_sendToEveryFrame] we received responses from all frames", responses); + + return responses; + } + catch(e){ + console.log("[Comms::_com_sendToEveryFrame] something went wrong when getting frames. this is error:", e); + return null; + } +} + var Comms = { queryTabs: _com_queryTabs, sendMessage: _com_sendMessage, - sendMessageRuntime: _com_sendMessageRuntime + sendMessageRuntime: _com_sendMessageRuntime, + sendToEach: _com_sendToEachFrame, + sendToAll: _com_sendToAllFrames, } diff --git a/js/modules/PageInfo.js b/js/modules/PageInfo.js index d8d3204..dea3009 100644 --- a/js/modules/PageInfo.js +++ b/js/modules/PageInfo.js @@ -2,12 +2,13 @@ if(Debug.debug) console.log("Loading: PageInfo.js"); var _pi_hasVideos = function(){ +// return true; var videos = document.getElementsByTagName("video"); if(videos.length == 0) return false; - if(videos[0].style.display == "none") // in this case ultrawidify doesn't even work - return false; +// if(videos[0].style.display == "none") // in this case ultrawidify doesn't even work +// return false; return true; diff --git a/js/uw-bg.js b/js/uw-bg.js index 0e7a92b..d85bb12 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -1,29 +1,27 @@ +console.log("blabla"); + async function main(){ if(Debug.debug) console.log("[uw-bg::main] setting up background script"); await Settings.init(); - if(BrowserDetect.usebrowser == "chrome") - browser.runtime.onMessage.addListener(ChromeCancer.recvmsg); - else - browser.runtime.onMessage.addListener(_uwbg_rcvmsg); + browser.tabs.onActivated.addListener(_uwbg_onTabSwitched); + if(Debug.debug) console.log("[uw-bg::main] listeners registered"); } -async function sendMessage(message){ - console.log("SENDING MESSAGE TO CONTENT SCRIPT"); - var tabs = await Comms.queryTabs({currentWindow: true, active: true}); +async function _uwbg_onTabSwitched(activeInfo){ if(Debug.debug) - console.log("[uw-bg::sendMessage] trying to send message", message, " to tab ", tabs[0], ". (all tabs:", tabs,")"); - - var response = await browser.tabs.sendMessage(tabs[0].id, message); - console.log("[uw-bg::sendMessage] response is this:",response); - return response; + console.log("[uw-bg::onTabSwitched] TAB CHANGED, GETTING INFO FROM MAIN TAB"); + + var tabId = activeInfo.tabId; // just for readability + + Comms.sendToEach({"cmd":"has-video"}); } async function _uwbg_rcvmsg(message){ diff --git a/js/uw.js b/js/uw.js index a825226..9bc7f0c 100644 --- a/js/uw.js +++ b/js/uw.js @@ -1,5 +1,17 @@ -if(Debug.debug) +if(Debug.debug){ console.log("\n\n\n\n\n\n ——— Sᴛλʀᴛɪɴɢ Uʟᴛʀᴀᴡɪᴅɪꜰʏ ———\n << ʟᴏᴀᴅɪɴɢ ᴍᴀɪɴ ꜰɪʟᴇ >>\n\n\n\n"); + try { + if(window.self !== window.top){ + console.log("%cWe aren't in an iframe.", "color: #afc, background: #174"); + } + else{ + console.log("%cWe are in an iframe!", "color: #fea, background: #d31", window.self, window.top); + } + } catch (e) { + console.log("%cWe are in an iframe!", "color: #fea, background: #d31"); + } +} + // global-ish @@ -80,7 +92,8 @@ var _video_recheck_counter = 0; var _video_recheck_period = 60; // on this many retries function ghettoOnChange(){ - +// console.log(".."); +// console.log("events:", $._data($(document)[0], "events")); if(_video_recheck_counter++ > _video_recheck_period){ _video_recheck_counter = 0; @@ -170,10 +183,10 @@ function receiveMessage(message, sender, sendResponse) { console.log("[uw::receiveMessage] we received a message.", message); if(message.cmd == "has-videos"){ - var anyVideos = PageInfo.hasVideos(); + var anyVideos = GlobalVars.video != null; if(Debug.debug) - console.log("[uw::receiveMessage] returning response:", {response: {"hasVideos": anyVideos }}); + console.log("[uw::receiveMessage] are there any videos on this page?", anyVideos, GlobalVars.video, this); if(BrowserDetect.usebrowser == "firefox") return Promise.resolve({response: {"hasVideos": anyVideos }}); @@ -244,6 +257,6 @@ function receiveMessage(message, sender, sendResponse) { } -$(document).ready(function() { +// $(document).ready(function() { main(); -}); +// }); diff --git a/manifest-chrome.json b/manifest-chrome.json index b276636..9d98f51 100644 --- a/manifest-chrome.json +++ b/manifest-chrome.json @@ -26,8 +26,6 @@ "js/conf/Status.js", "js/conf/ExtensionConf.js", - "js/lib/FullScreenDetect.js", - "js/modules/PageInfo.js", "js/modules/ArDetect.js", "js/modules/Resizer.js", @@ -38,8 +36,10 @@ "all_frames": true }], + + "permissions": [ - "tabs", "storage", "activeTab", "", "*://*.youtube.com/*", "*://youtube.com/*", "*://imdb.com/*", "*://*.imdb.com/*" + "tabs", "storage", "webNavigation", "activeTab", "" ], "browser_action": { diff --git a/manifest.json b/manifest.json index 6180a76..35f3e28 100644 --- a/manifest.json +++ b/manifest.json @@ -39,8 +39,34 @@ "all_frames": true }], + "background": { + "scripts": [ + "js/dep/jquery-3.1.1.js", + "js/dep/chrome/chrome-extension-async.js", + + "js/lib/BrowserDetect.js", + "js/lib/StorageManager.js", + "js/lib/Comms.js", + + "js/conf/Debug.js", + "js/conf/Settings.js", + "js/conf/SitesConf.js", + "js/conf/Status.js", + "js/conf/ExtensionConf.js", + + + "js/modules/PageInfo.js", + "js/modules/ArDetect.js", + "js/modules/Resizer.js", + + "js/conf/Keybinds.js", + + "js/uw-bg.js" + ] + }, + "permissions": [ - "tabs", "storage", "activeTab", "", "*://*.youtube.com/*", "*://youtube.com/*", "*://imdb.com/*", "*://*.imdb.com/*" + "tabs", "storage", "activeTab", "", "webNavigation" ], "browser_action": { diff --git a/res/popup/js/popup.js b/res/popup/js/popup.js index 95b6f54..6b2b0ee 100644 --- a/res/popup/js/popup.js +++ b/res/popup/js/popup.js @@ -66,15 +66,19 @@ async function sendMessage(message){ function check4videos(){ - sendMessage({cmd: "has-videos"}) - .then(response => { - if(Debug.debug) - console.log("[popup.js::check4videos] received response:",response); - - if(response.response.hasVideos){ - hasVideos = true; - openMenu(selectedMenu); + Comms.sendToEach({cmd: "has-videos"}) + .then(responses => { + if(Debug.debug){ + console.log("[popup.js::check4videos] received responses:",responses); + for(response of responses){ + console.log(response.response); + } } + +// if(response.response.hasVideos){ +// hasVideos = true; +// openMenu(selectedMenu); +// } }) .catch(error => { if(Debug.debug) @@ -89,7 +93,7 @@ function check4conf(){ sendMessage({cmd: "get-config"}) .then(response => { if(Debug.debug) - console.log("[popup.js::check4conf] received response:",response); + console.log("[popup.js::check4conf] received response:",response, response.response); loadConfig(response.response); })