166
edits
Line 661: | Line 661: | ||
</pre> | </pre> | ||
== | == Implentierung asynchroner Substantiv-Vorschläge == | ||
Alle Substantiv-Typen, die wir bisher gesehen haben, arbeiteten bei der Rückgabe ihrer Vorschläge synchron. Ubiquity unterstützt aber auch asynchrone Substantiv-Vorschläge. Das ist nützlich in Fällen, in denen ein Substantiv-Typ zuerst eine zeitintensiive Arbeit durchführen muss, bevor er die Vorschlagsliste ausgeben kann, meisten dann, wenn er einen externen Dienst konultieren muss. | Alle Substantiv-Typen, die wir bisher gesehen haben, arbeiteten bei der Rückgabe ihrer Vorschläge synchron. Ubiquity unterstützt aber auch asynchrone Substantiv-Vorschläge. Das ist nützlich in Fällen, in denen ein Substantiv-Typ zuerst eine zeitintensiive Arbeit durchführen muss, bevor er die Vorschlagsliste ausgeben kann, meisten dann, wenn er einen externen Dienst konultieren muss. | ||
Line 712: | Line 712: | ||
*Weil der Aufwand für die Erzeugung asynchroner Vorschläge relativ hoch ist und die <code>suggest</code> - Funktion eines Substantiv-Typs bei jedem Tastendruck des Anwenders aufgerufen wird, sollte man eine Zeitverzögerung für den Aufruf implementieren und/oder die bereits ermittelten Vorschläge für weitere Aufrufe cachen. Ubiquity überlässt das zur Zeit jedem Substantiv-Typ selbst. | *Weil der Aufwand für die Erzeugung asynchroner Vorschläge relativ hoch ist und die <code>suggest</code> - Funktion eines Substantiv-Typs bei jedem Tastendruck des Anwenders aufgerufen wird, sollte man eine Zeitverzögerung für den Aufruf implementieren und/oder die bereits ermittelten Vorschläge für weitere Aufrufe cachen. Ubiquity überlässt das zur Zeit jedem Substantiv-Typ selbst. | ||
* Sieh Dir auch einmal diese wesentlich [http://graynorton.com/ubiquity/freebase-nouns.html robustere Implementierung eines Freebase-abgeleiteten Substantiv-Typs] an. | * Sieh Dir auch einmal diese wesentlich [http://graynorton.com/ubiquity/freebase-nouns.html robustere Implementierung eines Freebase-abgeleiteten Substantiv-Typs] an. | ||
== Running on page load and startup == | |||
In order to run some code on page load, you simply have to prefix your function with <code>pageLoad_</code>. For example, if you want to say "Hi" every time a page is loaded, your code would look like this: | |||
<pre> | |||
function pageLoad_hi(){ | |||
displayMessage("hi"); | |||
} | |||
</pre> | |||
If you modify the function and want to see the changes, remember to first invoke Ubiquity. Although your function like above, might not be a Ubiquity command, this is necessary to refresh the cached code. | |||
Similarly, if you want to run some code everytime Firefox starts up, you just have to prefix the function with <code>startup_</code> . | |||
The awesome thing about these functions is the ability to develop whole Firefox extensions (that require minimal UI) as Ubiquity plugins in lesser lines of code. You don't need to worry about chrome.manifest or install.rdf. Another added benefit is that you never have to restart your Firefox during development unless of course, you are running code on Firefox startup. | |||
<center>http://img388.imageshack.us/img388/3086/picture5eo9.png</center> | |||
Here's the code for [http://foyrek.com/commands/keyscape.js Keyscape] which is a Ubiquity command that makes use of <code>pageLoad</code> and <code>startup</code> to recreate the functionality of the [https://addons.mozilla.org/en-US/firefox/addon/339 Search Keys extension] by Jesse Ruderman. In line with Ubiquity's aim to let you do things quicker using your keyboard, this command lets you go to search results on Google by just pressing a number. It'll add hints to show the number of each link. | |||
<pre> | |||
//A lot of this code is borrowed from the Search Keys extension | |||
//Many thanks to Jeese Ruderman | |||
function startup_keyscape() { | |||
window.addEventListener("keydown", keyscape_down, true); | |||
} | |||
function pageLoad_keyscape(doc){ | |||
var uri = Utils.url(doc.documentURI); | |||
//If we are on about: or chrome://, just return | |||
if(uri.scheme != "http") | |||
return; | |||
//Check if the page we are on is google | |||
if( keyscape_isGoogle(uri) ){ | |||
for(var num=1; num<=10; num++){ | |||
var link = jQuery(doc.body).find('a.l')[num-1]; | |||
if( link ){ | |||
var hint = doc.createElementNS("http://www.w3.org/1999/xhtml", "span"); | |||
hint.style.color = "blue"; | |||
hint.style.background = "white"; | |||
hint.style.padding = "1px 2px 1px 2px"; | |||
hint.style.marginLeft = ".5em"; | |||
hint.appendChild(doc.createTextNode(num == 10 ? 0 : num)); | |||
link.parentNode.insertBefore(hint, link.nextSibling); | |||
} | |||
} | |||
} | |||
} | |||
function keyscape_isGoogle(uri){ | |||
return uri.host.indexOf("google") != -1 | |||
&& (uri.path.substr(0,8) == "/search?" | |||
|| uri.path.substr(0,8) == "/custom?"); | |||
} | |||
function keyscape_down(event){ | |||
var doc = Application.activeWindow.activeTab.document; | |||
var uri = Utils.url(doc.documentURI); | |||
if( keyscape_isGoogle(uri) ){ | |||
var key = parseInt(event.keyCode || event.charCode); | |||
var num; | |||
if(48 <= key && key <= 57) //number keys | |||
num = key - 48; | |||
else if(96 <= key && key <= 105) //numeric keypad with numlock on | |||
num = key - 96; | |||
else | |||
return; | |||
//Don't do anything if we are in a textbox | |||
//or some other related elements | |||
var elt = window.document.commandDispatcher.focusedElement; | |||
if (elt) { | |||
var ln = new String(elt.localName).toLowerCase(); | |||
if (ln == "input" || ln == "textarea" || ln == "select" || ln == "isindex") | |||
return; | |||
} | |||
//Get the link url from the search results page | |||
var url_dest = jQuery(doc.body).find('a.l').eq(num-1).attr('href'); | |||
if(event.altKey){ | |||
//Open in new tab | |||
Application.activeWindow.open(Utils.url(url_dest)); | |||
}else{ | |||
//Open in same tab | |||
doc.location.href = url_dest; | |||
} | |||
} | |||
} | |||
</pre> | |||
If Ubiquity does indeed become ubiquitous, a lot of extensions can be re-written as Ubiquity commands. This is much nicer for the end-user, as well, because the Ubiquity command installation process is a lot easier. | |||
In the future, Ubiquity is also likely to have the ability to convert your Ubiquity commands into proper Firefox extensions. Look [http://labs.toolness.com/trac/ticket/3 here] to check on the progress of this functionality. |
edits