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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 28x 28x 28x 13x 13x 4x 9x 3x 6x 647x 2x 2x 47x 47x 47x 47x 47x 47x 47x 47x 2x 2x 2x 2x 1x 1x 1x 47x 47x 2x 47x 4x 4x 4x 43x 41x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 53x 53x 2x 2x 51x 51x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 188x 188x 64x 47x 47x 68x 68x 68x 68x 68x 68x 50x 50x 47x 47x 47x 47x 47x 24x 24x 1x 1x 23x 23x 23x 9x 9x 9x 14x 14x 14x 14x 7x 7x 7x 7x 7x 7x 16x 16x 1x 1x 1x 15x 24x 24x 24x 24x 47x 47x | 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 CrawlLogin, type CrawlRenderMode } from "../types";
import { applyConfig } from "../utils/reindex";
import { buildCrawlLogin } from "../utils/crawl-login";
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;
/** The server added this setting in the same commit as the crawl login, so a server that reports it accepts the `login` object. */
const SHIPPED_WITH_LOGIN_KEY = "crawl_respect_robots_txt";
/** The login boxes; secrets are masked and nothing here is written to settings. */
const LOGIN_FIELDS = [
{
key: "authorization",
cls: "lilbee-crawl-login-authorization",
label: MESSAGES.LABEL_CRAWL_LOGIN_AUTHORIZATION,
secret: true,
},
{ key: "username", cls: "lilbee-crawl-login-username", label: MESSAGES.LABEL_CRAWL_LOGIN_USERNAME, secret: false },
{ key: "password", cls: "lilbee-crawl-login-password", label: MESSAGES.LABEL_CRAWL_LOGIN_PASSWORD, secret: true },
{ key: "cookie", cls: "lilbee-crawl-login-cookie", label: MESSAGES.LABEL_CRAWL_LOGIN_COOKIE, secret: true },
] as const;
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;
}
}
/** An older server ignores the `login` object, so the boxes wait for a server that reports the setting. */
private async revealLoginWhenSupported(loginBox: HTMLElement): Promise<void> {
const cfg = await this.plugin.api.config().catch(() => null);
if (cfg !== null && cfg[SHIPPED_WITH_LOGIN_KEY] !== undefined) loginBox.show();
}
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 = "";
const loginBox = contentEl.createEl("details", { cls: "lilbee-crawl-login" });
loginBox.hide();
void this.revealLoginWhenSupported(loginBox);
loginBox.createEl("summary", { text: MESSAGES.LABEL_CRAWL_LOGIN });
loginBox.createDiv({ cls: "lilbee-crawl-login-note", text: MESSAGES.NOTE_CRAWL_LOGIN });
const loginInputs = new Map<keyof CrawlLogin, HTMLElement>();
for (const field of LOGIN_FIELDS) {
const label = loginBox.createEl("label", { text: field.label });
loginInputs.set(
field.key,
label.createEl("input", {
cls: field.cls,
attr: { type: field.secret ? "password" : "text", autocomplete: "off" },
}),
);
}
const loginValue = (key: keyof CrawlLogin): string => asInput(loginInputs.get(key)!).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 loginResult = buildCrawlLogin({
authorization: loginValue("authorization"),
username: loginValue("username"),
password: loginValue("password"),
cookie: loginValue("cookie"),
});
if (loginResult.error) {
errorEl.setText(loginResult.error);
loginBox.open = true;
return;
}
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,
loginResult.login ?? undefined,
);
this.close();
});
const cancelBtn = actions.createEl("button", { text: MESSAGES.BUTTON_CANCEL });
cancelBtn.addEventListener("click", () => this.close());
}
}
|