1 /* The "elinks" object */
9 #include "bfu/msgbox.h"
10 #include "config/home.h"
11 #include "ecmascript/spidermonkey-shared.h"
12 #include "intl/gettext/libintl.h"
13 #include "protocol/uri.h"
14 #include "scripting/scripting.h"
15 #include "scripting/smjs/action_object.h"
16 #include "scripting/smjs/bookmarks.h"
17 #include "scripting/smjs/core.h"
18 #include "scripting/smjs/elinks_object.h"
19 #include "scripting/smjs/global_object.h"
20 #include "scripting/smjs/globhist.h"
21 #include "scripting/smjs/keybinding.h"
22 #include "scripting/smjs/load_uri.h"
23 #include "scripting/smjs/view_state_object.h"
24 #include "session/location.h"
25 #include "session/session.h"
26 #include "session/task.h"
30 elinks_get_home(JSContext
*ctx
, JSObject
*obj
, jsval id
, jsval
*vp
)
32 *vp
= STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx
, elinks_home
));
38 elinks_get_location(JSContext
*ctx
, JSObject
*obj
, jsval id
, jsval
*vp
)
42 if (!smjs_ses
) return JS_FALSE
;
44 uri
= have_location(smjs_ses
) ? cur_loc(smjs_ses
)->vs
.uri
45 : smjs_ses
->loading_uri
;
46 if (!uri
) return JS_FALSE
;
48 *vp
= STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx
, struri(uri
)));
54 elinks_set_location(JSContext
*ctx
, JSObject
*obj
, jsval id
, jsval
*vp
)
59 if (!smjs_ses
) return JS_FALSE
;
61 jsstr
= JS_ValueToString(smjs_ctx
, *vp
);
62 if (!jsstr
) return JS_FALSE
;
64 url
= JS_GetStringBytes(jsstr
);
65 if (!url
) return JS_FALSE
;
67 goto_url(smjs_ses
, url
);
72 /* @elinks_funcs{"alert"} */
74 elinks_alert(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
)
76 unsigned char *string
;
81 string
= jsval_to_string(ctx
, &argv
[0]);
85 info_box(smjs_ses
->tab
->term
, MSGBOX_NO_TEXT_INTL
,
86 N_("User script alert"), ALIGN_LEFT
, string
);
88 undef_to_jsval(ctx
, rval
);
93 /* @elinks_funcs{"execute"} */
95 elinks_execute(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
)
97 unsigned char *string
;
102 string
= jsval_to_string(ctx
, &argv
[0]);
106 exec_on_terminal(smjs_ses
->tab
->term
, string
, "", TERM_EXEC_BG
);
107 undef_to_jsval(ctx
, rval
);
112 static const JSClass elinks_class
= {
115 JS_PropertyStub
, JS_PropertyStub
,
116 JS_PropertyStub
, JS_PropertyStub
,
117 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
120 static const spidermonkeyFunctionSpec elinks_funcs
[] = {
121 { "alert", elinks_alert
, 1 },
122 { "execute", elinks_execute
, 1 },
127 smjs_get_elinks_object(void)
132 assert(smjs_global_object
);
134 jsobj
= spidermonkey_InitClass(smjs_ctx
, smjs_global_object
, NULL
,
135 (JSClass
*) &elinks_class
, NULL
, 0, NULL
,
136 elinks_funcs
, NULL
, NULL
);
137 if (!jsobj
) return NULL
;
139 if (!JS_DefineProperty(smjs_ctx
, jsobj
, "location", JSVAL_NULL
,
140 elinks_get_location
, elinks_set_location
,
141 JSPROP_ENUMERATE
| JSPROP_PERMANENT
))
144 if (!JS_DefineProperty(smjs_ctx
, jsobj
, "home", JSVAL_NULL
,
145 elinks_get_home
, JS_PropertyStub
,
155 smjs_init_elinks_object(void)
157 smjs_elinks_object
= smjs_get_elinks_object(); /* TODO: check NULL */
159 smjs_init_action_interface();
160 smjs_init_bookmarks_interface();
161 smjs_init_globhist_interface();
162 smjs_init_keybinding_interface();
163 smjs_init_load_uri_interface();
164 smjs_init_view_state_interface();
167 /* If elinks.<method> is defined, call it with the given arguments,
168 * store the return value in rval, and return JS_TRUE. Else return JS_FALSE. */
170 smjs_invoke_elinks_object_method(unsigned char *method
, jsval argv
[], int argc
,
174 assert(smjs_elinks_object
);
178 if (JS_FALSE
== JS_GetProperty(smjs_ctx
, smjs_elinks_object
,
182 if (JSVAL_VOID
== *rval
)
185 return JS_CallFunctionValue(smjs_ctx
, smjs_elinks_object
,
186 *rval
, argc
, argv
, rval
);