User:Thehayro/JavaScript Addons

From KDE TechBase
Revision as of 16:29, 10 September 2010 by Thehayro (talk | contribs)


Introduction

This is a tutorial on how to write a JavaScript Addon for Plasma. Since it is possible to write Plasmoids in JavaScript (and other languages), you are able to extend your Plasmoid with Addons. This tutorial goes through basic steps you need to do to get your Addon running.

Setting up

metadata.desktop

A very simple Addon

Our Addon will be a simple JavaScript class that prints out "Hello World" when it gets loaded. Furthermore we will provide one method that prints out an entered text.

First of all we will define a constructor. In this case we name it MyJavaScriptAddon

function MyJavaScriptAddon() {

   print("Hello World");

}

This constructor gets called, when this addon is loaded by another JavaScript instance. In there we will get a console output with "Hello World".

The next step is to provide our Addon a method, which print out our input.

MyJavaScriptAddon.prototype.echo = function(text) {

   print(text);

}

In this part, the actual method is called echo, however having MyJavaScriptAddon.prototype prefixed is important for JavaScript to make sure that echo is a member of this class.

At the end of your Addon it is also important that you "publish" your class for other instances that would like to use it. registerAddon(MyJavaScriptAddon)