Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix survol container getting out of height (issue #96) #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
z-index: 16777271!important;
position: absolute!important;
word-wrap: break-word;
overflow: hidden;
}

.survol-container.dark-theme {
Expand Down
24 changes: 13 additions & 11 deletions js/core.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* core.js is the heart of the extension.
* It is a content script, injected on each page.
*
*
* The core script injects the "survol-container" div into the DOM,
* positions the div, and fill it with content.
*
*
* It uses the mousemove event to detect when the cursor is hovering an anchor link (<a href>)
* It is responsible for checking if this anchor link has an href, if the link should be
* previewed or not, and which preview template should be used.
Expand Down Expand Up @@ -38,7 +38,7 @@ document.addEventListener('DOMContentLoaded', () => {

/* <https://www.chromium.org/Home/chromium-security/extension-content-script-fetches>
* "Later in 2019, Extension Manifest V3 will become available, requiring cross-origin requests to occur in background pages rather than content scripts. This new manifest version will have its own migration period, before support for Extension Manifest V2 is eventually removed from Chrome."
*
*
* Using the background script to pull data from APIs safely, i.e forwarding request from the content script to the background script
* Direct asynchronous messaging, making it as a function to avoid having some request code across every file
*
Expand All @@ -58,7 +58,7 @@ document.addEventListener('DOMContentLoaded', () => {
};

/* insertSurvolDiv
* Parameters:
* Parameters:
* selfReferDisabled - {Boolean} - A setting to disable preview of inner links on a list of websites.
*
* This function:
Expand Down Expand Up @@ -104,23 +104,25 @@ document.addEventListener('DOMContentLoaded', () => {
container.innerHTML = '';
}
}

//get the popup dims
//get the popup width
let popupWidth = container.clientWidth;
let popupHeight = container.clientHeight;

//get the current scroll distance
let scrolled = window.pageYOffset || (document.documentElement || document.body.parentNode || document.body).scrollTop;

//calculate the popup positions
//if the current mouse X position plus the popup width is greater than the width of the viewport set the popup to the left, else right
let leftPosition = (e.pageX + popupWidth + buffer >= window.innerWidth) ? `${e.pageX - (popupWidth + buffer)}px` : `${e.pageX + buffer}px`;
//if the current mouse Y position (mius scroll distance) plus the popup height is greater than viewport height, set the popup above mouse, else below
let topPosition = ((e.pageY - scrolled) + popupHeight + buffer >= window.innerHeight) ? `${e.pageY - (popupHeight + buffer)}px` : `${e.pageY + buffer}px`;
//if the current mouse Y position is in the bottom half of the screen, set the popup above mouse, else below
let topPosition = ((e.pageY - scrolled) >= window.innerHeight / 2) ? 0 : `${e.pageY + buffer}px`;
let bottomPosition = ((e.pageY - scrolled) >= window.innerHeight / 2) ? `${window.innerHeight - e.pageY + buffer}px` : 0;

//update the popup with the calculated values
container.style.left = leftPosition;
container.style.top = topPosition;

container.style.top = topPosition ? topPosition : 'auto';
container.style.bottom = bottomPosition ? bottomPosition : 'auto';
//sets max-height of popup to half height of window - buffer
container.style.maxHeight = `${(window.innerHeight / 2) - buffer}px`;
});

// Insert the container into the DOM
Expand Down