Skip to content

Commit

Permalink
fixed race condition for popup commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene committed Feb 9, 2021
1 parent 142cef8 commit 07c9d1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
58 changes: 31 additions & 27 deletions src/core/views/popup-command-view/iframe-popup-command-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ browser.runtime.onConnect.addListener(handleConnection);
/**
* Builds all popup html contents
**/
async function initialize (dataset) {
function initialize (dataset) {
// add event listeners
window.addEventListener("contextmenu", handleContextmenu, true);
window.addEventListener("keydown", handleKeyDown, true);
Expand Down Expand Up @@ -42,32 +42,36 @@ async function initialize (dataset) {
// append list
document.body.appendChild(list);

// the width and height the list occupies
const requiredDimensions = {
width: list.scrollWidth,
height: list.scrollHeight
}
// initiate popup display
// also get the available width and height
const availableDimensions = await browser.runtime.sendMessage({
subject: "popupInitiation",
data: requiredDimensions
});

// focus popup frame
window.focus();
window.onblur = handleBlur;

// add scroll buttons if list is scrollable
if (availableDimensions.height < requiredDimensions.height) {
const buttonUp = document.createElement("div");
buttonUp.classList.add("button", "up");
buttonUp.onmouseover = handleScrollButtonMouseover;
const buttonDown = document.createElement("div");
buttonDown.classList.add("button", "down");
buttonDown.onmouseover = handleScrollButtonMouseover;
document.body.append(buttonUp, buttonDown);
}
// append the code to the end of the event queue/wait untill the brwoser finished the reflow/repaint
// otherwise sometimes the dimensions of the list element are 0 due to a race condition
setTimeout(async () => {
// the width and height the list occupies
const requiredDimensions = {
width: list.scrollWidth,
height: list.scrollHeight
}
// initiate popup display
// also get the available width and height
const availableDimensions = await browser.runtime.sendMessage({
subject: "popupInitiation",
data: requiredDimensions
});

// focus popup frame
window.focus();
window.onblur = handleBlur;

// add scroll buttons if list is scrollable
if (availableDimensions.height < requiredDimensions.height) {
const buttonUp = document.createElement("div");
buttonUp.classList.add("button", "up");
buttonUp.onmouseover = handleScrollButtonMouseover;
const buttonDown = document.createElement("div");
buttonDown.classList.add("button", "down");
buttonDown.onmouseover = handleScrollButtonMouseover;
document.body.append(buttonUp, buttonDown);
}
}, 0);
}


Expand Down
2 changes: 1 addition & 1 deletion src/core/views/popup-command-view/popup-command-view.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ html {
}

#list {
display: inline-block;
display: block;
max-width: 300px;
padding: 1rem 0;
cursor: default;
Expand Down

0 comments on commit 07c9d1a

Please sign in to comment.