Guest User

Locate in Sidebar function for Notion App

a guest
Mar 21st, 2025
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.16 KB | Source Code | 0 0
  1. function locatePageInSidebar() {
  2.   const breadcrumbElements = Array.from(document.querySelectorAll('.shadow-cursor-breadcrumb > div'));
  3.   let breadcrumbLinks = [];
  4.   const ellipsisButton = document.querySelector('.shadow-cursor-breadcrumb [role="button"][aria-haspopup="dialog"]');
  5.  
  6.   // Collect visible breadcrumb links
  7.   breadcrumbElements.forEach(element => {
  8.     const link = element.querySelector('a');
  9.     if (link) {
  10.       breadcrumbLinks.push(link.getAttribute('href').split('?')[0]);
  11.     }
  12.   });
  13.  
  14.   // If ellipsis exists, collect hidden breadcrumb links
  15.   if (ellipsisButton) {
  16.     ellipsisButton.click();
  17.     setTimeout(() => {
  18.       const popup = document.querySelector('[role="dialog"]');
  19.       if (popup) {
  20.         const popupLinks = Array.from(popup.querySelectorAll('[role="menuitem"]'))
  21.           .map(mi => mi.textContent);
  22.        
  23.         // Insert popup links into breadcrumbLinks array
  24.         breadcrumbLinks.splice(1, 0, ...popupLinks);
  25.        
  26.         // Close the popup
  27.         document.body.click();
  28.           ellipsisButton.click();
  29.        
  30.         // Wait for popup to close before expanding sidebar items
  31.         setTimeout(() => {
  32.           expandSidebarItems(breadcrumbLinks);
  33.         }, 300);
  34.       }
  35.     }, 300);
  36.   } else {
  37.     // If no ellipsis, expand sidebar items immediately
  38.     expandSidebarItems(breadcrumbLinks);
  39.   }
  40. }
  41.  
  42. function expandSidebarItems(links) {
  43.   const sidebar = document.querySelector('.notion-sidebar');
  44.  
  45.   links.forEach((link, index) => {
  46.     setTimeout(() => {
  47.       const sidebarItem = sidebar.querySelector(`[href^="${link}"]`) || document.evaluate(`//a[contains(.,"${link}")]`, sidebar).iterateNext();
  48.       if (sidebarItem) {
  49.         const expandButton = sidebarItem.querySelector('[aria-label="Open"], [aria-label="Close"]');
  50.         if (expandButton && expandButton.getAttribute('aria-label') === 'Open') {
  51.           expandButton.click();
  52.         }
  53.         if (index === links.length - 1) {
  54.           sidebarItem.focus();
  55.           sidebarItem.scrollIntoView({ behavior: 'smooth', block: 'center' });
  56.         }
  57.       }
  58.     }, index * 100); // Stagger expansion to allow DOM updates
  59.   });
  60. }
Advertisement
Add Comment
Please, Sign In to add comment