diff --git a/background.js b/background.js index 154851f..074b64e 100644 --- a/background.js +++ b/background.js @@ -1,10 +1,3 @@ -function doInCurrentTab(tabCallback) { - chrome.tabs.query( - { currentWindow: true, active: true }, - function (tabArray) { tabCallback(tabArray[0]); } - ); -} - chrome.commands.onCommand.addListener(function(command) { if (command === "start") { chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ diff --git a/keyhinter.js b/keyhinter.js index 37875f6..14a16e8 100644 --- a/keyhinter.js +++ b/keyhinter.js @@ -1,12 +1,31 @@ -var isVisible = function(node) { - // From: http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433 - var rect = node.getBoundingClientRect(); - return ( - rect.top >= 0 && - rect.left >= 0 && - rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */ - rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */ - ); +// Checks if the provided child is descendant of the parent (any level) +function is_descendant(parent, child) { + var node = child.parentNode; + while (node !== null) { + console.log(node) + if (node === parent) return true; + node = node.parentNode; + } + return false; +} + +function isVisible(element) { + // Element is obviously not visible + if (element.offsetWidth === 0 || element.offsetHeight === 0) return false; + let rect = element.getClientRects()[0]; + if (!rect) return false; + + // Get the element clicked in the center of the element rect + var element_in_position = document.elementFromPoint((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2); + + // If there's no result, assume not visible + if (!element_in_position) return false; + + // If the element clicked is the same as provided, the element is clearly visible + if (element_in_position === element) return true; + + // If the element is not in the position we clicked, check if it's at least a child node + return is_descendant(element, element_in_position); } class KeyHinter {