Tag Archives: javascript

wordpress.tv Download Link Script

Scripts No Comments

I’ve been watching a lot of videos from WordPress TV lately. There is some absolutely cracking content on there! However, I really wanted to be able to download videos. There wasn’t a download link, so I wrote this Greasemonkey script to add one.

Download

This script can be downloaded from Userscripts.org

Why Download Vidoes From WordPress.tv?

I mostly consume WordPress.tv content from my Android phone.

I don’t want to watch it over my 3G connection. These videos are often 400MB! It seems there’s a lower bandwidth version when you visit on a mobile device, but it’s still around 80MB in the one I did test. That’s still going to chew through my data plan far too quickly.

Sure, I could watch when connected via WiFi, but these videos are long enough that I may want to stop halfway through and pick it up later (when I may be on my 3G connection). If I leave the page open, then the video should play without needing to download anything later – but that’s a big if. Having the raw mp4 file on my SD card gives me more flexibility.

Also, although there are some basic controls built into the player (pause, move around the timeline), it doesn’t compare with the native player on my phone. It can even be viewed in full screen, but it’s fiddly to activate that control.

So all in all, it’s more convenient to be able to download the video file and have it on my device, rather than watch it through the website.

The Code

As with most of my Greasemonkey Scripts, I’ve embedded jQuery in it. This makes it so much quicker to write. It ends up being only a few lines:

[sourcecode language=”javascript”]
$(document).ready(function($){
// add contents of noscript into the HTML so we can access it via the DOM, hide it, then extract the URL and add Download link
$(‘noscript’).before(‘<div style="display:none" id="wp-tv-download">’+$(‘noscript’).eq(0).text()+'</div>’);
var url = $(‘div#wp-tv-download a’).attr(‘href’);
$(‘div#wpl-likebox’).after(‘<div style="text-align:left !important" class="sd-block"><h3 class="sd-title">Download:</h3><div class="sd-content"><a href="’+url+’">’+url+'</a></div></div>’);
});
[/sourcecode]

Line 3: We grab the contents of the noscript element, which includes the download link, and add it in a hidden div before the noscript element. We have to do this so we can manipulate the content of the noscript element, as we can’t access it via the DOM.

Line 4: We then extract the URL from the hidden div we created (which is now in the DOM) and store it in a variable.

Line 5: We then add the download link after the #wpl-likebox div. Note that we use the wordpress.tv classes, such as sd-block, sd-title and sd-content, which makes the result look like the rest of the site.

License

This script is dual licensed under the MIT and GPL licenses.

How To Get The Domain From A URL Using JavaScript

| Created: August 29th, 2011
JavaScript Development 12 Comments

I’m sure this is a common problem: getting the domain or host name from a URL using JavaScript. There are certainly many solutions to this problem out there. However, the solutions I found weren’t robust enough for my needs, so I ended up writing my own.
Continue reading

The John O’Nolan Twitter Redesign Script

| Created: October 7th, 2010
Scripts No Comments

A week or so ago, John O’Nolan tweeted a link to a Photoshop redesign of Twitter, making it look less cluttered. Like a bit of a nerd, I turned it into a Greasemonkey script.

Really it was just a bit of fun. Once I start thinking about how to do something, I may as well just go and do it. So it was just a once off – I won’t be updating the script in future, etc.

Continue reading

markItUp! For Greasemonkey

| Created: February 24th, 2010
Scripts 6 Comments

The other day a great comment was left on this site. When I went to answer, I realized that I wanted to be able to select part of what the commentator said and automatically turn it into a quote in my comment.

Copy and paste are too mundane for me, so I wrote a Greasemonkey script that adds the markItUp! Editor to any textarea on a page. I then extended markItUp! by adding a ‘quote’ button that wraps the currently selected text (from anywhere on the page) in blockquote tags.

Continue reading

Show Adsense To Search Visitors Only – On Blogger

| Created: January 31st, 2009
Adsense Smart Pricing 59 Comments

Want to make money online with Google Adsense? If so, you better make sure you don’t get smart priced! One of my most successful posts covers how to avoid smart pricing by only serving Adsense to search engine visitors. That post was written for the WordPress platform. In this post, I outline how to do the same on Blogger blogs (although there are severe limitations). Continue reading

Creating A JavaScript Array Dynamically Via PHP

| Created: March 20th, 2008
PHP Development 16 Comments
Update – 1 August 2013:
There are lots of suggestions in the comments that are better than my original solution. Also, with 5 years of hindsight, I’d approach it differently anyway! The way I’d do this now would be as follows:
[sourcecode language=”php”]
<script type="text/javascript">
var lcValues = <?php echo json_encode($myPHPArray); ?>;
</script>
[/sourcecode]
So much simpler! PHP4 has almost disappeared so there’s no reason not to use json_encode. I’d recommend that you stop reading now and just go with that.

If you use PHP to create web pages, there may be times when you need to create a JavaScript array dynamically from a PHP array. This is not hard to do, but there is a trap you need to be careful of: dealing with arrays containing only a single element. Continue reading