Videos from tabs/frames are being registered in the background script

This commit is contained in:
Tamius Han 2018-11-07 00:03:06 +01:00
parent 8760626e8f
commit 5cf3ec9b86
4 changed files with 110 additions and 14 deletions

View File

@ -9,7 +9,8 @@ Debug = {
debugResizer: true,
debugArDetect: true,
debugStorage: false,
comms: false,
// comms: false,
comms: true,
// showArDetectCanvas: true,
flushStoredSettings: true,
// flushStoredSettings: false,

View File

@ -137,16 +137,24 @@ class CommsClient {
}
registerVideo(){
if (Debug.debug && Debug.comms) {
console.log(`[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page.");
}
this.port.postMessage({cmd: "has-video"});
}
unregisterVideo(){
if (Debug.debug && Debug.comms) {
console.log(`[CommsClient::unregisterVideo] <${this.commsId}>`, "Unregistering video for current page.");
}
this.port.postMessage({cmd: "noVideo"}); // ayymd
}
announceZoom(scale){
this.port.postMessage({cmd: "announce-zoom", zoom: scale});
this.registerVideo()
}
unregisterVideo(){
this.port.postMessage({cmd: "noVideo"}); // ayymd
}
}
class CommsServer {
@ -298,6 +306,10 @@ class CommsServer {
this.settings.save();
} else if (message.cmd === 'set-zoom') {
this.sendToActive(message);
} else if (message.cmd === 'has-video') {
this.server.registerVideo(port.sender);
} else if (message.cmd === 'noVideo') {
this.server.unregisterVideo(port.sender);
}
}

View File

@ -11,17 +11,12 @@ class PageInfo {
this.lastUrl = window.location.href;
this.extensionMode = extensionMode;
this.rescan(RescanReason.PERIODIC);
this.scheduleUrlCheck();
if(comms){
this.comms = comms;
}
if(this.videos.length > 0){
console.log("registering video")
comms.registerVideo();
}
this.rescan(RescanReason.PERIODIC);
this.scheduleUrlCheck();
this.currentZoomScale = 1;
}
@ -34,6 +29,7 @@ class PageInfo {
clearTimeout(this.rescanTimer);
}
for (var video of this.videos) {
this.comms.unregister(video.id)
video.destroy();
}
}
@ -46,6 +42,8 @@ class PageInfo {
}
rescan(rescanReason){
const oldVideoCount = this.videos.length;
try{
var vids = document.getElementsByTagName('video');
@ -92,7 +90,7 @@ class PageInfo {
continue;
} else {
if(Debug.debug && Debug.periodic && Debug.videoRescan){
console.log("[PageInfo::rescan] found new video candidate:", video)
console.log("[PageInfo::rescan] found new video candidate:", video, "NOTE:: Video initialization starts here:\n--------------------------------\n")
}
v = new VideoData(video, this.settings, this);
// console.log("[PageInfo::rescan] v is:", v)
@ -101,12 +99,31 @@ class PageInfo {
this.videos.push(v);
if(Debug.debug && Debug.periodic && Debug.videoRescan){
console.log("[PageInfo::rescan] videos[] is now this:", this.videos,"\n\n\n\n\n\n\n\n")
console.log("[PageInfo::rescan] END VIDEO INITIALIZATION\n\n\n-------------------------------------\nvideos[] is now this:", this.videos,"\n\n\n\n\n\n\n\n")
}
}
}
this.removeDestroyed();
// če smo ostali brez videev, potem odregistriraj stran.
// če nismo ostali brez videev, potem registriraj stran.
//
// if we're left withotu videos on the current page, we unregister the page.
// if we have videos, we call register.
// if(Debug.debug) {
// console.log("[PageInfo::rescan] Comms:", this.comms, "\nvideos.length:", this.videos.length, "\nold video count:", oldVideoCount)
// }
if (this.comms) {
if (this.videos.length != oldVideoCount) { // only if number of videos changed, tho
if (this.videos.length > 0) {
this.comms.registerVideo({host: window.location.host, location: window.location});
} else {
this.comms.unregisterVideo({host: window.location.host, location: window.location});
}
}
}
}catch(e){
console.log("rescan error:",e)
}

View File

@ -12,6 +12,8 @@ class UWServer {
this.hasVideos = false;
this.currentSite = "";
this.setup();
this.videoTabs = {};
}
async setup() {
@ -20,7 +22,6 @@ class UWServer {
await this.settings.init();
this.comms = new CommsServer(this);
var ths = this;
if(BrowserDetect.firefox) {
browser.tabs.onActivated.addListener(function(m) {ths.onTabSwitched(m)});
@ -79,6 +80,71 @@ class UWServer {
//TODO: change extension icon based on whether there's any videos on current page
}
registerVideo(sender) {
if (Debug.debug && Debug.comms) {
console.log("[UWServer::registerVideo] registering video.\nsender:", sender);
}
const tabHostname = this.extractHostname(sender.tab.url);
const frameHostname = this.extractHostname(sender.url);
// preveri za osirotele/zastarele vrednosti ter jih po potrebi izbriši
// check for orphaned/outdated values and remove them if neccessary
if (this.videoTabs[sender.tab.id]) {
if (this.videoTabs[sender.tab.id].host != tabHostname) {
delete this.videoTabs[sender.tab.id]
} else {
if(this.videoTabs[sender.tab.id].frames[sender.frameId]) {
if (this.videoTabs[sender.tab.id].frames[sender.frameId].host != frameHostname) {
delete this.videoTabs[sender.tab.id].frames[sender.frameId];
}
}
}
}
if (this.videoTabs[sender.tab.id]) {
if (this.videoTabs[sender.tab.id].frames[sender.frameId]) {
return; // existing value is fine, no need to act
} else {
this.videoTabs[sender.tab.id].frames[sender.frameId] = {
host: frameHostname,
url: sender.url
}
}
} else {
this.videoTabs[sender.tab.id] = {
host: tabHostname,
url: sender.tab.url,
frames: {}
};
this.videoTabs[sender.tab.id].frames[sender.frameId] = {
host: frameHostname,
url: sender.url
}
}
if (Debug.debug && Debug.comms) {
console.log("[UWServer::registerVideo] video registered. current videoTabs:", this.videoTabs);
}
}
unregisterVideo(sender) {
if (Debug.debug && Debug.comms) {
console.log("[UWServer::unregisterVideo] unregistering video.\nsender:", sender);
}
if (this.videoTabs[sender.tab.id]) {
if ( Object.keys(this.videoTabs[sender.tab.id].frames).length <= 1) {
delete this.videoTabs[sender.tab.id]
} else {
if(this.videoTabs[sender.tab.id].frames[sender.frameId]) {
delete this.videoTabs[sender.tab.id].frames[sender.frameId];
}
}
}
if (Debug.debug && Debug.comms) {
console.log("[UWServer::ungisterVideo] video unregistered. current videoTabs:", this.videoTabs);
}
}
}
var server = new UWServer();