General Musing

blaze your trail

YouTube Channel Unsubscribe #bookmarklet

with 2 comments

I like , and often subscribe to new channels and unsubscribe after a while. There is a lot of good stuff on YouTube, and a lot of rubbish. YouTube has been smart in making it possible to easily add channels, and in my opinion they have put less thought into making it easy to leave the channels. In the case of multiple channels I would either need to go to each of the channels and remove them one by one, or I would go to My Feeds / My Subscriptions and remove them one by one from there. In both cases I need to modify the subscription settings shown below.

Editing the YouTube channel subscription

Editing the YouTube channel subscription

My issue with this is not that it takes a long time, although that is certainly a small factor. Or that having subscription management for a channel is overkill when I just want to leave a channel. Personally I believe that it should be manageable from one page, just push of a button, like subscribing. And it wasn’t, until today.

While examining the code I discovered that all the subscription changes are send using Ajax, with most of the relevant data in the query string, with the exception of the session token which is in the body. A subscribe/unsubscribe message generally contains the following information.:

  • Channel Username
  • Subscription Identifier
  • Subscription Status (unsubscribe, uploads, all_activity)
  • Session Token

And a typical request URL looks like this:

http://www.youtube.com/ajax_subscriptions?edit_subscription=YXPHNxiiXUU&username=CBS&subscription_level=unsubscribe

In spite of the obfuscation it’s clear to see the difference in coding conventions used bij the YouTube and Google developers, much of the JavaScript is inconsistent over the site, using a mixture of global variables which might be called something else on different pages, or even have a different structure. I assume that this is the legacy code which I assume they are slowly replacing, as it does look like they are migrating to a more clean and structured codebase. I’m mentioning this as this was my first hurdle: retrieving the Session Token. The Session Token is called window.subscribeaxc in some places and gSecureTokens["ajax_subscriptions"] in others. And the main function used to perform the subscriptions events relies on some global variables and form fields, rather than passing the values straight to the function:

yt.www.subscriptions.edit.onUpdateSubscription(

    <session_token>,
    <subscription_id>,
    <attribute_suffix>

);

All in all it was pretty clear what I would need to do:

  1. research the variables and functions that already exist on YouTube for reuse
  2. write a proof of concept bookmarklet based on the research
  3. pick an existing central location for the subscription management
  4. write the subscription management bookmarklet containing:
    1. hidden form
    2. data access
    3. integrate it into a central location, the My Subscriptions page
    4. test it all
  5. release the bookmarklet
  6. convert to GreaseMonkey userscript
  7. write a blog post on my experience

The research didn’t need to be very thorough, as I had most of the answers I felt I needed, so I started writing my POC, which can be used on the page of a channel to unsubscribe. I wrote Unsubscribe From Current YouTube Channel (pastebin.com) in about 30 minutes, it was reasonably simple to interact on this part of the site, the page already had the hidden form elements needed for onUpdateSubscription to play nice.

I’d already picked the My Subscriptions page as the central location, so all that remained was to start on the management script. I started by writing the JavaScript to cycle through the list of subscriptions and generate the dynamic unsub links, for the function to work I needed the Subscription Identifier, which was in the onclick attribute of the channels in the subscriptions list, some simple regexp to seperate the code from the identifier. Then I took the channel name, which is the anchor text in the channel list, and put it all together in a event handler. I make a small mistake that meant that the Username and the Subscription Identifier where being overwritten, I had forgotten that closures work a little different in JavaScript than I expected, but that was easily rectified by adding these as attributes to the unsub anchor. I had thought of putting all the items in an Array or Map, but decided that it was too much overhead for what I was doing.

Next I started building the hidden form, due to the nature of the obfuscated code I new that I could make 90%, but the last 10% would be trial and error. I got a load of errors on the first run, but these were easily solved. All that remained in the development cycle was to merge the 3 scripts together and finish debugging. And this is the finished product:

Create YouTube Unsubscribe Buttons (pastebin.com)

It has currently been tested in FireFox, Safari and Chrome.

Here are all the links again:

Written by Daniël W. Crompton (webhat)

May 22, 2010 at 8:14 pm

2 Responses

Subscribe to comments with RSS.

  1. Nice article. Are you kowing some websites where is possbile to download youtube clips?

    Interesting

    September 19, 2010 at 11:44 am

    • You can download them using many different programs, I usually use Real Player, or you can ask the owner of the original file if he can make it available to you.

      webhat

      September 20, 2010 at 10:15 am


Leave a reply to webhat Cancel reply