Skip to content

AgriciDaniel/claude-blog / skills/blog-notebooklm

blog-notebooklm

Verified

Query a user's Gemini Notebook (formerly NotebookLM) for source-grounded answers through a logged-in browser session on their Google account. Uses the account's usage limits and writes browser state. Runs only on an explicit request: /blog notebooklm, "ask my Gemini Notebook", "ask my NotebookLM notebook", or "query my notebook library". Also used by blog-write when the user named a notebook for research. Not for generic research or note-taking.

scripts/browser_utils.py

View on GitHub
"""
Browser Utilities for NotebookLM Skill
Handles browser launching, stealth features, and common interactions
"""

import json
import time
import random
from pathlib import Path
from typing import Optional, List

from config import (
    BROWSER_PROFILE_DIR, STATE_FILE, BROWSER_ARGS, USER_AGENT,
    ensure_private_dir, fail_missing_dependency,
)

try:
    from patchright.sync_api import Playwright, BrowserContext, Page
except ImportError as _exc:
    fail_missing_dependency("patchright", _exc)


def _harden_perms(path) -> None:
    """Set restrictive permissions on a credential file or directory.

    Files: 0o600 (owner read/write only).
    Dirs: 0o700 (owner traverse only).
    Recursively applies to all children of dirs.
    Best-effort: failures are swallowed silently (Windows fs may lack POSIX mode bits).

    Duplicated here (also lives in auth_manager) to avoid an import cycle:
    auth_manager already imports browser_utils.
    """
    p = Path(path)
    try:
        if p.is_dir():
            p.chmod(0o700)
            for child in p.rglob("*"):
                try:
                    child.chmod(0o600 if child.is_file() else 0o700)
                except OSError:
                    pass
        elif p.is_file():
            p.chmod(0o600)
    except OSError:
        pass


class BrowserFactory:
    """Factory for creating configured browser contexts"""

    @staticmethod
    def launch_persistent_context(
        playwright: Playwright,
        headless: bool = True,
        user_data_dir: str = str(BROWSER_PROFILE_DIR)
    ) -> BrowserContext:
        """
        Launch a persistent browser context with anti-detection features
        and cookie workaround.
        """
        # The profile holds the Google session cookies: owner-only from creation
        ensure_private_dir(user_data_dir)

        # Launch persistent context. accept_downloads=False: this skill never
        # downloads files, so a page-triggered download must not be saved.
        # Dialogs are never auto-accepted (Playwright dismisses them by default).
        context = playwright.chromium.launch_persistent_context(
            user_data_dir=user_data_dir,
            channel="chrome",  # Use real Chrome
            headless=headless,
            accept_downloads=False,
            no_viewport=True,
            ignore_default_args=["--enable-automation"],
            user_agent=USER_AGENT,
            args=BROWSER_ARGS
        )

        # Harden the persistent profile dir recursively (VULN-004 mitigation).
        # Patchright writes cookies, tokens, and cache here under default 0o755.
        _harden_perms(user_data_dir)

        # Cookie Workaround for Playwright bug #36139
        # Session cookies (expires=-1) don't persist in user_data_dir automatically
        BrowserFactory._inject_cookies(context)

        return context

    @staticmethod
    def _inject_cookies(context: BrowserContext):
        """Inject cookies from state.json if available"""
        if STATE_FILE.is_symlink():
            print("  ⚠️  Ignoring state.json: it is a symlink")
        elif STATE_FILE.exists():
            try:
                with open(STATE_FILE, 'r') as f:
                    state = json.load(f)
                    if 'cookies' in state and len(state['cookies']) > 0:
                        context.add_cookies(state['cookies'])
                        # print(f"  🔧 Injected {len(state['cookies'])} cookies from state.json")
            except Exception as e:
                # Report the error type only: never echo file contents or cookie values.
                print(f"  ⚠️  Could not load state.json ({type(e).__name__})")


class StealthUtils:
    """Human-like interaction utilities"""

    @staticmethod
    def random_delay(min_ms: int = 100, max_ms: int = 500):
        """Add random delay"""
        time.sleep(random.uniform(min_ms / 1000, max_ms / 1000))

    @staticmethod
    def human_type(page: Page, selector: str, text: str, wpm_min: int = 320, wpm_max: int = 480):
        """Type with human-like speed"""
        element = page.query_selector(selector)
        if not element:
            # Try waiting if not immediately found
            try:
                element = page.wait_for_selector(selector, timeout=2000)
            except Exception:
                pass
        
        if not element:
            print(f"⚠️ Element not found for typing: {selector}")
            return

        # Click to focus
        element.click()
        
        # Type
        for char in text:
            element.type(char, delay=random.uniform(25, 75))
            if random.random() < 0.05:
                time.sleep(random.uniform(0.15, 0.4))

    @staticmethod
    def realistic_click(page: Page, selector: str):
        """Click with realistic movement"""
        element = page.query_selector(selector)
        if not element:
            return

        # Optional: Move mouse to element (simplified)
        box = element.bounding_box()
        if box:
            x = box['x'] + box['width'] / 2
            y = box['y'] + box['height'] / 2
            page.mouse.move(x, y, steps=5)

        StealthUtils.random_delay(100, 300)
        element.click()
        StealthUtils.random_delay(100, 300)

    @staticmethod
    def random_mouse_movement(page: Page, moves: int = 3):
        """Move the mouse across random visible points."""
        viewport = page.viewport_size or {"width": 1280, "height": 720}
        width = max(1, int(viewport.get("width", 1280)))
        height = max(1, int(viewport.get("height", 720)))
        for _ in range(max(1, moves)):
            x = random.randint(1, width)
            y = random.randint(1, height)
            page.mouse.move(x, y, steps=random.randint(3, 8))
            StealthUtils.random_delay(80, 220)
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall