1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[quikcjs] libdom navigator.c

This commit is contained in:
Witold Filipczyk 2023-03-27 16:44:21 +02:00
parent e3e8c49506
commit c4a747749d
5 changed files with 193 additions and 3 deletions

View File

@ -1,6 +1,6 @@
top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj navigator.o
include $(top_srcdir)/Makefile.lib

View File

@ -1 +1 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp')
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp', 'navigator.c')

View File

@ -0,0 +1,177 @@
/* The Quickjs navigator object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/navigator.h"
#include "intl/libintl.h"
#include "osdep/sysname.h"
#include "protocol/http/http.h"
#include "util/conv.h"
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_navigator_class_id;
/* @navigator_class.getProperty */
static JSValue
js_navigator_get_property_appCodeName(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
JSValue r = JS_NewString(ctx, "Mozilla"); /* More like a constant nowadays. */
RETURN_JS(r);
}
static JSValue
js_navigator_get_property_appName(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
JSValue r = JS_NewString(ctx, "ELinks (roughly compatible with Netscape Navigator, Mozilla and Microsoft Internet Explorer)");
RETURN_JS(r);
}
static JSValue
js_navigator_get_property_appVersion(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
JSValue r = JS_NewString(ctx, VERSION);
RETURN_JS(r);
}
static JSValue
js_navigator_get_property_language(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
#ifdef CONFIG_NLS
if (get_opt_bool("protocol.http.accept_ui_language", NULL)) {
JSValue r = JS_NewString(ctx, language_to_iso639(current_language));
RETURN_JS(r);
}
#endif
return JS_UNDEFINED;
}
static JSValue
js_navigator_get_property_platform(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
JSValue r = JS_NewString(ctx, system_name);
RETURN_JS(r);
}
static JSValue
js_navigator_get_property_userAgent(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
char *optstr = get_opt_str("protocol.http.user_agent", NULL);
if (*optstr && strcmp(optstr, " ")) {
char *ustr, ts[64] = "";
static char custr[256];
/* TODO: Somehow get the terminal in which the
* document is actually being displayed. */
struct terminal *term = get_default_terminal();
if (term) {
unsigned int tslen = 0;
ulongcat(ts, &tslen, term->width, 3, 0);
ts[tslen++] = 'x';
ulongcat(ts, &tslen, term->height, 3, 0);
}
ustr = subst_user_agent(optstr, VERSION_STRING, system_name, ts);
if (ustr) {
safe_strncpy(custr, ustr, 256);
mem_free(ustr);
JSValue r = JS_NewString(ctx, custr);
RETURN_JS(r);
}
}
JSValue rr = JS_NewString(ctx, system_name);
RETURN_JS(rr);
}
static JSValue
js_navigator_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
return JS_NewString(ctx, "[navigator object]");
}
static const JSCFunctionListEntry js_navigator_proto_funcs[] = {
JS_CGETSET_DEF("appCodeName", js_navigator_get_property_appCodeName, NULL),
JS_CGETSET_DEF("appName", js_navigator_get_property_appName, NULL),
JS_CGETSET_DEF("appVersion", js_navigator_get_property_appVersion, NULL),
JS_CGETSET_DEF("language", js_navigator_get_property_language, NULL),
JS_CGETSET_DEF("platform", js_navigator_get_property_platform, NULL),
JS_CGETSET_DEF("userAgent", js_navigator_get_property_userAgent, NULL),
JS_CFUNC_DEF("toString", 0, js_navigator_toString)
};
static JSClassDef js_navigator_class = {
"navigator",
};
int
js_navigator_init(JSContext *ctx)
{
JSValue navigator_proto;
/* create the navigator class */
JS_NewClassID(&js_navigator_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_navigator_class_id, &js_navigator_class);
JSValue global_obj = JS_GetGlobalObject(ctx);
REF_JS(global_obj);
navigator_proto = JS_NewObject(ctx);
REF_JS(navigator_proto);
JS_SetPropertyFunctionList(ctx, navigator_proto, js_navigator_proto_funcs, countof(js_navigator_proto_funcs));
JS_SetClassProto(ctx, js_navigator_class_id, navigator_proto);
JS_SetPropertyStr(ctx, global_obj, "navigator", JS_DupValue(ctx, navigator_proto));
JS_FreeValue(ctx, global_obj);
return 0;
}

View File

@ -42,6 +42,8 @@
#include "viewer/text/link.h"
#include "viewer/text/vs.h"
#ifndef CONFIG_LIBDOM
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_navigator_class_id;
@ -199,3 +201,4 @@ js_navigator_init(JSContext *ctx)
return 0;
}
#endif

View File

@ -2,5 +2,15 @@
#define EL__ECMASCRIPT_QUICKJS_NAVIGATOR_H
#include <quickjs/quickjs.h>
int js_navigator_init(JSContext *ctx);
#ifdef __cplusplus
extern "C" {
#endif
int js_navigator_init(JSContext *ctx);
#ifdef __cplusplus
}
#endif
#endif