diff --git a/js/modules/PageInfo.js b/js/modules/PageInfo.js new file mode 100644 index 0000000..d8d3204 --- /dev/null +++ b/js/modules/PageInfo.js @@ -0,0 +1,18 @@ +if(Debug.debug) + console.log("Loading: PageInfo.js"); + +var _pi_hasVideos = function(){ + 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; + + + return true; +} + +var PageInfo = { + hasVideos: _pi_hasVideos +} diff --git a/js/uw-bg.js b/js/uw-bg.js index eaeaaa6..e13cc1d 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -16,21 +16,26 @@ async function sendMessage(message){ if(Debug.debug) console.log("[uw-bg::sendMessage] trying to send message", message, " to tab ", tabs[0], ". (all tabs:", tabs,")"); - browser.tabs.sendMessage(tabs[0].id, message); + var response = await browser.tabs.sendMessage(tabs[0].id, message); + return response; } async function _uwbg_rcvmsg(message){ if(Debug.debug){ - console.log("received message", message); + console.log("[uw-bg::_uwbg_rcvmsg] received message", message); } - /* - message.sender = "uwbg"; - message.receiver = "uw";*/ - if(message.cmd == "debug-ping"){ - + message.sender = "uwbg"; + message.receiver = "uw"; + + if(message.cmd == "has-videos"){ + var response = await sendMessage(message); + if(Debug.debug){ + console.log("[uw-bg::_uwbg_rcvmsg] received response!", message); + } + return Promise.resolve(response); } else if(message.cmd == "force-ar"){ sendMessage(message); // args: {cmd: string, newAr: number/"auto"} diff --git a/js/uw.js b/js/uw.js index 00d6815..a0c9414 100644 --- a/js/uw.js +++ b/js/uw.js @@ -108,6 +108,12 @@ function receiveMessage(message) { if(message.cmd == "reload-settings"){ Settings.reload(); } + if(message.cmd == "has-videos"){ + var anyVideos = PageInfo.hasVideos(); + return Promise.resolve({response: {"hasVideos": anyVideos }}); + } } + + main(); diff --git a/manifest.json b/manifest.json index 8872d6b..dd00ce6 100644 --- a/manifest.json +++ b/manifest.json @@ -38,6 +38,7 @@ "js/lib/FullScreenDetect.js", + "js/modules/PageInfo.js", "js/modules/ArDetect.js", "js/modules/Resizer.js", "js/conf/Keybinds.js", diff --git a/res/popup/js/popup.js b/res/popup/js/popup.js index 750833d..7e3f641 100644 --- a/res/popup/js/popup.js +++ b/res/popup/js/popup.js @@ -1,35 +1,117 @@ if(Debug.debug) console.log("[popup.js] loading popup script!"); +document.getElementById("uw-version").textContent = browser.runtime.getManifest().version; -async function test(){ - await StorageManager.getopt_async("uw-settings"); - console.log("popup: settings machine :b:roke?", Settings); +var Menu = {}; +Menu.noVideo = document.getElementById("no-videos-display"); +Menu.general = document.getElementById("extension-mode"); +Menu.arSettings = document.getElementById("aspect-ratio-settings"); +Menu.cssHacks = document.getElementById("css-hacks-settings"); +Menu.about = document.getElementById("panel-about"); + +var MenuTab = {}; +MenuTab.general = document.getElementById("_menu_general"); +MenuTab.arSettings = document.getElementById("_menu_aspectratio"); +MenuTab.cssHacks = document.getElementById("_menu_hacks"); +MenuTab.about = document.getElementById("_menu_about"); + +var selectedMenu = "arSettings"; +var hasVideos = false; + +function check4videos(){ + var command = {}; + command.cmd = "has-videos"; + command.sender = "popup"; + command.receiver = "uwbg"; + browser.runtime.sendMessage(command) + .then(response => { + if(Debug.debug) + console.log("[popup.js::check4videos] received response:",response); + + if(response.response.hasVideos){ + hasVideos = true; + openMenu(selectedMenu); + } + }) + .catch(error => { + if(Debug.debug) + console.log("%c[popup.js::check4videos] sending message failed with error", "color: #f00", error, "%c retrying in 1s ...", "color: #f00"); + + setTimeout(check4videos, 1000); + }); } -function changeAr(ar){ - if(Debug.debug) - console.log("[popup.js] changing ar to ", ar) +function openMenu(menu){ + if(Debug.debug){ + console.log("[popup.js::openMenu] trying to open menu", menu, "| element: ", Menu[menu]); + } + + for(var m in Menu){ + Menu[m].classList.add("hidden"); + } + for(var m in MenuTab){ + if(MenuTab[m]) + MenuTab[m].classList.remove("selected"); + } + + if(menu == "arSettings" || menu == "cssHacks" ){ + if(!hasVideos) + Menu.noVideo.classList.remove("hidden"); + else{ + Menu[menu].classList.remove("hidden"); + if(Debug.debug){ + console.log("[popup.js::openMenu] unhid", menu, "| element: ", Menu[menu]); + } + } + } + else{ + Menu[menu].classList.remove("hidden"); + if(Debug.debug){ + console.log("[popup.js::openMenu] unhid", menu, "| element: ", Menu[menu]); + } + } + + if(menu != "noVideo"){ + selectedMenu = menu; + MenuTab[menu].classList.add("selected"); + } } -test(); - -// browser.runtime.sendMessage("test"); - - document.addEventListener("click", (e) => { - console.log("we clicked. e?",e); +// console.log("we clicked. e?",e); function getcmd(e){ - console.log("extracting command from e", e); +// console.log("extracting command from e", e); var command = {}; command.sender = "popup"; command.receiver = "uwbg"; + if(e.target.classList.contains("disabled")) + return; + + if(e.target.classList.contains("menu-item")){ + if(e.target.classList.contains("_menu_general")){ + openMenu("general"); + } + else if(e.target.classList.contains("_menu_aspectratio")){ + openMenu("arSettings"); + } + else if(e.target.classList.contains("_menu_hacks")){ + openMenu("cssHacks"); + } + else if(e.target.classList.contains("_menu_about")){ + openMenu("about"); + } + + // don't send commands + return; + } + if(e.target.classList.contains("_changeAr")){ if(e.target.classList.contains("_ar_auto")){ command.cmd = "force-ar"; @@ -101,6 +183,10 @@ document.addEventListener("click", (e) => { } var command = getcmd(e); - console.log("command: ", command); - browser.runtime.sendMessage(command); + if(command) + browser.runtime.sendMessage(command); }); + + + +check4videos(); diff --git a/res/popup/popup.html b/res/popup/popup.html index 698b4b6..e373e4d 100644 --- a/res/popup/popup.html +++ b/res/popup/popup.html @@ -5,10 +5,11 @@
@@ -95,17 +111,24 @@ Ultrawidify: Quick settings