Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, June 25, 2009

Drawing coordinates from a Twitter feed in Google Maps

A friend of mine is in vacations and he started posting coordinates of the places he was visiting on Twitter. It would be nice to show this path on a map, I thought, and that is what I did in one hour last night.

I used the "write less, do more" Javascript library jQuery, along with jTwitter, a utility on twitter that works as a jQuery plugin, in a mashup with google maps.

You can see the result here. Type in the twitter username of someone that is posting coordinates (strings like "(-23.748504,-65.500402)" will be parsed out from any tweet text), and that's it!



Following is most of the code for this. I added a couple of lines to jTwitter to allow to (a) call a custom function after each tweet is rendered, and (b) call another function when everything is done. Then we do this when pressing the Go! button:

var matches, u = $("input#user").val(), a = [], s = [];
var fitem = function() { // Called on each tweet. "this" is the tweet object
if (matches = this.text.match(/\(-?[0-9]+\.[0-9]+,-?[0-9]+\.[0-9]+\)/g)) {
for (var i=0; i<matches.length; i++) {
a.push(matches[i]);
s.push(this.text);
}
}
};
var fload = function() { // Called when all tweets were read and written to html
draw(a, s);
}
$("#target").jTwitter({ username: u, onitem: fitem, onload: fload });


And this for drawing the coordinates we just parsed:

function draw(coords, texts) {
// All coords are texts -- Map them into GLatLng objects
var points = [];
for (var i=0; i<coords.length; i++) {
var m = coords[i].substr(1, coords[i].length-2).split(",");
points.push(new GLatLng(parseFloat(m[0]), parseFloat(m[1])));
}
// Create a map, and the GPolyLine from the points
var line = new GPolyline(points, "#FF0000", 2.0, 0.8);
var map = new GMap2(document.getElementById("map"));
// Center map and set zoom level to show our points
var bounds = line.getBounds();
var zoom = map.getBoundsZoomLevel(bounds);
if (bounds) map.setCenter(bounds.getCenter(), zoom);
// Render each point as a separate marker (to get a tooltip), and draw the polyline
for(var i=0;i<points.length;i++) {
map.addOverlay(new GMarker(points[i], {
title: texts[i]
}));
}
map.addOverlay(line);

// More mapping stuff
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
map.addControl(new GLargeMapControl());
map.addMapType(G_PHYSICAL_MAP);


It is a toy and with very limited functionality... but it was a nice exercise. I am planning to add features, I think it is a cool idea. I would love to hear if anyone has ideas around this.

Wednesday, June 10, 2009

Badware report! Hacked?

My blog is about the little hacks I do every day. Like wikipedia's definition #5 of the verb hacking:
  • #5. (computing) To accomplish a difficult programming task. He can hack like no one else and make the program work as expected.
Yes, that's my hacking. But today I think I was hacked, sadly with the definition #4 of this term:
  • #4. (slang, computing) To hack into; to gain unauthorized access to a computer system (e.g. website) or network by manipulating code; a crack.
I received an hour ago an email starting like:

Dear site owner or webmaster of (mysite),

We recently discovered that some of your pages can cause users to be infected with malicious software. We have begun showing a warning page to users who visit these pages by clicking a search result on Google.com.

...

In fact, if I do a google search that lists my site, it says below "This site can harm your computer". And if you are brave and click the result, an awful page with a big and red Warning! message stops you explaining my site is going to harm you...
It turned out that somehow, I got injected a couple of script tags in some pages. They were an obfuscated javascript code, starting like this:

FBE38="parseI";FBE38+="n";FBE38+="t";DD6EDCFB9FEE="St";
DD6EDCFB9FEE+="rin";DD6EDCFB9FEE+="g.";DD6EDCFB9FEE+="fro";
DD6EDCFB9FEE+="mC";DD6EDCFB9FEE+="harCode";
function F5304B9279(C5F40D2E4C)
....
Well, a fun hacking (definition #5, for us good hackers) exercise is to decode that. I am not going to post the entire code here. It did some wrapping around parseInt(_,16) and String.fromCharCode to build a kind of evaluator of a piece of javascript code, encoded as a very long hexadecimal string. Then, when evaluated, it inserted an iframe linking to a place hosted in hostsads.cn domain. I don't know what they inserted in my page. It might be invisible, it might be harmful and exploit some browser vulnerability.

Scripts removed, passwords reset, a form filled into google for my site to be reviewed and the warning removed.

But.... Did anyone have the same problem? Do you know what they do exactly with the injected iframe? I could find this problem by googling around -- I would love to hear if anyone have seen it.