All files / src/views crawl-modal.ts

100% Statements 125/125
100% Branches 39/39
100% Functions 15/15
100% Lines 120/120

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                    28x 28x 28x 13x 13x 4x   9x 3x   6x     525x           41x 41x 41x         41x 41x 41x       41x 41x 2x 2x 2x 2x 1x 1x 1x       41x         41x 1x     41x 4x 4x 4x   37x 36x         41x 41x 41x   41x   41x           41x 41x       41x       41x 41x   41x               41x   41x 41x 41x   41x 41x 47x 47x 2x 2x   45x 45x     41x   41x 41x       41x       41x 41x   41x   41x 41x   41x 41x       41x       41x 41x   41x   41x 41x         41x   41x 41x         41x       41x   41x 62x 62x 62x 62x 62x 62x 44x 44x     41x 41x   41x 41x 41x 22x 22x 1x 1x   21x   21x       21x 7x 7x 7x   14x 14x 14x 14x 7x 7x 7x   7x 7x 7x     14x       22x     22x 22x 22x     41x 41x      
import { App, Modal, Notice, setIcon } from "obsidian";
import type LilbeePlugin from "../main";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, ensureUrlScheme } from "../utils";
import { CAPABILITY, CONFIG_KEY, CRAWL_RENDER_MODE, type CrawlRenderMode } from "../types";
import { applyConfig } from "../utils/reindex";
 
type ParseResult = { value: number | null; error: string | null };
 
function parseOptionalCount(raw: string, opts: { allowZero: boolean }): ParseResult {
    const errMsg = opts.allowZero ? MESSAGES.ERROR_CRAWL_DEPTH_INVALID : MESSAGES.ERROR_CRAWL_MAX_PAGES_POSITIVE;
    const trimmed = raw.trim();
    if (trimmed === "") return { value: null, error: null };
    const n = Number(trimmed);
    if (!Number.isFinite(n) || !Number.isInteger(n) || n < 0) {
        return { value: null, error: errMsg };
    }
    if (n === 0 && !opts.allowZero) {
        return { value: null, error: errMsg };
    }
    return { value: n, error: null };
}
 
const asInput = (el: HTMLElement): HTMLInputElement => el as unknown as HTMLInputElement;
 
export class CrawlModal extends Modal {
    private plugin: LilbeePlugin;
 
    constructor(app: App, plugin: LilbeePlugin) {
        super(app);
        this.plugin = plugin;
        bindEscapeToClose(this);
    }
 
    /** The Chromium install offer, shown only when the server cannot render with a browser. */
    private renderBrowserSetup(contentEl: HTMLElement, browserInput: HTMLElement): void {
        const browserSetup = contentEl.createDiv({ cls: "lilbee-crawl-browser-setup" });
        browserSetup.createSpan({ text: MESSAGES.NOTICE_CRAWL_BROWSER_MISSING });
        const installBtn = browserSetup.createEl("button", {
            text: MESSAGES.BUTTON_INSTALL_CHROMIUM,
            attr: { type: "button" },
        });
        browserSetup.hide();
        installBtn.addEventListener("click", () => {
            installBtn.disabled = true;
            void this.plugin.installCrawlerBrowser().then((installed) => {
                installBtn.disabled = false;
                if (!installed) return;
                asInput(browserInput).disabled = false;
                asInput(browserInput).checked = true;
                browserSetup.hide();
            });
        });
 
        void this.applyBrowserCapability(browserInput, browserSetup);
    }
 
    /** Resolve both before touching the toggle: the sticky default must not re-check a disabled box. */
    private async applyBrowserCapability(browserInput: HTMLElement, browserSetup: HTMLElement): Promise<void> {
        const [cfg, browserReady] = await Promise.all([
            this.plugin.api.config().catch(() => null),
            this.plugin.api.getCapability(CAPABILITY.CRAWLING_BROWSER),
        ]);
        if (!browserReady) {
            asInput(browserInput).disabled = true;
            browserSetup.show();
            return;
        }
        if (cfg !== null) {
            asInput(browserInput).checked = cfg.crawl_render_mode === CRAWL_RENDER_MODE.BROWSER;
        }
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-crawl-modal");
 
        contentEl.createEl("h2", { text: MESSAGES.TITLE_CRAWL_WEB_PAGE });
 
        const urlInput = contentEl.createEl("input", {
            cls: "lilbee-crawl-url",
            placeholder: MESSAGES.PLACEHOLDER_URL,
            attr: { type: "text", title: MESSAGES.TOOLTIP_CRAWL_URL },
        });
 
        const recursiveRow = contentEl.createDiv({ cls: "lilbee-crawl-recursive-row" });
        const recursiveLabel = recursiveRow.createEl("label", {
            cls: "lilbee-crawl-recursive",
            attr: { title: MESSAGES.TOOLTIP_CRAWL_RECURSIVE },
        });
        const recursiveInput = recursiveLabel.createEl("input", {
            cls: "lilbee-crawl-recursive-input",
            attr: { type: "checkbox" },
        });
        asInput(recursiveInput).checked = false;
        recursiveLabel.createSpan({ text: MESSAGES.LABEL_CRAWL_RECURSIVE });
 
        const infoBtn = recursiveRow.createEl("button", {
            cls: "lilbee-crawl-info-btn",
            attr: {
                type: "button",
                "aria-label": MESSAGES.LABEL_CRAWL_RECURSIVE_INFO,
                "aria-expanded": "false",
            },
        });
        setIcon(infoBtn, "info");
 
        const notice = contentEl.createDiv({ cls: "lilbee-crawl-notice" });
        notice.setAttribute("hidden", "hidden");
        notice.setText(MESSAGES.NOTICE_CRAWL_RECURSIVE);
 
        let noticeOpen = false;
        const setNoticeOpen = (open: boolean): void => {
            noticeOpen = open;
            if (open) {
                notice.removeAttribute("hidden");
                infoBtn.setAttribute("aria-expanded", "true");
            } else {
                notice.setAttribute("hidden", "hidden");
                infoBtn.setAttribute("aria-expanded", "false");
            }
        };
        infoBtn.addEventListener("click", () => setNoticeOpen(!noticeOpen));
 
        const browserRow = contentEl.createDiv({ cls: "lilbee-crawl-browser-row" });
        const browserLabel = browserRow.createEl("label", {
            cls: "lilbee-crawl-browser",
            attr: { title: MESSAGES.TOOLTIP_CRAWL_USE_BROWSER },
        });
        const browserInput = browserLabel.createEl("input", {
            cls: "lilbee-crawl-browser-input",
            attr: { type: "checkbox" },
        });
        asInput(browserInput).checked = false;
        browserLabel.createSpan({ text: MESSAGES.LABEL_CRAWL_USE_BROWSER });
 
        this.renderBrowserSetup(contentEl, browserInput);
 
        const advanced = contentEl.createEl("details", { cls: "lilbee-crawl-advanced" });
        advanced.createEl("summary", { text: MESSAGES.LABEL_CRAWL_ADVANCED });
 
        const subdomainsRow = advanced.createDiv({ cls: "lilbee-crawl-subdomains-row" });
        const subdomainsLabel = subdomainsRow.createEl("label", {
            cls: "lilbee-crawl-subdomains",
            attr: { title: MESSAGES.TOOLTIP_CRAWL_SUBDOMAINS },
        });
        const subdomainsInput = subdomainsLabel.createEl("input", {
            cls: "lilbee-crawl-subdomains-input",
            attr: { type: "checkbox" },
        });
        asInput(subdomainsInput).checked = false;
        subdomainsLabel.createSpan({ text: MESSAGES.LABEL_CRAWL_SUBDOMAINS });
 
        const options = advanced.createDiv({ cls: "lilbee-crawl-options" });
 
        const depthLabel = options.createEl("label", { text: MESSAGES.LABEL_DEPTH });
        const depthInput = depthLabel.createEl("input", {
            cls: "lilbee-crawl-depth",
            placeholder: MESSAGES.HINT_CRAWL_BLANK_NO_LIMIT,
            attr: { type: "number", min: "0", title: MESSAGES.TOOLTIP_CRAWL_DEPTH },
        });
        asInput(depthInput).value = "";
 
        const maxLabel = options.createEl("label", { text: MESSAGES.LABEL_MAX_PAGES });
        const maxInput = maxLabel.createEl("input", {
            cls: "lilbee-crawl-max-pages",
            placeholder: MESSAGES.HINT_CRAWL_BLANK_NO_LIMIT,
            attr: { type: "number", min: "1", title: MESSAGES.TOOLTIP_CRAWL_MAX_PAGES },
        });
        asInput(maxInput).value = "";
 
        // Error element lives OUTSIDE the Advanced disclosure so it's visible
        // even when the user collapses Advanced after typing bad input.
        const errorEl = contentEl.createDiv({ cls: "lilbee-crawl-error" });
 
        const syncRecursiveState = (): void => {
            const recursive = asInput(recursiveInput).checked;
            asInput(depthInput).disabled = !recursive;
            asInput(maxInput).disabled = !recursive;
            advanced.style.display = recursive ? "" : "none";
            infoBtn.style.display = recursive ? "" : "none";
            if (!recursive) {
                errorEl.setText("");
                setNoticeOpen(false);
            }
        };
        recursiveInput.addEventListener("change", syncRecursiveState);
        syncRecursiveState();
 
        const actions = contentEl.createDiv({ cls: "lilbee-crawl-actions" });
        const crawlBtn = actions.createEl("button", { text: MESSAGES.BUTTON_CRAWL, cls: "mod-cta" });
        crawlBtn.addEventListener("click", () => {
            const raw = asInput(urlInput).value.trim();
            if (!raw) {
                new Notice(MESSAGES.NOTICE_ENTER_URL);
                return;
            }
            const url = ensureUrlScheme(raw);
 
            const recursive = asInput(recursiveInput).checked;
            let depth: number | null;
            let maxPages: number | null;
 
            if (!recursive) {
                depth = 0;
                maxPages = null;
                errorEl.setText("");
            } else {
                const depthRes = parseOptionalCount(asInput(depthInput).value, { allowZero: true });
                const maxRes = parseOptionalCount(asInput(maxInput).value, { allowZero: false });
                const err = maxRes.error ?? depthRes.error;
                if (err) {
                    errorEl.setText(err);
                    advanced.open = true;
                    return;
                }
                errorEl.setText("");
                depth = depthRes.value;
                maxPages = maxRes.value;
            }
 
            const renderMode: CrawlRenderMode = asInput(browserInput).checked
                ? CRAWL_RENDER_MODE.BROWSER
                : CRAWL_RENDER_MODE.HTTP;
            // Persist the sticky default; non-fatal since runCrawl below drives this crawl explicitly.
            void applyConfig(this.plugin, { [CONFIG_KEY.CRAWL_RENDER_MODE]: renderMode }).catch(() => {});
 
            // Subdomain scope only applies to link discovery, so a single-page crawl omits it.
            const includeSubdomains = recursive ? asInput(subdomainsInput).checked : undefined;
            void this.plugin.runCrawl(url, depth, maxPages, renderMode, includeSubdomains);
            this.close();
        });
 
        const cancelBtn = actions.createEl("button", { text: MESSAGES.BUTTON_CANCEL });
        cancelBtn.addEventListener("click", () => this.close());
    }
}