|
1519 | 1519 | } |
1520 | 1520 | } |
1521 | 1521 |
|
1522 | | - function preloadLeftSidebar(elementSelector) { |
| 1522 | + function preloadLeftSidebar(elementSelector, retryCount = 0) { |
1523 | 1523 | log(DEBUG, 'preloadLeftSidebar()'); |
| 1524 | + log(DEBUG, 'retryCount', retryCount); |
1524 | 1525 |
|
1525 | | - if (!LEFT_SIDEBAR_PRELOADED) return; |
| 1526 | + if (LEFT_SIDEBAR_PRELOADED) { |
| 1527 | + log(DEBUG, 'Left sidebar already preloaded'); |
| 1528 | + return; |
| 1529 | + } |
1526 | 1530 |
|
1527 | | - const leftModalDialog = HEADER.querySelector(elementSelector.left.modalDialog).remove(); |
| 1531 | + const topDiv = document.querySelector(SELECTORS.sidebars.left.topDiv); |
| 1532 | + if (!topDiv) { |
| 1533 | + if (retryCount < 50) { |
| 1534 | + log(DEBUG, `Selector '${SELECTORS.sidebars.left.topDiv}' not found`); |
| 1535 | + setTimeout(() => preloadLeftSidebar(elementSelector, retryCount + 1), 100); |
| 1536 | + } else { |
| 1537 | + log(DEBUG, `Selector '${SELECTORS.sidebars.left.topDiv}' not found after 2 seconds, giving up`); |
| 1538 | + } |
1528 | 1539 |
|
1529 | | - if (!leftModalDialog) { |
1530 | | - logError(`Selector '${elementSelector.left.modalDialog}' not found`); |
1531 | | - preloadLeftSidebar(elementSelector); |
1532 | 1540 | return; |
1533 | 1541 | } |
| 1542 | + topDiv.style.setProperty('display', 'none'); |
| 1543 | + |
| 1544 | + const button = HEADER.querySelector(SELECTORS.hamburgerButton); |
| 1545 | + if (!button) { |
| 1546 | + if (retryCount < 50) { |
| 1547 | + log(DEBUG, `Selector '${SELECTORS.hamburgerButton}' not found`); |
| 1548 | + setTimeout(() => preloadLeftSidebar(elementSelector, retryCount + 1), 100); |
| 1549 | + } else { |
| 1550 | + log(DEBUG, `Selector '${SELECTORS.hamburgerButton}' not found after 2 seconds, giving up`); |
| 1551 | + } |
1534 | 1552 |
|
1535 | | - window.addEventListener('load', () => { |
1536 | | - HEADER.querySelector(`${SELECTORS.hamburgerButton} button`).click(); |
| 1553 | + return; |
| 1554 | + } else { |
| 1555 | + log(DEBUG, `Clicking selector '${SELECTORS.hamburgerButton}' to preload left sidebar`); |
| 1556 | + button.click(); |
| 1557 | + // This click isn't reliably triggering the dialog, likely due to race conditions on page load |
| 1558 | + } |
| 1559 | + |
| 1560 | + function waitForLeftModalDialog(waitCount = 0) { |
| 1561 | + const leftModalDialog = topDiv.querySelector(elementSelector.left.modalDialog); |
| 1562 | + if (!leftModalDialog) { |
| 1563 | + if (waitCount < 10) { |
| 1564 | + setTimeout(() => waitForLeftModalDialog(waitCount + 1), 100); |
| 1565 | + } else { |
| 1566 | + log(DEBUG, `Selector '${elementSelector.left.modalDialog}' not found after 2 seconds, giving up`); |
| 1567 | + setTimeout(() => preloadLeftSidebar(elementSelector, retryCount + 1), 100); |
| 1568 | + } |
| 1569 | + return; |
| 1570 | + } |
| 1571 | + leftModalDialog.remove(); |
1537 | 1572 | log(INFO, 'Left sidebar preloaded'); |
1538 | | - }); |
| 1573 | + LEFT_SIDEBAR_PRELOADED = true; |
1539 | 1574 |
|
1540 | | - LEFT_SIDEBAR_PRELOADED = true; |
| 1575 | + button.click(); |
| 1576 | + topDiv.style.removeProperty('display'); |
| 1577 | + } |
| 1578 | + |
| 1579 | + waitForLeftModalDialog(); |
1541 | 1580 | } |
1542 | 1581 |
|
1543 | 1582 | function updateSidebars() { |
|
1558 | 1597 | `; |
1559 | 1598 | } |
1560 | 1599 |
|
1561 | | - if (elementConfig.left.preload) preloadLeftSidebar(elementSelector); |
| 1600 | + if (elementConfig.left.preload) { |
| 1601 | + window.addEventListener('load', () => { |
| 1602 | + log(DEBUG, 'Attempting to preload left sidebar'); |
| 1603 | + preloadLeftSidebar(elementSelector); |
| 1604 | + }); |
| 1605 | + } |
1562 | 1606 |
|
1563 | 1607 | if (elementConfig.right.floatUnderneath) { |
1564 | 1608 | HEADER_STYLE.textContent += ` |
|
3672 | 3716 | }, |
3673 | 3717 | sidebars: { |
3674 | 3718 | left: { |
| 3719 | + topDiv: '#__primerPortalRoot__', |
3675 | 3720 | backdrop: 'dialog[data-target="deferred-side-panel.panel"]::backdrop', |
3676 | | - modalDialog: '.Overlay--placement-left', |
| 3721 | + modalDialog: 'div[data-position-regular="left"]', |
3677 | 3722 | }, |
3678 | 3723 | right: { |
3679 | 3724 | topDiv: '#__primerPortalRoot__', |
|
0 commit comments