Google has just released some new extension APIs for it’s superb browser chrome. I already use some own experimental extensions to augment my browsing experience. And now I start to test the new possibilities. There will be a lot of new uses. You can now implement own infobars, program the omnibox (how about tweeting directly from the omnibox?) and add customized context menus, that can work selectively on the type of content (for example a page, a link/video/image or a selection) or the current url.

My first experiment with the new APIs was a simple one: Provide a context menu item to search for the selected text on different sites (too bad, this can’t at this time be integrated with the defined/personally configured search engines/sites of chrome). Nonetheless I'd like to share this simple naive implementation with you, to help get you started with chrome extensions.

The whole code contains of two files that you put together in a directory. File one is “manifest.json”, which declares the permissions used by the extension (see documentation):

{
  "name": "LookUp",
  "description": "Look up selected word(s) on different sites",
  "version": "0.9",
  "permissions": ["contextMenus","tabs"],
  "backgroundpage": "background.html"
}

The other file is the “backgroud.html” which gets loaded when the extension is loaded/activated. It only contains the javascript needed to define the sites to search on and the action that should be taken on click (in this case always open a new tab with a search url) and hook these actions to the menu:

<script>
var definitions=[
    {"title":"Wiki (en)","action":function(info,tab){chrome.tabs.create({"url":"http://en.wikipedia.org/w/index.php?title=Special:Search&search="+info.selectionText});}},
    {"title":"Wiki","action":function(info,tab){chrome.tabs.create({"url":"http://de.wikipedia.org/w/index.php?title=Spezial:Suche&search="+info.selectionText});}},
    {"title":"Amazon","action":function(info,tab){chrome.tabs.create({"url":"http://www.amazon.de/s/ref=nbss?__mkdeDE=%C5M%C5Z%D5%D1&url=search-alias%3Daps&tag=stecki&x=0&y=0&field-keywords="+info.selectionText});}}, 
    {"title":"IMDB","action":function(info,tab){chrome.tabs.create({"url":"http://www.imdb.com/find?s=all&q="+info.selectionText});}}
];

for(var i=0;i<definitions.length;i++) { var item=definitions[i]; var id=chrome.contextMenus.create({"title": item.title, "contexts":["selection"],"onclick": item.action}); } </script>

To get such a local extension running, you go to chrome://extensions/, click on ”load unpacked extension…” and select the directory in which you saved above two files. That’s it.



Dieser Text ist mir etwas wert:

Trackbacks


Trackback-URL für diesen Eintrag
    Keine Trackbacks

Kommentare


    #1 bandler am 09/04/10 um 02:21 [Antwort]

    *den google browser finde ich auch sehr gut. mir fehlt hier nur noch eine stand alone version ohne installationsroutinen.

    #2 Stecki am 09/04/10 um 09:18 [Antwort]

    *Mal Chrome portable http://www.heise.de/software/download/portable_chrome/57619 probiert?

Kommentar schreiben

Umschließende Sterne heben ein Wort hervor (*wort*), per _wort_ kann ein Wort unterstrichen werden.
Standard-Text Smilies wie :-) und ;-) werden zu Bildern konvertiert.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA

:'(  :-)  :-|  :-O  :-(  8-)  :-D  :-P  ;-) 
Gravatar, Identica, Favatar, Pavatar, Twitter Autoren-Bilder werden unterstützt.
Markdown-Formatierung erlaubt

  Kommentare werden erst nach redaktioneller Prüfung freigeschaltet!