2 Scripting ELinks with ECMAScript
3 --------------------------------
5 As a user of ELinks, you can control its behaviour by writing scripts
6 in ECMAScript. Unlike <<ecmascript,scripts in SCRIPT elements of
7 HTML>>, these user scripts run with all the permissions of your user
8 account, the same as with <<lua-scripting,Lua>>. The object model is
11 Support for ECMAScript user scripts was first added in ELinks 0.11.0.
12 The `configure` script enables it by default if the required SpiderMonkey
13 library has been installed, but you can disable it with `configure
14 \--disable-sm-scripting` or by <<CONFIG-SCRIPTING-SPIDERMONKEY,editing
17 WARNING: ECMAScript scripting is still a bit experimental: there seem to be
18 ways to crash ELinks with it, and the object model may change. However, if
19 you don't have a `hooks.js` file, there is not much risk in enabling the
20 feature at compile time.
22 When ELinks starts up, it evaluates the ECMAScript file `hooks.js` in
23 your ELinks configuration directory (thus normally `~/.elinks/hooks.js`
24 on Unix-like systems), or if the file does not exist there, then in
25 the system-wide ELinks configuration directory (the location depends
26 on how ELinks was built, but `/etc/elinks/hooks.js` is typical).
28 In the ELinks source tree, the `contrib/smjs` directory contains some
29 examples about scripting ELinks with ECMAScript. Please see the
30 `README` file in that directory for details.
33 [[smjs-global-object]]
37 The global object provided to ECMAScript user scripts contains the standard
38 ECMAScript classes, as well as the following:
41 [[smjs-global-methods]]
45 [[smjs-global.do_file]] do_file(path)::
46 Load and evaluate the file with the given path (string). For example:
49 ----------------------------------------------------------------------
50 do_file("/home/me/.elinks/hooks.js");
51 ----------------------------------------------------------------------
53 will reload your hooks file.
55 *Compatibility:* ELinks 0.11.0
59 [[smjs-global-properties]]
60 Global Object Properties
61 ^^^^^^^^^^^^^^^^^^^^^^^^
63 [[smjs-global.elinks]] elinks (elinks)::
64 A reference to the <<smjs-elinks-object,ELinks object>>.
66 *Compatibility:* ELinks 0.11.0
69 [[smjs-elinks-object]]
73 The global <<smjs-global.elinks,'elinks'>> property refers to this object.
76 [[smjs-elinks-methods]]
80 [[smjs-elinks.alert]] elinks.alert(message)::
81 Display the given message (string) in a message box. For example:
84 ----------------------------------------------------------------------
85 elinks.alert("Hello, world!");
86 ----------------------------------------------------------------------
88 will display a friendly greeting.
90 *Compatibility:* ELinks 0.11.0
93 [[smjs-elinks.execute]] elinks.execute(command)::
94 Execute the given command (string) on the current terminal.
98 ----------------------------------------------------------------------
99 var quoted_uri = "'" + elinks.location.replace(/'/g, "'\\''") + "'";
100 elinks.execute("firefox " + quoted_uri);
101 ----------------------------------------------------------------------
103 will run Firefox with the URI of the current document.
105 *Compatibility:* ELinks 0.12pre1
107 WARNING: One must be very careful with 'elinks.execute', because depending
108 on the OS, the command may be subject to interpretation by a command
109 shell language. When constructing the command string, be sure to quote
110 any dubious parts (such as the URI of the current document, as above).
113 [[smjs-elinks.load_uri]] elinks.load_uri(uri, callback)::
114 Load the given URI (string). When the URI completes loading, ELinks
115 calls the given callback (function). The callback is passed the
116 <<smjs-cache_entry-object,cache object>> that corresponds to the URI.
120 ----------------------------------------------------------------------
121 elinks.load_uri("http://www.eldar.org/cgi-bin/fortune.pl?text_format=yes",
122 function (cached) { elinks.alert(cached.content); });
123 ----------------------------------------------------------------------
127 The <<smjs-cache_entry-object,cache object>> will not expire until after the
130 *Compatibility:* ELinks 0.12pre1
134 [[smjs-elinks-properties]]
135 ELinks Object Properties
136 ^^^^^^^^^^^^^^^^^^^^^^^^
138 [[smjs-elinks.home]] elinks.home (string)::
139 ELinks's ``home'' directory, where it stores its configuration files.
140 Read-only. For example,
143 ----------------------------------------------------------------------
144 do_file(elinks.home + "hooks.js");
145 ----------------------------------------------------------------------
147 will reload your hooks file.
149 *Compatibility:* ELinks 0.11.0
152 [[smjs-elinks.location]] elinks.location (string)::
153 The URI of the currently open document. This can be read to get a
154 string with the URI or set to load a different document.
158 ----------------------------------------------------------------------
159 elinks.location = elinks.location + "/..";
160 ----------------------------------------------------------------------
162 will go up a directory (if the URI doesn't end in a file).
164 *Compatibility:* ELinks 0.11.0
167 [[smjs-elinks.bookmarks]] elinks.bookmarks (hash)::
168 This is a hash, the elements of which correspond to the bookmarks.
169 One can delve into the bookmarks hierarchy in a reasonably nifty
170 fashion, just by using standard ECMAScript syntax:
173 ----------------------------------------------------------------------
174 elinks.bookmarks.x.children.y.children.z.children.foo.title
175 ----------------------------------------------------------------------
177 gets the title of the bookmark titled ``foo'' under the folder ``z'',
178 which is a subfolder of ``y'', which is a subfolder of ``x''.
180 *Compatibility:* ELinks 0.11.0
182 [[smjs-bookmark-properties]]
183 A bookmark object has these properties:
185 [[smjs-bookmark.title]] item.title (string)::
186 This is the title of the bookmark. It can be read and set.
188 [[smjs-bookmark.url]] item.url (string)::
189 This is the URI of the bookmark. It can be read and set.
191 [[smjs-bookmark.children]] item.children (hash)::
192 This is a hash, the elements of which are the bookmarks that
193 are children to the item. It is read-only.
196 [[smjs-elinks.globhist]] elinks.globhist (hash)::
197 This is a hash, the elements of which correspond to entries in ELinks's
198 global history. The hash is indexed by URI. For example,
201 ----------------------------------------------------------------------
202 elinks.globhist["file:///"]
203 ----------------------------------------------------------------------
205 will get you the history item for your root directory.
207 *Compatibility:* ELinks 0.12pre1
209 [[smjs-global_history_item-properties]]
210 A history item has these properties:
212 [[smjs-global_history_item.title]] item.title (string)::
213 This is the title of the history item. It can be read and set.
215 [[smjs-global_history_item.url]] item.url (string)::
216 This is the URI of the history item. It can be read and set.
218 [[smjs-global_history_item.last_visit]] item.last_visit (number)::
219 This is the UNIX time of the last visit time for the item. UNIX time
220 is the number of seconds that have passed between the UNIX epoch
221 (which is 1970-01-01 00:00:00 UTC) and the represented time. Note that
222 this is 'seconds' since the epoch, whereas ECMAScript likes to use
223 'milliseconds' since the epoch. This property can be set or read.
226 [[smjs-elinks.action]] elinks.action (hash)::
227 This hash lets you call the built-in actions of ELinks. For example,
228 you can call `elinks.action.auth_manager()` to open the authentication
229 manager. The names of the actions are the same as in elinks.conf or
230 in the keybinding manager, except they have underscores instead of
231 dashes in order to make them valid ECMAScript identifiers.
234 *Compatibility:* ELinks 0.12pre1
236 NOTE: When you read an action function from this hash, ELinks binds it to the
237 current tab; any later calls to the function affect that tab. This may be
238 changed in a future version. It is safest to call the function right away,
239 rather than save it in a variable and call it later.
242 [[smjs-elinks.keymaps]] elinks.keymaps (hash)::
243 This is a hash, the elements of which correspond to ELinks's keymaps.
244 Currently, there are three: 'elinks.keymaps.main', 'elinks.keymaps.edit',
245 and 'elinks.keymaps.menu'. These elements are also hashes, the elements of
246 which correspond to bindings. For example, `elinks.keymaps.main["q"]` is
247 the binding to the ``q'' key in the main map. These bindings can be red,
248 to get the name of the action to which the key is bound, or set, either
249 to a string with the name of the ELinks action or to a function, which will
250 thenceforth be called when the key is pressed. For example,
253 ----------------------------------------------------------------------
254 elinks.keymaps.main["!"] = function () { elinks.alert("Hello!"); }
255 ----------------------------------------------------------------------
257 binds the ``!'' key in the main map to a function that displays a friendly
260 ----------------------------------------------------------------------
261 elinks.keymaps.main["/"] = "search-typeahead-text";
262 ----------------------------------------------------------------------
264 changes the ``/'' key to use the nice typeahead search function instead of
265 opening that ugly old search dialogue box.
267 *Compatibility:* ELinks 0.11.0
269 NOTE: Do not read a function from <<smjs-elinks.action,'elinks.action'>>,
270 e.g. `elinks.action.search_typeahead_text`, and place it in a keymap.
271 ELinks binds such functions to the current tab when the script reads
272 them from 'elinks.action', so they will not work right in other tabs.
273 Use the name of the action instead.
276 [[smjs-elinks.vs]] elinks.vs (view_state)::
277 This property refers to the <<smjs-view_state-object,view-state
278 object>> for the current document, if any.
280 *Compatibility:* ELinks 0.12pre1
283 [[smjs-elinks-hooks]]
287 These are actually properties, but a special case: one assigns functions
288 to them, which functions are called at certain events.
290 In the ELinks source tree, `contrib/smjs/hooks.js` provides a mechanism
291 with which multiple scripts can add their functions to the same hooks.
292 Please see `contrib/smjs/README` for details.
294 [[smjs-elinks.preformat_html]] elinks.preformat_html(cached, vs)::
295 This function is called every time a document is loaded, before the
296 document is actually rendered, to give scripts the opportunity to
297 modify it. The first parameter is the <<smjs-cache_entry-object,cache
298 object>> and the second is the <<smjs-view_state-object,view-state
302 The <<smjs-cache_entry-object,cache object>> will not expire until after this
305 *Compatibility:* ELinks 0.11.1 as described. ELinks 0.11.0 did not provide
309 [[smjs-elinks.goto_url_hook]] elinks.goto_url_hook(url)::
310 This function is called every time the user enters something in the
311 'Go to URL' box. The url (string) can be modified or not, and the
312 returned string is substituted for what the user entered. If the
313 value `false` is returned, the URL is not changed and further hooks
314 in ELinks are not run.
316 *Compatibility:* ELinks 0.11.0
318 [[smjs-elinks.follow_url_hook]] elinks.follow_url_hook(url)::
319 This function is called every time the user tries to load a document,
320 whether by following a link, by entering a URI in the Go to URL box,
321 by setting <<smjs-elinks.location,'elinks.location'>>, or whatever.
322 It behaves the same as <<smjs-elinks.goto_url_hook,'elinks.goto_url_hook'>>
325 *Compatibility:* ELinks 0.11.0
328 [[smjs-cache_entry-object]]
332 The cache object mentioned in the descriptions of
333 <<smjs-elinks.load_uri,'elinks.load_uri'>> and
334 <<smjs-elinks.preformat_html,'elinks.preformat_html'>> is a wrapper for the
335 internal ELinks cache object. ELinks passes the ECMAScript cache object as an
336 argument to your ECMAScript function, and keeps the corresponding document in
337 the cache until the function returns. After that, ELinks may remove the
338 document from the cache, even if the function has saved the cache object to
339 some global variable. Such an expired cache object does not work but it does
340 not crash ELinks either.
342 *Compatibility:* ELinks 0.11.0
345 [[smjs-cache_entry-properties]]
346 Cache Object Properties
347 ^^^^^^^^^^^^^^^^^^^^^^^
349 [[smjs-cache_entry.content]] cached.content (string)::
350 This is the content received from the server. It can be read and set.
352 [[smjs-cache_entry.type]] cached.type (string)::
353 This is the MIME type of the cache entry. It can be read and set.
355 [[smjs-cache_entry.length]] cached.length (number)::
356 This is the length of cached.content. It is read-only.
358 [[smjs-cache_entry.head]] cached.head (string)::
359 This is the header received from the server. It can be read and set.
361 [[smjs-cache_entry.uri]] cached.uri (string)::
362 This is the URI of the cache entry. It is read-only.
365 [[smjs-view_state-object]]
369 The view-state object mentioned in the descriptions of
370 <<smjs-elinks.preformat_html,'elinks.preformat_html'>> and
371 <<smjs-elinks.vs,'elinks.vs'>> is a wrapper for the internal ELinks view_state
372 object. The view state holds information on how the current document is being
375 *Compatibility:* ELinks 0.11.1
378 [[smjs-view_state-properties]]
379 View-state Object Properties
380 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
382 [[smjs.view_state.plain]] vs.plain (boolean)::
383 Whether the current document is rendered as HTML or displayed
384 as plaintext. This can be read and set.
386 [[smjs.view_state.uri]] vs.uri (string)::
387 This is the URI of the current document. It is read-only.