Files
browser/examples/c/tools.c
T
Adrià Arrufat 11f38b9787 c_api: add lp_last_error functions
Expose `lp_last_error` and `lp_browser_last_error` in the C API to
retrieve the error name of the most recent failing call.
2026-08-03 09:24:58 +02:00

61 lines
2.1 KiB
C

/* Drive the browser through the tool surface: navigate, then extract the
* page title and links via a selector schema.
*
* Build from the repo root after `make lib`, with the link line
* documented in include/lightpanda.h. Run:
* ./tools https://example.com
*/
#include <lightpanda.h>
#include <stdio.h>
#include <string.h>
static lp_status call(lp_session *session, const char *tool, const char *args) {
lp_result result = {0};
lp_status status = lp_call(session, tool, strlen(tool), args, args ? strlen(args) : 0, &result);
if (status != LP_OK) {
size_t err_len = 0;
const char *err = lp_last_error(session, &err_len);
fprintf(stderr, "%s failed: %d (%.*s)\n", tool, status, (int)err_len, err ? err : "");
return status;
}
printf("--- %s%s ---\n%.*s\n", tool, result.is_error ? " (page error)" : "", (int)result.len, result.text);
return LP_OK;
}
int main(int argc, char **argv) {
const char *url = argc > 1 ? argv[1] : "https://example.com";
lp_browser *browser = NULL;
if (lp_init(NULL, &browser) != LP_OK) {
fprintf(stderr, "lp_init failed\n");
return 1;
}
lp_session *session = NULL;
if (lp_session_new(browser, &session) != LP_OK) {
fprintf(stderr, "lp_session_new failed\n");
lp_shutdown(browser);
return 1;
}
char args[1024];
snprintf(args, sizeof args, "{\"url\":\"%s\"}", url);
lp_status status = call(session, "goto", args);
/* extract's "schema" argument is a string holding a JSON object literal
* that maps output fields to CSS-selector specs (not a JSON Schema). */
if (status == LP_OK)
status = call(session, "extract",
"{\"schema\":"
"\"{\\\"title\\\": \\\"title\\\","
" \\\"links\\\": [{\\\"selector\\\": \\\"a\\\", \\\"attr\\\": \\\"href\\\"}]}\""
"}");
if (status == LP_OK)
status = call(session, "getUrl", NULL);
lp_session_close(session);
lp_shutdown(browser);
return status == LP_OK ? 0 : 1;
}