Easily add multiple lightbox style popup videos to your page using Flowplayer and jQuery Tools Overlay

Adding a video to your page is easy enough by embedding directly or by using something like flashembed to embed Flash. What we needed to do was have multiple links on a page that would each open specific videos, whilst still keep the same generic JavaScript somewhere in a separate page template file. The accompanying CSS also had to stay in a separate CSS file and also needed to be generic. Our videos were all in .FLV format and needed to pop over the content in a nice overlay so as not to disturb the flow of the page.

Blacktrash have a nice demo of the kind of functionality we needed but it wasn’t quite generic enough to manage our very different sized videos. The code below, based on this example using Flowplayer and jQuery Tools Overlay, is simple enough that you can easily add multiple videos to a page. The size of the video player is determined by the html5 style attributes “data-width” and “data-height” on each “a” link which are picked up by jQuery and used to resize the player. If you do not specify these then the default is used, as given by the CSS.


<html>
<head>
<!-- include the flowplayer, jQuery and jQuery Tools libraries -->
<script src="http://releases.flowplayer.org/js/flowplayer-3.2.11.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js" type="text/javascript" charset="utf-8"></script>

<!-- set up the flowplayer to play videos in an overlay using jQuery Tools Overlay -->
<script type="text/javascript">
  $(function () {
    var player;
    $("a[rel]").overlay({
      mask: {
        color: '#000',
        opacity: 0.2
      },
      onLoad: function () {
		// create player object and load in the href of the link clicked as the source of the video
	    player = $f("player", "http://releases.flowplayer.org/swf/flowplayer-3.2.15.swf",this.getTrigger().attr("href"));
		player.load();
		// set the height and width based on data-height and data-width
		$("#player").height(this.getTrigger().attr("data-height"));
		$("#player").width(this.getTrigger().attr("data-width"));
      },
      onClose: function () {
        player.unload();
      }
    });
  });
</script>

<!-- set up the CSS to hide the overlay by default and set up default height and width for the player -->
<style>
#overlay {
      display: none;
      padding: 40px;
    }
    .close {
      background:url(http://flowplayer.org/media/img/overlay/close.png) no-repeat;
      position:absolute;
      top:2px;
      right:5px;
      display:block;
      width:35px;
      height:35px;
      cursor:pointer;
    }
    #player {
      display: block;
      width: 600px;
      height: 300px;
      margin: 0;
    }
    #player *:focus {
      outline-style: none;
    }
</style>
</head>
<body>

<!-- each link contains a reference to the video to be played in flowplayer, the JavaScript above ensures they are played in an overlay -->
<a rel="#overlay" href="http://www.sitename.com/video/video1.flv">Video 1</a>
<a rel="#overlay" href="http://www.sitename.com/video/video800x600.flv" data-height="600" data-width="800">800x600 Video</a>

<!-- the overlay <div> itself is empty apart from a &amp;nbsp; and hidden using CSS to make sure it isn't shown on screen -->
<div id="overlay"><a class="close"></a><div id="player">&amp;nbsp;</div></div>

</body>
</html>

You can do a lot more with flowplayer and overlays so this is just a starting point but the approach is generic enough that any of the web team can add nice popup videos to a page just by adding a simple “a” link.

Easily retrieve HTML content from websites using C# and HTML Agility Pack (HtmlAgilityPack)

I needed to retrieve a chunk of content (a product price) within a certain DIV (<div class=”price”>) on a website for a large number of search terms. The quickest way of doing this was to use the HtmlAgilityPack library for .NET to call the website’s search page (with custom search terms) and then pull out the content I needed using the SelectNodes() method. SelectNodes() uses XPATH to search across the document, which sounds tricky but is easy when you know some basic rules. XPATH is described well on w3schools if you need to know the syntax. You will need to download the Html Agility Pack .DLL and make a reference to it in your code.

The following is a simplified version of my code with a file reader/writer to read in a text file containing search terms line by line and output the result to another text file. The exact code will change depending on the website you are using, especially the XPATH in SelectNodes():

using System;
using System.IO;
using HtmlAgilityPack;

namespace webget
{  class Program
  {
    static void Main(string[] args)
    {
      string row = "", line = "";
      StreamReader infile = new StreamReader("input.csv");
      StreamWriter outfile = new StreamWriter("output.csv");

      // Create the Html Agility Pack object
      HtmlWeb hw = new HtmlWeb();

      while ((line = infile.ReadLine()) != null)
      {
        // load the website and store in a Htmldocument object
        HtmlDocument doc = hw.Load("http://www.website.com/search.php?keywords=" + line.Trim());

        try
        {
          // loop through every DIV with class "price" on the website and extract the content
          foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//div[@class='price']"))
          {
            // clean up the content by removing any standard text items and the £ symbol
            string price = link.InnerText.Replace("£", "").Replace("r", "").Replace("t", "").Replace("n", "").Trim();
            line += price + ",";
          }
        }
        catch
        {
          line += ",ERROR";
        }

        Console.WriteLine(line);
        outfile.WriteLine(line);
      }

     infile.Close();
     outfile.Close();
   }
  }
}