KJSEmbed

Introduction

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.

Features

To give you an idea of what KJSEmbed provides, here is a brief summary of the more interesting features:

First Steps - The Console Dialog

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="img0000755000000000000000000000000010156202100020013 5ustar rootroot00000000000000./usr/share/doc/libkjsembed1/tutorial/passwordfileviewdlg.ui0000644000000000000000000001421410066064510024453 0ustar rootroot00000000000000 password_viewer QDialog name password_viewer geometry 0 0 493 357 caption Password File Viewer sizeGripEnabled true margin 11 spacing 6 QLabel name TextLabel1 font 12 1 text Password File Viewer Line name Line1 orientation Horizontal name Spacer2 orientation Horizontal sizeType Expanding sizeHint 20 20 QPushButton name PushButton5 text &Dismiss QListView text User Name clickable true resizeable true text Password clickable true resizeable true text User Id clickable true resizeable true text Group Id clickable true