📜 ⬆️ ⬇️

Opera Unite for web developers

How to create a simple counter as a service Opera Unite?
This will help you understand how to generally write web applications on Opera Unite (also called “services” or “plugins”).

image

15 lines javascript + 7 on XML.
')
Obviously, first download Opera Unite , install it (if Opera is already installed, an update will be made if desired).

Create any folder where you need to create 2 files:

1. Create config.xml:

  <widget>
   <widgetname> Test </ widgetname>
   <feature name = "http://xmlns.opera.com/webserver">
     <param name = "type" value = "service" />
     <param name = "servicepath" value = "test" />
   </ feature>
 </ widget> 


2. Create an index.html file:

 <script>
 var counter = 0;
 window.onload = function () {
     webserver = opera.io.webserver
     if (webserver) {
         webserver.addEventListener ('_ index', start_page, false);
     }
 }
 function start_page (r) {// spaces are not necessary, just habr it replaces with (r)
     counter ++;
     var o = r.connection.response;
     o.write ('This page has been seen <b>' + counter + 'times </ b> !!');
     o.close ();
 }
 </ script>
 <html> <title> My Application </ title> </ html>


This is clearly not the most complete example. You can expand it by taking out the script in a separate file.

* Activate Opera Unite if it is not active (Opera will ask about it in the next step, so you can skip it)

4. Drag and drop config.xml into Opera. She will ask - whether to install? We confirm.

All is ready. We see the picture at the beginning of the post.
(In English - how to make a simple blog in 60 lines of JavaScript )

5. If we change something in index.html - the old version will be shown - debugging solution: Right click on the Unite icon -> “Manage Services” (or F4), right click on “Test” -> “Stop service”, then the same, only "Start service". Sources will reboot. The counter will be reset (persistence problem, I'm still looking for how to solve, most likely something with the fileio library + JSON will be or REST databases).

6. To share your creativity with someone, copy the URL (F8 - Ctrl + C in Opera) and change it like this:

unite: // -> http: //

so that,

unite: //my_compute.my_opera_login.operaunite.com/test/

becomes

http://my_compute.my_opera_login.operaunite.com/test/

7. Use Markuper for HTML templates.

Should I use Unite for web development? I have a lot of different thoughts on this, but I don’t have time to translate them. If the English order - read http://unitehowto.com/Why .

If very briefly:


I will, as far as finding a solution to these problems, publish the answers on unitehowto.com .

How to make the counter not reset



The problem persistence solved (Opera, let's more small examples!)

1. Modify the config.xml:

<widget>
<widgetname>Test</widgetname>
<feature name="http://xmlns.opera.com/fileio">
</feature>

<feature name="http://xmlns.opera.com/webserver">
<param name="type" value="service"/>
<param name="servicepath" value="test"/>
</code>
</widget>


This is what we connect the permission of Opera to use the file system (there it’s all tricky in sandboxes). Now we change the script:

  var counter = 0; 

replace with

 var dir = opera.io.filesystem.mountSystemDirectory ('storage');
 try {
	 stream = dir.open (dir.resolve ('/ storage / newfile'), opera.io.filemode.READ);
	 if (stream) {
		 var counter = parseInt (stream.readLine ());
		 stream.close ();
	 };
 } catch (err) {// message: FILE_NOT_FOUND_ERR
	 var counter = 0;
 };


Opera declares the .exists () method, but I never found it (I tried almost all the options to call it), and dir.open (...) immediately gives an exception if there is no file.

For information on how to access files in another sandbox (not in / storage /) - read the fileio (english) docks.

With these lines we load the variable from the file (/ storage / newfile) - in fact, this file will be stored by the user somewhere in / Documents and settings /..../ Local settings /..../ 4393408934 / (and .), i.e. - this is not an absolute path, but a path inside the sandbox.

In theory, we would need to save the JSON representation of the object, then we can store complex nested structures, and not just Int.

At the end of start_page we add saving:

	 var stream = dir.open ('/ storage / newfile', opera.io.filemode.WRITE);
	 stream.writeLine (counter);
	 stream.close ();


This is not the best option - to save the file after each request, but in my opinion, window.onunload was forgotten in Opera (window.onload was done).

Full source with preservation: t9.zip

Debugging scripts, pitfalls


Remember that this is the alpha release of Opera, so all that has been said is without charge:

Reload source: “Stop service” -> “Start service” -> (check Error Console) -> F5. (Sometimes you also need a reinstallation.)

You can try to find a hint of what happened in Tools -> Advanced -> Error Console, but there are also errors with meaningless names often. [Update] Open the URL opera: config # UserPrefs | Exceptions Have Stacktrace , check this box, do not forget to click the Save button below. This makes errors in the Error Console readable.

Syntax errors are checked by the Error Console immediately after each “Stop service” -> “Start service”.

If you change the config.xml - you have to change the name of the directory where it lies and reinstall the script (by dragging), otherwise the opera will take the old version from memory.

In no case do not use alert () and window.onunload in scripts (killed the opera, sent the bug report).

The “Resource not found” error can mean either a syntax error or anything else.

In general, while that debugging is horror.

But, in general, Opera is giants! Make a web that truly p2p now is power! And those problems that are described here and under the link "/ Why" are described - I think they will fix it.


Yoi Haji
view from Habra

Source: https://habr.com/ru/post/62208/


All Articles