How to : build a simple chrome extension [tutorial]

Intro :

Chrome extensions are a way of enhancing a users behavior, by extending the Google Chrome browser.
There are many things that can be achieved with chrome extensions. You can either have your own html view, get access to users bookmarks, new tab page or history,
You can inject code into websites and add functionality to enhance a users experience with that site.
Chrome Extensions are based on javascript, and are very accessible to front end developers, who have JS experience.

In this tutorial we will create a simple chrome extension, from the enhancing variety.
We will learn how to add a button on a wordpress.org plugin page, and use information from that page for our functionality.

Diving in :

First of all we need to decide what to enhance.
Wordpress.org has a plugin repository, and it allows you to download a zip archive of a plugin, for later uploading to your blog.
This is sometimes uncomfortable, and they later introduced a way of installing plugins right from the admin panel of your blog.
However, every time I’ve Googled for a specific plugin, I’ve ended up copying it’s name from wordpress.org, login to my admin panel and search that plugin from there.

Our extension can help us automate this proccess.
We will learn how to add a quick “Install” button for plugins on WordPress.org page.
Clicking that button will ask the user to enter his blog url, and then open a popup with the install page for that plugin.

Extensionizr to the rescue

Chrome extensions are structured exactly like a regular website, with 1 exception.
We need to specify a manifest.json file with all the options & settings of our extension (as mentioned before, an extension can do a lot of things, some of them requires permission from the user, those permissions are definable in the manifest file)
As this can get tedious, I’ve automated this process a bit with extensionizr.com.

Getting started :

Go to http://extensionizr.com, and select “Hidden”.

Extensionizr.com
Our extension won’t have any views, it will only enhance the wordpress site.

Next check “No background”, as we don’t need a background process for our extension.
A background process lets you run a javascript proccess, in the background, while the user surfs the web.
But for our purpose we only need to run code when the user goes to wordpress.org/extend/plugins/

Check both inject.css and inject.js. Those are the files that will be injected into the url we choose (wordpress.org/extend/plugins) every time a user visits them, and will run in the page’s context.
*One thing to note, an extension won’t have access to scripts already running on that page, according to Chrome’s security policy.

Click “Download it!”, and you should receive a zip file with a basic structure of a chrome extension and a manifest.json file in place.

Injection time :

Edit the manifest.json file, and specify the url our scripts will execute in.
We define a new context script section, add a js and a css file, and tell chrome to inject those files every time the user opens wordpress.org/extend/plugins/

Next we need to find out where we in the page want to inject our button.
I figured the best place for it was above the “download” button that’s already on the page.
Insert our button here
Wordpress.org marks the download button elements with a[itemprop="downloadUrl"].
In inject.js we add the following code to query the download button by the itemprop attribute.

Now that we have our download button element, we can extract the plugin name from it’s href attribute.
Because all of wordpress plugin urls are similarly structured, we can extract the plugin name from the url with ease.

Next we prepare the element to be inserted and add a click event handler to it, then insert it near the previously found download button.

Handling clicks :

We need to add code to handle the clicks on the install button so we can open the users blog admin panel with this plugins install page.
For that we need to ask the user for his blog url, and then save it to localStorage for a better experience,
so the user won’t have to enter the same url over and over next time.

Some additional styling

Our button will already receive the same style as the download button, but we want to make it look different.
In the inject.css add the following color to the button :

And that’s it, now all we have to do is test out our extension.
In chrome, navigate to about:extensions, and enable the “developer mode”.
This will show 2 additional buttons in the UI, “Load unpacked extension” and “Pack Extension”

While you develop a chrome extension, you have the option of loading a directory on your computer, work on files, and chrome will address that directory as the extensions code.
This means you can change your code, reload the extension and see the changes immediately.
When you want to send your extension to someone, chrome will pack it in a CRX file, which is a special archive for installing extensions.

We click on “Load unpacked extension”, and choose our directory and Viola.
End result
Our extension will execute code each time the user will surf to http://wordpress.org/extend/plugins and chooses a plugin
And add an “install” button that will prompt him to enter his blogs URL, and then conveniently open an install page for that specific plugin.
The extension can be found in Here and the code is on Github

Save URL to Pocket with Alfred workflow using oAuth

I’ve finished another workflow, and this one is a bit special. 

Post links to Pocket directly from Alfred. You can download it here and the code is on github

Usage :

First login with pocket using “pocket_login” command, this will take you to the pocket site to atuhenticate.

Screen Shot 2013-03-29 at 8.50.43 PM

Then use “pocket” keyword, to post urls from Chrome/Safari/Clipboard straight to pocket.

Screen Shot 2013-03-29 at 9.09.25 PM

There were already some nice pocket workflows, but they all required either the official Pocket.app for mac or another form of username/password authentication.

I didn’t really wanted to store my Pocket passwords, so I created this workflow using Pocket API and oAuth.

Doing oAuth with Alfred is a bit tricky. But manageable.

The way I found that works is as follows :

  • When user writes “pocket_login”, a ruby script runs via script filter, which requests the initial token from pocket.
  • After the token was returned, it’s saved to a file and the script returns an option to the user to login
  • Then 2 things happen
  • Alfred opens the Pocket oAuth url in the users default browser, with the “success_url” of localhost:2222
  • Another ruby script runs, which opens a server that listens on port 2222 on the localhost
  • When user has completed the auth flow in Pocket site, he then is redirected to the ruby server
  • When the server detects a connection, it completes the last step of the flow, which is converting the token to access_token with which we can call any Pocket API methods securely. And then saves that access_token to a file

Now I know this may seem a little hacky, but it works perfectly, and the server even has a timeout, so it won’t run forever and to minimize security risks.

The code was written in ruby, as I found out that it’s the simplest language to allow me to run a webserver from the command line without additional gems/packages what have you (php didn’t work for some reason :/).

And I must admit, the code is quite messy, because I’m a ruby noob and I was learning as I go.

Controlling pandora.com from Alfred 2 and media keys


love

A workflow for the new and incredibly awesome Alfred 2, that controls Pandora.com in chrome from Alfred.

Download (PandoraControlsForAlfred.zip)

If you want to give it a go, download the Pandora Controls for Alfred 2 archive. Inside you will find both the workflow and the chrome extension to make this happen. (if you don’t know how to install a crx file in chrome , see here)

Download Pandora Controls for Alfred

Available controls : 

  • pplay –    ”Play/Pause”
  • ppause – “Pause”
  • pnext –   “Next”
  • pfocus –  Focus the pandora tab
  • pplaylist – Show a list of pandora playlists
  • pplaylist {query} – Filter  pandora playlists, select a playlist to start playing

A little bit on how this is done.

I thought longs and hard how to connect to a chrome extension from Alfred. I came up with a couple of methods. I could make the extension connect to a websocket server somewhere, and wait for commands. That’s fine when I’m building a workflow for myself, but I wanted to share it. And I didn’t want to control the Pandora of everyone who’s using the workflow.

Instead I came up with a “hacky” long polling solution. 

I’ve unpacked the very cool SoundControl chrome extension and inserted a timer function, that fires an Ajax request to a localhost server. Most of the time it fails because theres no server listening on that port.

In Alfred, I’ve wired the workflow to fire up a ruby server, that wakes up, receives a connection sends a control, then dies.

Once the server is up, the extension connects, and receives a control action from Alfred input.

That worked like a charm, the only problem being all those Ajax calls failing. I didn’t want my mac to constantly fire an AJAX call even when I’m just listening to pandora. So I added idle detection to the chrome extension. After 15 seconds, the extension stop firing AJAX calls, and when user is active again, starts again.

That was nice and all, but that only let me to control pandora. And I wanted to go further, and see my playlists list inside Alfred and select from there which playlist I want to play.

In order to do that, the AJAX request has all the playlists info on each request. The “playlist” workflow in Alfred starts another server, which collects the playlists from the request data, and shows them inside Alfred.

Then when the user picks a playlist, the old server starts with the needed “action” for pandora to pick up. Easy right?

Media Keys

While writing this post, I decided to go a small step further and achieve any heavy pandora user dream, wire up a system wide hotkeys to pandora. Preferably even the media keys on keyboards.
This can be easily done with the new (and did I say awesome) Alfred!
I set up 2 hotkeys hooks in Alfred to activate the playpause/next workflows respectively. In my keyboards software, I mapped the media keys to some weird combination I’d never use, like cmd+opt+ctrl+shirt+w and then setup the same hotkey in alfred to activate the respective control.
Like-a-charm!

This whole thing smells a little hacky, but it works and works faboulusly. If you, my dear reader have an Idea how to make this better, don’t hesitate to comment :)

Why did I do this?

I gradually fell in love with Alfred, to a point that I now can’t use my macs without it. When version 2 was released, I instantly bought a powerpack license, and went to town!

I really recommend anyone with a mac to atleast download Alfred. And if you do, but the powerpack, it’s so worth it! Only the clipboard history feature is worth those 15GBP for the license, and Alfred has SO MUCH MORE

Disclaimer : I am not affiliated in any way with Alfred. I just love Alfred and love seeing this kind of app succeed.
The team (Ma’ and Pa’ operation, the Pepperrell’s) really listen to their customers, update the software often and are active on twitter and on forums, answering everybody, giving support!

How to fully automate TV downloads

“I love it when an automation comes together…”

TL;DR

In this article I will share my setup, of following, downloading, sorting, subtitling and streaming tv-shows, from end to end.
I’ve polished this process for years, after using FTPs, MIRC, torrents, rapidshare /w jDownloader, wget automation, TvShows.app and countless other methods (who remembers kazaa or DC++ ?).
Finally I’ve achieved Tv enthusiast haven. And now I want to share the love.

Automate all the things!

I love downloading and watching tv-shows of my choice! I love it so much that it’s my main method of watching television. I don’t own a cable box, and I don’t need one.
It used to take me time to search and download episodes manually, which took a lot of time, and was the main disadvantage over a PVR. Who want’s TV that you need to work for.
Well no more!
This article is a reference to all those who want to COMPLETELY automate their downloadable TV experience.
It may seem like much at first, but I promise you, if you stick to it, and set this up once, you will never have to search for episodes again. You will never have to remember when each show air and which episode you’ve seen last.

Disclaimer

This article is provided for reference only. Some of the described tools may be considered illegal in your country. Use the method described here at your own risk.

Requirements

  • A mac (although everything except the subtitlespart can be achieved on a windows machine as well)
  • A usenet provider account (between $9 – $12, depending on the provider)
    • I strongly recommend usenetserver.com (registering through this link will support one of the open source tools in this article!)
  • A good usenet indexer
  • Sabnzbd+ (open source) – to handle downloads from usenet
  • Sickbeard (open source) – follow and automatically download tv shows as soon as they are available
  • Hazel for mac ($25) – Handle files and folders by simple rules
  • Subsmarine ($6.99) – Search and download subtitles from various sites
  • Plex and Plex Media Server – Maintain your tv-shows library, get meta-data, stream and watch your episodes, maintain your watched list.
  • Some free time (believe me, this will save you tons of time later on)

Step 1 – The download manager (Sabnzbd+)

In order to start automating, you need to setup the download process.
First download and setup Sabnzbd+ (Instructions on lifehacker or sabnzbd.org ). The setup is really easy.
Some pointers on the setup :

  • Make sure you secure your installation with a password
  • Take note of your API key, you will need it later on

During the setup wizard, SabNzb+ will ask you to provide your usenet credentials, without this it will not download anything.

Step 2 – The automatic TV downloader (SickBeard)

Secondly, install SickBeard.
The download archive comes with a folder called scripts. Put this folder in your Documents folder and install the app.

Sickbeard will search and send downloads of all your favorite tv-shows to sabnzbd as soon as they’re available online.
SickBeard is an opensource software, but it can be a bit difficult to install from the project site.
To easily install Sickbeard visit http://sickbeard.lad1337.de, and download a mac app from there.
After installing SickBeard, add all your favorite tv-shows, by clicking “add show”
Then go into Config-> search providers, to set up your nzb indexer.
While sickbeard comes with a good indexer on it’s own, a backup will never hurt.

Step 3 – Connect the two programs

After all your tv-shows are set up, comes the tricky part, connecting SickBeard with SabNzbd+ for automatic downloads.
SickBeard and SabNzbd+ are working beautifully together, however to connect the two programs a few steps are required.

First, we need to tell SickBeard that we’re going to use SabNzbd+ for all our downloading needs.
Go into Config->General and choose SabNzbd+ as your NZB Method

image02

Then provide all the details of your SabNzbd+ installation, like IP,Port, Username, Password and API key (can be found in SabNzbd+ ->Config->General page

In the “Sabnzbd Category” field , enter “TV”.
Click “Test SabNzbd” and if everything works, click save.
Then, head over SabNzbd+ page.
There are several things to config here.

  • Go to Config->Folders->”Post-Processing Scripts Folder” and select the scripts folder that you extracted from SickBeard download in the previous step.
  • Go to Config->Categories  and set up a new category, call it TV, and set it up like the picture below.

    image01

  • The +delete option will tell SabNzbd to delete the files after SickBeard has proccessed them and put them into your TV folder
  • The script option is the connecting link between SabNzbd and Sickbeard, and it’s the script that will tell SickBeard to process a newly downloaded file.
  • Hit Save

Now all these settings can be confusing, so to recap, the whole process will work like this :

  • SickBeard finds a new tv-show episode.
  • SickBeard sends the download information to SabNzbd+, with a specific category TV.
  • SabNzbd+ downloads the file, and because we’ve set up a postprocess script for the TV category, it sends a message back to SickBeard that the file is downloaded.
  • SickBeard picks up the file, and puts it in an appropriate folder and season sub-folder.

And that’s it! Whenever a new episode comes out, SickBeard will trigger a download, and then put it in an appropriate folder!

Getting Subtitles

In order to get subtitles, you can download SubsMarine and select an episode, to find and download it’s subtitles.For long time, that’s how I did it, but now it’s automated with Hazel.
Hazel is an awesome piece of software. It comes with a simple interface of rules, that you can add to any folder, and Hazel will execute them.

For downloading subtitles, first, go into SubsMarine settings, and check “one click download” and “Ignore videos with subtitles”.
Then setup Hazel, to watch your tv-shows directory. I’ve added this rule to let hazel open files in SubsMarine as soon as they are downloaded.

image03

Notice I’ve set the “Date Added” rule for files that were added in the last three days. This is due to the fact that your episodes will download as soon as they are available, but the subtitles are ready for those episodes a little bit later.

And because SubsMarine is set to skip files with downloaded subtitles, you will always have a clean folder with the episode and it’s subtitle.

Watching, Streaming and Maintaining a watched list.

Now that we have our folder full of our favorite tv-shows and episodes, we need a way to know what’s new, and what’s already been watched.
Install plex media server, add a new category for “tv shows”, and point it to your tv-shows folder (the one SickBeard saves your files to).

Next, go back to SickBeard, Config -> Notifications, and find Plex Media Server.
Check “enable” and “update library” and hit save.

image00

This will instruct SickBeard to update Plex Media Server each time a new episode is saved to your folders. Plex will then get the information and meta-data for that episode from tvdb.com and put that episode in your “Recently Added” and “On Deck” lists.

Plex manages your watched list, so that you know which episodes you already seen and which are new. If you have 5 unwatched episodes, plex will show you only the next unwatched one in the “On Deck”.

Extras – remotely manage your downloads

You can setup a port-forwarding and access your SabNzbd+ from anywhere in the world. The best way I found is an iPhone app called Tanis ($1.99 – Appstore)
In Tanis, all you need to do is set up your IP and Port, and you can manage the downloads virtually anywhere.

Extras – download movies, music, books and others

Like SickBeard, there’s a ton of other open source packages to automate downloads of pretty much everything as soon as it becomes available online (lifehacker link).

  • CouchPotato allows you to define the movies you want to see, and let them automatically download when available in the format and definition you want.
  • Headphones downloads any music you might like as soon as it’s available
  • Omniverse automatically downloads comic books.
  • LazyLibrarian automatically downloads ebooks.
  • Gamez automatically downloads video games.

 

Connecting – a short film about UI, Interaction trends

I was really inspired by this short film.

It explores the current trends in UI and Interaction, and looking forward, trying to foresee how these trends and techniques can be used for a better, more connected world. 

Very inspiring and well executed.

The 18 minute “Connecting” documentary is an exploration of the future of Interaction Design and User Experience from some of the industry’s thought leaders. As the role of software is catapulting forward, Interaction Design is seen to be not only increasing in importance dramatically, but also expected to play a leading role in shaping the coming “Internet of things.” Ultimately, when the digital and physical worlds become one, humans along with technology are potentially on the path to becoming a “super organism” capable of influencing and enabling a broad spectrum of new behaviors in the world.

http://connectingthefilm.com/