De:Ubiquity 0.1.2 Programmier-Tutorial: Difference between revisions

Jump to navigation Jump to search
Line 222: Line 222:


= Weitere Befehle =
= Weitere Befehle =
== map-me! Standort, Schnappschüsse und einfügen von HTML ==
= Drittes Kommando: Map-Me! Standort ermitteln und in einer Karte konservieren  =


Ubiquity's "map" Kommando ist ziemlich mächtig, es ist vergleichsweise kompliziert anzuwenden und besteht zudem noch aus einigen hundert Zeilen Programm-Code. Das Kommando bietet aber dem entsprechend weitergehende Möglichkeiten. So könntest Du einige Häuser aus einer Immobilienangebotsliste auswählen oder einige Restaurants aus einer Liste mit Restaurantnamen und Ubiquity würde daraus Deine "Wunsch-Karte" erzeugen. Das dahinter stehende Konzept legt die ganze Macht der Rekombinationsmöglichkeit von WebInhalten in die Hand des Anwenders. Aber ich schweife hier doch zu sehr ab. Lass uns ein einfaches Kommando bauen, das lediglich Deine aktuelle geografische Position in Form einer Karte an der Cursorposition einfügt.
Ubiquity's "map" Kommando ist ziemlich mächtig, es ist vergleichsweise kompliziert anzuwenden und besteht zudem noch aus einigen hundert Zeilen Programm-Code. Das Kommando bietet aber dem entsprechend weitergehende Möglichkeiten. So könntest Du einige Häuser aus einer Immobilienangebotsliste auswählen oder einige Restaurants aus einer Liste mit Restaurantnamen und Ubiquity würde daraus Deine "Wunsch-Karte" erzeugen. Das dahinter stehende Konzept legt die ganze Macht der Rekombinationsmöglichkeit von WebInhalten in die Hand des Anwenders. Aber ich schweife hier doch zu sehr ab. Lass uns ein einfaches Kommando bauen, das lediglich Deine aktuelle geografische Position in Form einer Karte an der Cursorposition einfügt.
Line 269: Line 269:


There's also a <code>CmdUtils.getWindowSnapshot()</code> function, which allows you to get the image data for any tab/window. The function takes a window as the first paramater, and a callback for the second.
There's also a <code>CmdUtils.getWindowSnapshot()</code> function, which allows you to get the image data for any tab/window. The function takes a window as the first paramater, and a callback for the second.
= Kommandos mit Argumenten =
== Echo ==
We'll be working towards making some fun and useful commands&mdash;commands that let you control the seething tendrils of the internet with your little pinky. But, let's start by making a simple command to echo back whatever you type.
<pre>
CmdUtils.CreateCommand({
  name: "echo",
  takes: {"your shout": noun_arb_text},
  preview: function( pblock, theShout ) {
    pblock.innerHTML = "Will echo: " + theShout.text;
  },
  execute: function( theShout ) {
    var msg = theShout.text + "... " + theShout.text + "......";
    displayMessage( msg );
  }
})
</pre>
This says that the command "echo" takes one argument which is arbitrary text. Whatever text the user enters will get wrapped in an input object and passed into both the preview and execute function.
Ubiquity takes care of parsing the user's input, so you don't need to worry about handling prounoun substitution or any of the other natural-language-like features of the Ubiquity parser. Try selecting some text on a page, and Ubiq "echo this". Ubiquity should now echo the selected text.
=== The Input Object ===
The input object that your execute and preview functions receive has the following attributes:
<pre>
  inputObject.text  // a string of the input in plain text, without formatting
  inputObject.html  // a string of the input in formatted html, including tags
  inputObject.data  // for non-text input types, an arbitrary data object
  inputObject.summary // for very long inputs, an abbreviated display string
</pre>
Our example command only cares about the <code>.text</code> attribute of the input, because it simply wants plain text.  Often, when the user invokes your command by typing a few short words into the input box, <code>.text</code>, <code>.html</code>, and <code>.summary</code> will all have exactly the same value, and <code>.data</code> will be null.  Many, if not most, commands that you write will only care about the text value.  Nevertheless, the other versions of the input data are provided to you in case they differ from <code>.text</code> and in case your command has a use for them.
=== Introduction to Noun Types ===
Notice that we specified the type of argument to expect by passing in an object &mdash; in this case, the predefined <code>noun_arb_text</code> object, which accepts any arbitrary text as a valid argument.  If we had wanted to restrict the inputs that our command could take, we could have used a more specific noun-type object to restrict the scope of the argument: for instance, <code>noun_type_date</code> to accept only dates (like the "check-calendar" command) or <code>noun_type_language</code> to accept only names of languages (like the optional modifiers for the "translate" command).
The benefit of specifying a more restrictive noun-type is that it helps the Ubiquity parser generate better suggestions and auto-completions based on user-input.  For instance, if the user has a date selected, commands that operate specifically on dates are more likely to be apropos than commands that take arbitrary text, so Ubiquity can suggest the date-specific commands first.
There are many types of nouns that a command could conceivably take: people, dates, places, tabs, etc. Many of these noun-types aren't implemented yet, and most of the them currently have a lack-luster implementation. This is one of the areas where Ubiquity could use the greatest help. Noun-types enable creating compelling user experiences, with minimal amounts of code. It also allows for code-reuse across numerous commands.
Once you are familiar with writing commands, you should check out the [http://hg.toolness.com/ubiquity-firefox/file/6caa9d66b3bb/ubiquity/chrome/content/nlparser/en/nountypes.js nountypes.js], which has the implementation for most of the noun-types.  You can see what noun types are already available for your commands to use, what still needs to be written, and where the existing implementations could use improvement &mdash; and then come [[Labs/Ubiquity#Participation|get involved]] to help us improve them.
166

edits

Navigation menu