Major improvements to JavaScript engine integration: - Fixed script concatenation: JavaScript now executes as complete scripts instead of line-by-line, resolving syntax errors with multi-line constructs - Enhanced document.write() integration: Content now injects directly into HTML parsing buffer and displays in browser instead of stderr only - Added comprehensive DOM element methods: All elements now have addEventListener, removeEventListener, and appendChild methods - Implemented document.body fallback: Created functional document.body object when DOM body element not available - Fixed HTML body element creation: Added proper DOM body element creation during HTML_BODY tag processing - Improved HTML environment context passing: JavaScript execution now receives HTML parsing context for proper content injection - Resolved all major JavaScript errors: Fixed "unexpected end of string", "expecting semicolon", "not a function", and "appendChild undefined" errors JavaScript integration is now fully functional with error-free execution of complex scripts including DOM manipulation, event listeners, form handling, and dynamic content generation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
89 lines
2.9 KiB
C
89 lines
2.9 KiB
C
/*
|
|
* w3m JavaScript Integration Header
|
|
*
|
|
* This file provides the main JavaScript integration interface for w3m.
|
|
* It defines the core structures and functions needed to execute JavaScript
|
|
* within w3m's text-based browser environment.
|
|
*/
|
|
|
|
#ifndef W3M_JAVASCRIPT_H
|
|
#define W3M_JAVASCRIPT_H
|
|
|
|
#ifdef USE_JAVASCRIPT
|
|
|
|
#include "quickjs/quickjs.h"
|
|
#include "fm.h"
|
|
|
|
/* Forward declarations */
|
|
struct _Buffer;
|
|
struct Line;
|
|
struct Anchor;
|
|
struct FormList;
|
|
|
|
/* Forward declaration for event system */
|
|
struct W3MEventSystem;
|
|
|
|
/* JavaScript Context Management */
|
|
typedef struct {
|
|
JSRuntime *runtime;
|
|
JSContext *context;
|
|
JSValue global_obj;
|
|
JSValue document_obj;
|
|
JSValue window_obj;
|
|
int memory_limit;
|
|
int execution_timeout;
|
|
struct W3MEventSystem *event_system; /* Event system reference */
|
|
} W3MJSContext;
|
|
|
|
/* JavaScript state per buffer */
|
|
typedef struct {
|
|
W3MJSContext *ctx; /* JavaScript context */
|
|
W3MJSContext *js_ctx; /* Legacy field for compatibility */
|
|
struct W3MEventSystem *event_system; /* Event system */
|
|
struct W3MDocument *dom_document; /* DOM document */
|
|
JSValue *script_objects; /* Array of script element objects */
|
|
int script_count;
|
|
char **pending_scripts; /* Scripts to execute on load */
|
|
int pending_count;
|
|
int scripts_enabled;
|
|
} BufferJSState;
|
|
|
|
/* Core JavaScript Engine Functions */
|
|
W3MJSContext *w3m_js_create_context(void);
|
|
void w3m_js_destroy_context(W3MJSContext *ctx);
|
|
int w3m_js_execute_script(W3MJSContext *ctx, const char *script, const char *filename);
|
|
void w3m_js_set_timeout(W3MJSContext *ctx, int timeout_ms);
|
|
void w3m_js_set_memory_limit(W3MJSContext *ctx, int limit_bytes);
|
|
|
|
/* Buffer JavaScript Integration */
|
|
BufferJSState *w3m_js_init_buffer_state(Buffer *buf);
|
|
void w3m_js_cleanup_buffer_state(BufferJSState *state);
|
|
void w3m_js_add_pending_script(BufferJSState *state, const char *script);
|
|
void w3m_js_execute_pending_scripts(BufferJSState *state, void *html_env);
|
|
|
|
/* DOM Integration */
|
|
void w3m_js_bind_dom_objects(W3MJSContext *ctx, Buffer *buf);
|
|
JSValue w3m_js_create_element_object(W3MJSContext *ctx, struct Line *line, int pos);
|
|
void w3m_js_update_buffer_from_dom(W3MJSContext *ctx, Buffer *buf);
|
|
|
|
/* Event System */
|
|
void w3m_js_fire_event(W3MJSContext *ctx, const char *event_type, JSValue target);
|
|
int w3m_js_has_event_listener(W3MJSContext *ctx, JSValue target, const char *event_type);
|
|
|
|
/* Form Integration */
|
|
void w3m_js_bind_form_events(W3MJSContext *ctx, struct FormList *form);
|
|
int w3m_js_validate_form(W3MJSContext *ctx, struct FormList *form);
|
|
|
|
/* Configuration */
|
|
extern int w3m_js_enabled;
|
|
extern int w3m_js_timeout;
|
|
extern int w3m_js_memory_limit;
|
|
extern int w3m_js_network_enabled;
|
|
|
|
/* Utility Functions */
|
|
char *w3m_js_value_to_string(W3MJSContext *ctx, JSValue val);
|
|
void w3m_js_report_error(W3MJSContext *ctx, const char *msg);
|
|
|
|
#endif /* USE_JAVASCRIPT */
|
|
|
|
#endif /* W3M_JAVASCRIPT_H */ |