Tales of a Developer Advocate

Developer Relations @ Google

  • Chrome Extension "Post to Buzz": The basics

    • 30 Nov 2010
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    In this quick tutorial, we are going to start to build the super simple Buzz This Chrome Extension.

    At the end of each tutorial, you will have something that works.

    In this tutorial, we are going to create the basic manifest,

    First thing to do is to create a folder where you are going to store your extension. I am not going to tell you how to do that, you should know.

    Inside this folder, create a file called manifest.json. This manifest is the true heart of your extension. Without it, you have nothing that Chrome can load and work with. The manifest simply defines some basic information about your extension including it’s name, description and version number.

    In the manifest add the following:

    {
      "name": "Buzz This",
      "version" : "0.0.0.1"
    }

    Thats it. Honest. If you visit chrome://extensions, click “Developer Mode” and click “Load unpacked extension…” and select the folder with your manifest in, you will see that it loads….. Although it doesn’t do anything.

    Lets add a little something, so at least I can show you something cool (ish).

    This extension wouldn’t be much without a button that was alway visible to the user so that they can click on it to post the current page to Buzz. So lets do that.

    Lets go back to the manifest.json, and add a section called “browser_action” as follows:

    {
        "name": "Buzz This",
        "version" : "0.0.0.1",
        "browser_action": {
            "default_icon" : "buzz-little.png",
            "default_title" : "Post to Google Buzz"
         }
    }

    This is pretty simple, and self explanatory. We are just giving the browser_action a tooltip title and a nice icon (19x19px); note, the icon must be included in the directory of the manifest.

    Visit chrome://extensions, find your extension and click re-load. Boom! You now have a nice little icon in the top right corner of the browser. Click on it and see what happens! Nothing? Thats correct, no code is hooked up yet.

    Lets make it do something when the user clicks on it.

    We need to do two things now. First we need to create a “background page”; the background page allows you to add logic to your extension and is considered the “brains” of your extension. We also need to link to the background page from the manifest.json file.

    Add a file named what ever you want to your extension directory, and then modify the manifest so it looks like:

    {
        "name": "Buzz This",
        "version" : "0.0.0.1",
        "browser_action": {
            "default_icon" : "buzz-little.png",
            "default_title" : "Post to Google Buzz" 
         },
         "background_page": "background.html"
    }

    Now lets add some code to the background.html page, so that it actually does something.

    <html>
        <head>
            <script>
              chrome.browserAction.onClicked.addListener(function (t) {
                  alert(t.url);
              });
            </script>
        </head>
    </html>

    This is great, we now have user interaction. If you reload your extension and click it, it tells you the url that the user is currently on. But still it doesn’t do anything that useful.

    Now lets make it useful. We are going to use the API exposed by the Buzz system, to allow you create html widgets and hack it a little so that we can open a new tab to allow the user of the extension to post to Buzz. However, to open a new tab you need to use the chrome.tabs API and this requires special permission, so the first thing to do is ask for it in the manifest.

    {
        "name": "Buzz This",
        "version" : "0.0.0.1",
        "browser_action": {
            "default_icon" : "buzz-little.png",
            "default_title" : "Post to Google Buzz" 
         },
         "background_page": "background.html",
         "permissions": ["tabs"]
    }

    The Chrome Extension sub-system has a robust security model that requires you to declare upfront the systems (such as tabs) that your extension will need access to. This is designed to allow the user to understand the implications of installing your extension and to stop extensions from accessing information without the users consent. Luckily the whole process is as simple as specifing the permissions you require in the “permissions” array.

    Now that you have access to the “tabs” privilege, we can now open tabs, so lets modify our background page.

    <html>
        <head>
            <script>
              chrome.browserAction.onClicked.addListener(function (t) {
                  chrome.tabs.create(
                      {"url" : "http://www.google.com/buzz/post?url=" + encodeURI(t.url) });
              });
            </script>
        </head>
    </html>

    That’s pretty cool, one line of extra code to call the chrome.tabs.create API and we have our first version of the extension working.

    In the next tutorial I will add in the ability to get the stats for the number of buzzes for the current URL. This will show you how to do Cross domain request and also show you how to interact with the browser_action.

    • Tweet
  • Chrome Extension Example: Post to buzz Post 1

    • 30 Nov 2010
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    The other day I post a simple Chrome extension that allows you to post the current page to Google Buzz, but also allows you to see how popular the current page is on Buzz.

    Over the next couple of posts, I will use this app to show you how to build a Chrome Extension and how easy it is to add some awesome features (and also show you how cool Google Buzz is).

    It is pretty neat, and pretty simple to create. It uses the following Chrome Extension features:

    1. Browser Actions, to let you “Buzz This”,
    2. Tabs API, to determine the current page you are on,
    3. Cross Domain Requests to allow you to get the number of “Buzzes” that have been made about the current page,
    4. Context Menu API to allow you to post images and links to Buzz <– my favourite feature.

    To get started, the extension is here: https://chrome.google.com/extensions/detail/dnkpofojlncaepnglinmdjkfolgabldj If you want to dive straight in to the code it is here: http://github.com/PaulKinlan/BuzzThis

    • Tweet
  • Array.prototype.splice and a schoolboy error.

    • 30 Nov 2010
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    The other day I posted about there being no “delete an arbitrary element” method on Array in Javascrip. The problem being that I tried the solution I am about to present, but made the biggest School Boy Error possible – I didn’t read the documentation correctly!

    Whilst I maintain that my solution removes the need to first find the elements, and then delete them (which is better ;). It must be noted that Array.prototype.splice allows you to remove arbitrary elements if you know the index and the number of elements you want to remove.

    Anyway, here goes, to remove 1 element from an Array from an arbitrary position:

    var values = ["Ah", "hello", "world"];
    var result = values.splice(1,1);
    console.log(values);
    console.log(result);

    This removes the “hello” from the values array in place and returns the elements removed. The result is values = [“Ah”, “world”] and result = [“hello”]

    Thanks to @dezfowler

    • Tweet
  • JS: quickly removing an arbitrary element from an Array

    • 28 Nov 2010
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    Unless I am mistaken (and I could be), Arrays in Javascript have no direct way to delete an arbitrary element. I had a little problem where I knew the index of the item I want to remove, there was no easy way to do this. I can’t just say “array.remove(5)”.

    A way that I have seen some people use get around this problem is to use Array.prototype.join to push the contents into a string, then do a string replace on an element value and then String.split to produce the array with the element removed. It is a bit of a hack. Heck, it is a lot of a hack!

    If your browser supports parts of ECMA Script 5, then Array object now has some extra methods for you to use, including filter(). Filter allows you to specify a function that is called for every item in the list, and for that value you can say if you want it in the result set. So it is ideal to solve the problem of removing an element from the list if you know it’s value.

    var valToRemove = "hello";
    var values = ["Ah", "hello", "world"];
    values.filter(function(i) {
      if(i != valToRemove) return true;
      else return false;
      });

    Pretty easy, and quick given a small array. Obviously, this little piece of code will remove every element from the list with the value I am looking for – but this is ok for my code.

    Still, there isn’t an easy way to remove the nth element from a list unless you shift or pop your way through the array. But that is another story.

    • Tweet
  • Thoughts about the GDD's

    • 22 Nov 2010
    • 5 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    Last week I arrived back from the European Tour for the Google Developer Days.  We had 3 major events in about 10 days.  We started in Munich, moved to Moscow and finally visited Prauge in the Czech Republic. The whole trip was pretty damn amazing.

    I had two talks at each event, (apart from in Munich where we had an awesome local Chrome engineer give the talk - thanks Jochen).  The first talk was a Chrome Extension talk shared with @Mahemoff and was about developing Chrome extensions.

    The slides for the Chrome Extensions talk can be found here: http://gdd-2010.appspot.com/Extension.crx it all runs as a Chrome Extension, so you can quickly see the power of what can be achieved inside a Chrome extension by directly showing you the API's in action.  It must be noted, that this extension needs to run in experimental mode, which in the latest dev channel can be enabled by visiting about://flags.  I will put the code on Github soon.

    The second talk at each event was about building great Web Apps.  I really enjoyed this talk, because it is a logical extension to the pure HTML5 talks that present the technology, and I presented some ideas for building great experiences for your users and how you can use the latest HTML5 API's and CSS3 attributes.

    The slides for the building great Web Apps can be found here: http://gdd-2010.appspot.com/WebStore/index.html again, I will put the code for these online soon so that everyone can see how we built them.

    I met a lot of really enthusiastic developers over the course of the trip, most of which are building really great applications on the Web.  I met developers building protein sequencers, automatic app builders, games and much more.  It is amazing the amount of innovation that I saw directly from the developers that we don't get to see highlighted in the Tech Blogs.  I did find that a lot of developers don't travel with Business cards, I would really like to reach out to you, but I can't if don't know your email address :)

    I have also attached some awesome photos of my travels and the events, so I hope you enjoy them.  Each of the cities were amazing, I managed to spend quite a bit of time in Prague and I loved the city.

    (download)
    Click here to download:
    Thoughts_about_the_GDDs.zip (14.07 MB)

    • Tweet
  • About

    I help developers build really cool products on the Web.

    I work for Google as a Developer Advocate in London, specializing in Chrome, HTML5 and the Chrome Web Store.

    231036 Views
  • Archive

    • 2012 (5)
      • February (5)
    • 2011 (30)
      • July (1)
      • June (4)
      • May (10)
      • April (4)
      • March (1)
      • February (5)
      • January (5)
    • 2010 (35)
      • December (15)
      • November (5)
      • October (2)
      • August (5)
      • July (8)
    • 2009 (1)
      • January (1)
    • 2008 (8)
      • December (1)
      • April (2)
      • March (3)
      • February (2)
    • 2007 (17)
      • December (1)
      • September (1)
      • August (5)
      • May (1)
      • March (2)
      • February (4)
      • January (3)
    • 2006 (153)
      • November (3)
      • October (7)
      • September (11)
      • August (9)
      • July (9)
      • June (14)
      • May (11)
      • April (32)
      • March (23)
      • February (26)
      • January (8)
    • 2005 (274)
      • December (7)
      • November (43)
      • October (63)
      • September (54)
      • August (66)
      • July (13)
      • June (10)
      • May (10)
      • April (6)
      • January (2)
    • 2004 (1)
      • August (1)

    Get Updates

    Subscribe via RSS
    TwitterFriendfeedLinkedIn