Skip to content

Instantly share code, notes, and snippets.

@volkanunsal
Last active September 13, 2025 11:59
Show Gist options
  • Save volkanunsal/284a7804684b5741e15ec582e3327d1e to your computer and use it in GitHub Desktop.
Save volkanunsal/284a7804684b5741e15ec582e3327d1e to your computer and use it in GitHub Desktop.

Auto Close Dropdown for NotebookLM

Auto Close Dropdown is a userscript that automatically closes dropdown menus in Google NotebookLM after clicking away from the menu. This improves the user experience by reducing the likelihood of "sticky" dropdowns that remain open unintentionally.

Key Features

  • Automatically closes dropdown menus when clicking outside of them

Getting Started

🔧 Installation

  1. Install Tampermonkey browser extension
  2. Click on the "Raw" button of the script below.
  3. Visit notebooklm.google.com to see the magic! ✨
// @ts-nocheck
// ==UserScript==
// @name Auto Close Dropdown on Click Away
// @namespace https://github.com/volkanunsal
// @version 2025-09-13:3
// @description Close dropdown when clicking outside of it
// @author Volkan Unsal
// @downloadURL https://gist.githubusercontent.com/volkanunsal/284a7804684b5741e15ec582e3327d1e/raw/auto-close-dropdown.user.js
// @updateURL https://gist.githubusercontent.com/volkanunsal/284a7804684b5741e15ec582e3327d1e/raw/auto-close-dropdown.user.js
// @match https://notebooklm.google.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
// ==/UserScript==
"use strict";(()=>{var g=class{constructor(e={}){this.namespace=e.namespace,this.prefix=[this.namespace,e.prefix].filter(Boolean).join(" "),this.enabled=e.enabled!==!1,this.timestamp=e.timestamp!==!1,this.timestampFormat=e.timestampFormat||"locale"}getTimestamp(){let e=new Date;return this.timestampFormat==="ISO"?e.toISOString():e.toLocaleString()}formatMessage(e,...t){let r=[this.prefix];return this.timestamp&&r.push(`[${this.getTimestamp()}]`),r.push(`[${e.toUpperCase()}]`),[r.join(" "),...t]}debug(...e){this.enabled&&console.debug(...this.formatMessage("debug",...e))}info(...e){this.enabled&&console.info(...this.formatMessage("info",...e))}warn(...e){this.enabled&&console.warn(...this.formatMessage("warn",...e))}error(...e){this.enabled&&console.error(...this.formatMessage("error",...e))}log(...e){this.enabled&&console.log(...this.formatMessage("log",...e))}custom(e,...t){this.enabled&&console.log(...this.formatMessage(e,...t))}group(e,t){if(!this.enabled)return t();console.group(`${this.prefix} ${e}`);try{t()}finally{console.groupEnd()}}groupCollapsed(e,t){if(!this.enabled)return t();console.groupCollapsed(`${this.prefix} ${e}`);try{t()}finally{console.groupEnd()}}table(e,t){this.enabled&&(this.info("Table data:"),console.table(e,t))}time(e){this.enabled&&console.time(`${this.prefix} ${e}`)}timeEnd(e){this.enabled&&console.timeEnd(`${this.prefix} ${e}`)}timeLog(e){this.enabled&&console.timeLog(`${this.prefix} ${e}`)}enable(){this.enabled=!0}disable(){this.enabled=!1}setPrefix(e){this.prefix=e}};function d(n={}){return new g(n)}function m(n){let{parentSelector:e="body",eventType:t,targetSelector:r,callback:p,useCapture:l=!1,preventDefault:s=!1,stopPropagation:i=!1,parentElement:h,namespace:w}=n,o=d({prefix:"[addEventDelegation]",namespace:w});if(o.info("Setting up event delegation",{eventType:t,targetSelector:r,parentSelector:e,useCapture:l,preventDefault:s,stopPropagation:i}),!t||!r||!p)return o.error("eventType, targetSelector, and callback are required"),null;if(!e&&!h)return o.error("Either parentSelector or parentElement must be provided"),null;let a=h;if(!a&&e){let c=document.querySelector(e);if(!c)return o.error(`Parent element not found with selector: ${e}`),null;a=c}if(!a)return o.error("No valid parent element found"),null;o.debug("Found parent element for event delegation",{parentTagName:a.tagName,parentId:a.id,parentClasses:a.className});let b=c=>{let f=c.target;if(!f||!f.closest)return;o.debug(`Event of type ${t} triggered`,{event:c,target:f});let u=f.closest(r);if(u&&a&&a.contains(u)){o.debug(`Event delegation triggered for ${t} on ${r}`,{eventType:t,targetSelector:r,matchingElement:u.tagName+(u.id?`#${u.id}`:"")+(u.className?`.${u.className.replace(/\s+/g,".")}`:""),parentSelector:e}),s&&(c.preventDefault(),o.debug("Prevented default event behavior")),i&&(c.stopPropagation(),o.debug("Stopped event propagation"));try{p({event:c,matchingElement:u})}catch(y){o.error("Error in event delegation callback:",y)}}};return a.addEventListener(t,b,l),o.info(`Event delegation set up: ${t} on ${r} within ${e||"provided parent"}`,{eventType:t,targetSelector:r,parentSelector:e,useCapture:l,preventDefault:s,stopPropagation:i}),{cleanup(){a&&(a.removeEventListener(t,b,l),o.info(`Event delegation cleaned up: ${t} on ${r}`))},getInfo(){return{eventType:t,targetSelector:r,...e&&{parentSelector:e},useCapture:l,preventDefault:s,stopPropagation:i,isActive:!!a}}}}function v({selector:n,onClickAway:e,onClickInside:t,namespace:r="Library"}){let p=d({prefix:"[whenClickAway]",namespace:r}),l=s=>{let i;if(typeof n=="string")i=document.querySelector(n);else if(typeof n=="function")i=n();else{p.warn("whenClickAway: selector must be a string or function");return}i&&(i.contains(s.target)?t&&t({event:s,targetElement:i}):e({event:s,targetElement:i}))};return document.addEventListener("click",l),()=>{document.removeEventListener("click",l)}}(function(){"use strict";let n="[AutoCloseDropdown]",e=d({prefix:"UserScript",namespace:n}),t=null;m({targetSelector:'.artifact-library-container [mattooltip="More"]',parentSelector:"html",eventType:"mouseup",callback:({matchingElement:r})=>{t=t===r?null:r,e.log("Dropdown button clicked:",r)},namespace:n}),m({targetSelector:'[aria-label="Project Actions Menu"]',parentSelector:"html",eventType:"mouseup",callback:({matchingElement:r})=>{t=t===r?null:r,e.log("Dropdown button clicked:",r)},namespace:n}),v({namespace:n,selector:".cdk-overlay-container",onClickAway:()=>{t&&(t&&t.click(),e.log("Dropdown closed due to click away",{clickedButtonElement:t}),t=null)},onClickInside:()=>{t=null,e.log("Clicked inside dropdown, not closing")}})})();})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment