KJSEmbed is a library for embedding the KJS Javascript (ECMAScript) interpreter in KDE applications. It provides developers with an easy way to allow users to extend application, and can even be used with applications that have not been designed with this in mind thanks to a KParts plugin. In addition, KJSEmbed provides a command line tool for running scripts so users can easily create simple applications of their own in Javascript. KJSEmbed scripts are surprisingly powerful because they access the properties and slots of QObjects, and can even load widgets and dialogs created with Qt's graphical dialog editor.
To give you an idea of what KJSEmbed provides, here is a brief summary of the more interesting features:
The quickest way to see what KJSEmbed can do is with kjscmd, a tool for running scripts from the command line. To begin, we'll run kjscmd without any parameters which brings up the KJSEmbed console dialog. The console provides an easy way to run short (one line) scripts, as you can see in figure 1 the scripts have full access to the core Javascript language, and to standard objects such as Math. In addition to the standard Javascript operations, this screenshot also demonstrates the global function print() provided by KJSEmbed.
-- Enter a JS expression and press enter --
kjs> 10+20
30
kjs> print("Hello World!")
Hello World!
undefined
kjs> Math.sin(0)
0
kjs> Math.cos(0)
1
Figure 1: The KJSEmbed Console Dialog
Things get more interesting when you realise that we also have access to the widgets that make up the dialog, as you can in figure 2:
kjs> console
JSConsoleWidget (KJSEmbed::JSConsoleWidget)
kjs> console.childCount()
4
kjs> console.child(1)
CmdEdit (QComboBox)
kjs> console.child(2)
RunButton (QPushButton)
kjs> console.child("RunButton")
RunButton (QPushButton)
kjs> console.child("RunButton").text = "Go!"
Go!
kjs> console.caption = "Different Title"
Different Title
kjs> console.child("Title").hide()
undefined
Figure 2: Modifying the Console Dialog
As you can see, the console dialog has been made available to scripts as the
global variable 'console'. We can access the child widgets that make up the
dialog either by index using the childCount() and child() methods, or by name
using the child() method (you can also use getElementById() if you
want a DOM-style API. As well as being able to see the widgets, we can modify
them by setting properties - in this example, we modify the css="img 0000755 0000000 0000000 00000000000 10156202100 020013 5 ustar root root 0000000 0000000 ./usr/share/doc/libkjsembed1/tutorial/passwordfileviewdlg.ui 0000644 0000000 0000000 00000014214 10066064510 024453 0 ustar root root 0000000 0000000