From 1f09c7c2df2ec8c0bf6f4097d0d350029d5b8b93 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 7 Aug 2022 16:23:52 +0200 Subject: [PATCH] [mujs] navigator --- src/ecmascript/mujs.cpp | 2 + src/ecmascript/mujs/meson.build | 2 +- src/ecmascript/mujs/navigator.cpp | 180 ++++++++++++++++++++++++++++++ src/ecmascript/mujs/navigator.h | 10 ++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 src/ecmascript/mujs/navigator.cpp create mode 100644 src/ecmascript/mujs/navigator.h diff --git a/src/ecmascript/mujs.cpp b/src/ecmascript/mujs.cpp index 9819e9ede..72f084a2f 100644 --- a/src/ecmascript/mujs.cpp +++ b/src/ecmascript/mujs.cpp @@ -25,6 +25,7 @@ #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/mujs.h" +#include "ecmascript/mujs/navigator.h" #include "ecmascript/mujs/screen.h" #include "ecmascript/mujs/unibar.h" #include "ecmascript/mujs/window.h" @@ -80,6 +81,7 @@ mujs_get_interpreter(struct ecmascript_interpreter *interpreter) mjs_window_init(interpreter, J); mjs_screen_init(interpreter, J); mjs_unibar_init(interpreter, J); + mjs_navigator_init(interpreter, J); return J; #if 0 diff --git a/src/ecmascript/mujs/meson.build b/src/ecmascript/mujs/meson.build index 6f4b1c2e4..7001e190d 100644 --- a/src/ecmascript/mujs/meson.build +++ b/src/ecmascript/mujs/meson.build @@ -1,3 +1,3 @@ #srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'document.cpp', 'element.cpp', 'form.cpp', 'forms.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp', #'input.cpp', 'localstorage.cpp', 'location.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp') -srcs += files('screen.cpp', 'unibar.cpp', 'window.cpp') +srcs += files('navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp') diff --git a/src/ecmascript/mujs/navigator.cpp b/src/ecmascript/mujs/navigator.cpp new file mode 100644 index 000000000..a6d8dabc1 --- /dev/null +++ b/src/ecmascript/mujs/navigator.cpp @@ -0,0 +1,180 @@ +/* The mujs navigator object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "bfu/dialog.h" +#include "cache/cache.h" +#include "cookies/cookies.h" +#include "dialogs/menu.h" +#include "dialogs/status.h" +#include "document/html/frames.h" +#include "document/document.h" +#include "document/forms.h" +#include "document/view.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/navigator.h" +#include "intl/libintl.h" +#include "main/select.h" +#include "osdep/newwin.h" +#include "osdep/sysname.h" +#include "protocol/http/http.h" +#include "protocol/uri.h" +#include "session/history.h" +#include "session/location.h" +#include "session/session.h" +#include "session/task.h" +#include "terminal/tab.h" +#include "terminal/terminal.h" +#include "util/conv.h" +#include "util/memory.h" +#include "util/string.h" +#include "viewer/text/draw.h" +#include "viewer/text/form.h" +#include "viewer/text/link.h" +#include "viewer/text/vs.h" + + +static void +mjs_navigator_get_property_appCodeName(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "Mozilla"); +} + +static void +mjs_navigator_get_property_appName(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "ELinks (roughly compatible with Netscape Navigator, Mozilla and Microsoft Internet Explorer)"); +} + +static void +mjs_navigator_get_property_appVersion(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, VERSION); +} + +static void +mjs_navigator_get_property_language(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif +#ifdef CONFIG_NLS + if (get_opt_bool("protocol.http.accept_ui_language", NULL)) { + js_pushstring(J, language_to_iso639(current_language)); + return; + } +#endif + js_pushundefined(J); +} + +static void +mjs_navigator_get_property_platform(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, system_name); +} + +static void +mjs_navigator_get_property_userAgent(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + char *optstr; + + 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); + + js_pushstring(J, custr); + return; + } + } + js_pushstring(J, system_name); +} + +static void +mjs_navigator_toString(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "[navigator object]"); +} + +int +mjs_navigator_init(struct ecmascript_interpreter *interpreter, js_State *J) +{ + js_getglobal(J, "Object"); + js_getproperty(J, -1, "prototype"); + js_newuserdata(J, "navigator", interpreter, NULL); + + js_newcfunction(J, mjs_navigator_toString, "navigator.prototype.toString", 0); + js_defproperty(J, -2, "toString", JS_DONTENUM); + + js_newcfunction(J, mjs_navigator_get_property_appCodeName, "navigator.prototype.appCodeName", 0); + js_pushnull(J); + js_defaccessor(J, -3, "appCodeName", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_newcfunction(J, mjs_navigator_get_property_appName, "navigator.prototype.appName", 0); + js_pushnull(J); + js_defaccessor(J, -3, "appName", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_newcfunction(J, mjs_navigator_get_property_appVersion, "navigator.prototype.appVersion", 0); + js_pushnull(J); + js_defaccessor(J, -3, "appVersion", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_newcfunction(J, mjs_navigator_get_property_language, "navigator.prototype.language", 0); + js_pushnull(J); + js_defaccessor(J, -3, "language", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_newcfunction(J, mjs_navigator_get_property_platform, "navigator.prototype.platform", 0); + js_pushnull(J); + js_defaccessor(J, -3, "platform", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_newcfunction(J, mjs_navigator_get_property_userAgent, "navigator.prototype.userAgent", 0); + js_pushnull(J); + js_defaccessor(J, -3, "userAgent", JS_READONLY | JS_DONTENUM | JS_DONTCONF); + + js_defglobal(J, "navigator", JS_DONTENUM); + + return 0; +} diff --git a/src/ecmascript/mujs/navigator.h b/src/ecmascript/mujs/navigator.h new file mode 100644 index 000000000..b04eb639a --- /dev/null +++ b/src/ecmascript/mujs/navigator.h @@ -0,0 +1,10 @@ +#ifndef EL__ECMASCRIPT_MUJS_NAVIGATOR_H +#define EL__ECMASCRIPT_MUJS_NAVIGATOR_H + +#include + +struct ecmascript_interpreter; + +int mjs_navigator_init(struct ecmascript_interpreter *interpreter, js_State *J); + +#endif