Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function locatePageInSidebar() {
- const breadcrumbElements = Array.from(document.querySelectorAll('.shadow-cursor-breadcrumb > div'));
- let breadcrumbLinks = [];
- const ellipsisButton = document.querySelector('.shadow-cursor-breadcrumb [role="button"][aria-haspopup="dialog"]');
- // Collect visible breadcrumb links
- breadcrumbElements.forEach(element => {
- const link = element.querySelector('a');
- if (link) {
- breadcrumbLinks.push(link.getAttribute('href').split('?')[0]);
- }
- });
- // If ellipsis exists, collect hidden breadcrumb links
- if (ellipsisButton) {
- ellipsisButton.click();
- setTimeout(() => {
- const popup = document.querySelector('[role="dialog"]');
- if (popup) {
- const popupLinks = Array.from(popup.querySelectorAll('[role="menuitem"]'))
- .map(mi => mi.textContent);
- // Insert popup links into breadcrumbLinks array
- breadcrumbLinks.splice(1, 0, ...popupLinks);
- // Close the popup
- document.body.click();
- ellipsisButton.click();
- // Wait for popup to close before expanding sidebar items
- setTimeout(() => {
- expandSidebarItems(breadcrumbLinks);
- }, 300);
- }
- }, 300);
- } else {
- // If no ellipsis, expand sidebar items immediately
- expandSidebarItems(breadcrumbLinks);
- }
- }
- function expandSidebarItems(links) {
- const sidebar = document.querySelector('.notion-sidebar');
- links.forEach((link, index) => {
- setTimeout(() => {
- const sidebarItem = sidebar.querySelector(`[href^="${link}"]`) || document.evaluate(`//a[contains(.,"${link}")]`, sidebar).iterateNext();
- if (sidebarItem) {
- const expandButton = sidebarItem.querySelector('[aria-label="Open"], [aria-label="Close"]');
- if (expandButton && expandButton.getAttribute('aria-label') === 'Open') {
- expandButton.click();
- }
- if (index === links.length - 1) {
- sidebarItem.focus();
- sidebarItem.scrollIntoView({ behavior: 'smooth', block: 'center' });
- }
- }
- }, index * 100); // Stagger expansion to allow DOM updates
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment