Insert multiple URL links into NotebookLM sources

by garrulinae

HTML

------
README
------

This code creates a popup input box so that you can paste a list of web links into NotebookLM sources.
When you click the button, some Javascript loops through the links and clicks the buttons for you until all links are added as sources.

To use it, open https://notebooklm.google.com/ in your browser and create or open a notebook

Then open the browser developer tools (usually F12), and navigate to the Console tab.

Paste the JavaScript from below. Press Enter.

Test it with 2 or 3 links first, you'll get the idea.

It's not intended for Youtube links.

JavaScript

// Helper function: wait for an element to appear in the DOM
function waitForElement(selector, timeout = 5000) {
  return new Promise((resolve, reject) => {
    const interval = setInterval(() => {
      const element = document.querySelector(selector);
      if (element) {
        clearInterval(interval);
        resolve(element);
      }
    }, 100);
    setTimeout(() => {
      clearInterval(interval);
      reject(new Error(`Timeout waiting for element: ${selector}`));
    }, timeout);
  });
}

// Function that performs the steps for one link
async function processLink(url) {
  try {
    // 0. Click the "Add source" span
    const addSourceSpan = Array.from(document.querySelectorAll('span.mdc-button__label')).find(
      el => el.textContent.trim() === 'Add source'
    );
    if (addSourceSpan) {
      addSourceSpan.click();
      console.log('Clicked "Add source" span.');
    } else {
      throw new Error('Could not find "Add source" span.');
    }
    
    // Wait briefly for the DOM to update
    await new Promise(resolve => setTimeout(resolve, 1000));

    // 1. Find and click the "Website" span
    const websiteSpan = Array.from(document.querySelectorAll('span')).find(
      el => el.textContent.trim() === 'Website'
    );
    if (websiteSpan) {
      websiteSpan.click();
      console.log('Clicked "Website" span.');
    } else {
      throw new Error('Could not find "Website" span.');
    }

    // Wait briefly for the DOM to update
    await new Promise(resolve => setTimeout(resolve, 1000));

    // 2. Wait for the input element and insert the URL
    const urlInput = await waitForElement('input[formcontrolname="newUrl"]', 5000);
    if (urlInput) {
      urlInput.value = url;
      // Dispatch an input event to trigger any attached listeners
      urlInput.dispatchEvent(new Event('input', { bubbles: true }));
      console.log(`Entered URL: ${url}`);
    }

    // Wait briefly before clicking the button
    await new Promise(resolve =>...