reintroducing background script, which will keep track of frames and videos in currently opened tab (not yet implemented)

This commit is contained in:
Tamius Han 2018-01-24 23:15:54 +01:00
parent 85a6a680b9
commit 551bee1724
7 changed files with 134 additions and 34 deletions

View File

@ -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,
}

View File

@ -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;

View File

@ -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){

View File

@ -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();
});
// });

View File

@ -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", "<all_urls>", "*://*.youtube.com/*", "*://youtube.com/*", "*://imdb.com/*", "*://*.imdb.com/*"
"tabs", "storage", "webNavigation", "activeTab", "<all_urls>"
],
"browser_action": {

View File

@ -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", "<all_urls>", "*://*.youtube.com/*", "*://youtube.com/*", "*://imdb.com/*", "*://*.imdb.com/*"
"tabs", "storage", "activeTab", "<all_urls>", "webNavigation"
],
"browser_action": {

View File

@ -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);
})