Crazy Windows with JavaScript

Filed under: JavaScript, Programming, Web Development — Wrote by Kay Park on Saturday, March 29th, 2008 @ 3:58 pm

Ever seen a window shake like crazy? Well here’s a script for it. You can start annoying all your friends with this simple and crazy script.

It’s a relatively simple code but the effect is cool enough. It tends to stop in IE if you click on another window, but it works nicely on firefox.

<script type="text/javascript">
function lol(){
 self.moveTo(Math.random()*100,Math.random()*100);
 window.setTimeout("lol()",50);
}
window.setTimeout("lol()",50);
</script>

This code first defines a function called lol() which contains the self object connected to the moveTo() function.
The notation is moveTo([distance from left],[distance from top]). The
example above generates a random string which is between 0 and 1(like
0.5487732…) using the random() function of the Math object and multiplies it by 500 to make a number that’ll move the window enough.

We then have the setTimeout() function. The notation of this one is
setTimeout(”[function]“,[time(in milliseconds)]). So, in this case, the
position of the window changes every 50 milliseconds. You can adjust
this to make it faster or slower.

Preview the example (press alt+f4 to close)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati

FLV Downloader Tips

Filed under: PHP, Programming, Web Development — Wrote by Kay Park on Saturday, March 29th, 2008 @ 2:03 pm

The FLV video downloader I designed was written in PHP and some Javascript on the client side. So you’ll need to know a bit of PHP to understand how this works. It’s actually very simple, all you do is get the source from YouTube and then parse it so only the address of the .flv file is left. The following script locates the download URL and returns it. This is the core to any downloader (it’s the same concept for even Google Video downloaders and so on).

<?php
function get_video_url($id){
$url = "http://youtube.com/watch?v=".$id;
if ($contents = @file_get_contents($url)) {
if (preg_match('/video_id=\S+&.+&t=.+&f/i', $contents, $match)) {
$vars = $match[0];
$url = “http://www.youtube.com/get_video?”.$vars;
return $url;
}
}
}
?>

Line by Line Analysis

This line by line analysis of the code above should help you understand the script better.

function get_video_url($id){

This line declares a function (a repetitively usable operation) called get_video_url() so we can use it easily and efficiently. The $id is the video id from YouTube and would be a submitted value, something like FzRH3iTQPrk.

$url = "http://youtube.com/watch?v=".$id;

This line stores the youtube url will the video id in the variable $url.

if ($contents = @file_get_contents($url)) {

This line of code stored the HTML code of the YouTube page ($url) by using the file_get_contents()
function (note that some hosting services may have disabled the function). The line below it will only be executed if $contents is not empty since the if() operator is controlling it.

if (preg_match('/video_id=\S+&.+&t=.+&f/i', $contents, $match)) {

This is the core of the whole thing. It finds the required
information to get the video URL from the source code by searching the HTML code with regular expressions using the preg_match() function. This function uses regular expressions to find patterns in a variable (in this case $html) and puts the results as arrays into another variabe (in this case $match).

$vars = $match[0];

This line stores the needed information from the array $match[0] (which is the first item) into the $vars variable.

$url = "http://www.youtube.com/get_video?".$vars;

This line finally puts the information together to get the full video URL. It’s quite simple, it’s just combining the found information and http://www.youtube.com/get_video together.

return $url;

This line just returns the $url variable so we can use it later. The function can then be used by doing something like this:

echo get_video_url($url);

The rest is just closing the function and the if() operator. Try to look the function up in the PHP manual (they’re linked above).

Tips

If you want to make a downloader for other sites, try using this method:

  1. Get the firefox plugin: tamper data
  2. Go to the site with tamper data open and see what requests come in.
  3. Look for an xml page or an flv file.
  4. Look for a pattern and see how you could automate this.
  5. Write a script for it.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati
© FLIXEY.COM