Gitorious : DNS service for this domain has expired with DNS Made Easy :)

by nolovelust 23. December 2010 17:17

It happens to everyone, even large startups! Someone forget to pay their DNS hosting fees at Gitorious :)

 

Tags: , ,

Funny

google sitelinks stays after design change

by nolovelust 19. December 2010 13:40

I was worried about Google sitelinks disappearing after design change on one of my sites but after using permanent redirection from each link to new target Google updated sitelinks within couple of days!

Tags: ,

Useful

override javascript alert with jquery

by nolovelust 19. December 2010 13:38

Sample One

<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<style type='text/css'>
#popup{
display: none;
position: absolute;
left: 100px;
top: 100px;
background-color: #ffffdd;
border: 1px solid #888;
padding: 5px;
cursor: pointer;
}

#title{
font-weight: bold;
}
</style>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$(document).ready(function() {

$("a").click(function(evt) {
var title = $(this).data('title');
var msg = $(this).data('msg');
var top = evt.pageY + 3;
var left = evt.pageX + 3;
showPopup(top, left, title, msg);
});

$("#popup").click(function() {
$(this).hide();
});

});

function showPopup(top, left, title, msg) {
$("#title").html(title);
$("#msg").html(msg);
$("#popup").css({
top: top,
left: left
}).show();

}
});
//]]>
</script>

</head>
<body>
<a href='#' data-title='Alert' data-msg='This is my message'>alert</a>
<div id="popup">
<div id="title"></div>
<div id="msg"></div>
</div>

</body>
</html>

 Sample Two

<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<script>window.alert = function() {
jQuery('<div></div>', { html: arguments[0].replace(/\n/, "<br />") }).dialog({title:'Alert'});
};</script>
<title>Test Page</title>
</head>
<body>
<a href="#" onclick="alert('test');return false;">Custom Alert</a>

</body>
</html>

 

from http://stackoverflow.com/questions/4483088/can-jquery-replace-default-javascript-alert-or-confirm/

Tags: , , ,

Useful

mobile geolocation with jquery and google maps api and asp.net c#

by nolovelust 13. December 2010 09:38

Below code uses google maps api with local proxy code to get country,city and local area of user. Fully functuional apart from multiple entries not being filtered in the result. I'll hopefully fix it too.

It only workd phones that support navigator.geolocation such as iPhone, webOs, Android, Nokia N900

Due to same origin policy I've had to use local proxy code to download location data from google (see below)

 

To run the code create an html file and paste below code on to it 

<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
         function find() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(success, error);
            } 
            else if ( window.google && google.gears ) {
            var geo = google.gears.factory.create( 'beta.geolocation' );
            geo.getCurrentPosition(success, error);
          }
          else {
                $('#output').html('Not supported');
            }
        }
        function success(position) {
            var lat = position.coords.latitude;
            var long = position.coords.longitude;
            var pullurl = 'getLocationResponse.ashx?latlng=' + lat + ',' + long;
            get(pullurl);
            //$('#output').html(pullurl);
        }
        function error(msg) {
            $('#output').html('failed to get coordinates');
        }
        function get(url) {
            $.ajax({
                type: "GET",
                url: url,
                dataType: "xml",
                success: parseXml,
                error: function (xhr, status, error) {
                     $('#output').html("An AJAX error occured: " + status + "\nError: " + error);
                }
            });
        }
        function parseXml(xml) {
            $(xml).find("GeocodeResponse").each(function () {
                if ($(this).find("status").text() == "OK") {
                    parseOkXml(xml)
                }
                else {
                    $('#output').html('response code was not OK');
                }
            });
        }
        function parseOkXml(xml) {
            $(xml).find('type').each(function () { // find all "type" tag in XML
                if ($(this).text() == 'administrative_area_level_1') { // in here $(this) is our "type" tag
                    add($(this).parent().find('long_name').text());
                    //return false;
                }
				 if ($(this).text() == 'administrative_area_level_2') { // in here $(this) is our "type" tag
                    add($(this).parent().find('long_name').text());
                    //return false;
                }
				 if ($(this).text() == 'administrative_area_level_3') { // in here $(this) is our "type" tag
                    add($(this).parent().find('long_name').text());
                    //return false;
                }
            });
        }
		function add(data)
		{
                $('#location').append('');
		}
    </script>

Proxy code to load maps data from google getLocationResponse.ashx

 

<%@ WebHandler Language="C#" Class="getLocationResponse" %>
using System;
using System.Web;
using System.Net;
using System.IO;

public class getLocationResponse : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/xml";
        string latlng = context.Request.QueryString["latlng"];
        string url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + latlng + "&sensor=true";
        WebRequest request = WebRequest.Create(url);
        request.Timeout = 1000;
        ;


        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(dataStream))
                {
                    context.Response.Write(reader.ReadToEnd());
                }
            }
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 you now can request location info by calling find() JavaScript function from html

Tags: , , , , ,

ASP.NET | Mobile web | Useful

Generate website screenshot/thumbnail with .net webbrowser control on c# asp.net website

by nolovelust 4. December 2010 11:29

Attached code uses altered version of WebPreview from http://smallsharptools.com/Projects/WebPreview/. WebPreview works just fine with most of the sites but, if site you are trying to get screenshot/thumbnail has a JavaScript prompt, by default webbrowser control shows it to user. Its ok if you are using it on an asp.net application, user can click to prompt. Things change if you try to use it on an asp.net website. As there will be no one to click to prompt or even no way to display the prompt code hangs. I have derived from webbrowser control and overwrite OnNavigated event and injected some java script code to disable prompts. Download SmallSharpTools.WebPreview-1.0.0-src.rar (84.13 kb). Keep in mind that webbrowser control uses internet explorer installed on the system and trying to get screenshots/thumbnails of malicious sites may create trouble for your server.
You may also be interested with http://nolovelust.com/post/C-Website-Screenshot-Generator-AKA-Get-Screenshot-of-Webpage-With-Aspnet-C.aspx

 

WebBrowserEx.cs 

 

using System.Windows.Forms;
using mshtml;

namespace SmallSharpTools.WebPreview
{
    class WebBrowserEx : WebBrowser
    {
        public WebBrowserEx() { }

        protected override void OnNavigated(WebBrowserNavigatedEventArgs e)
        {

            HtmlElement he = this.Document.GetElementsByTagName("head")[0];
            HtmlElement se = this.Document.CreateElement("script");
            mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)se.DomElement;
            string alertBlocker = "window.alert = function () { }";
            element.text = alertBlocker;
            he.AppendChild(se);
            string alertBlockerB = "window.confirm = function () { }";
            element.text = alertBlockerB;
            he.AppendChild(se);
            string alertBlockerC = "window.prompt = function () { }";
            element.text = alertBlockerC;
            he.AppendChild(se);
            string alertBlockerD = "window.open = function () { }";
            element.text = alertBlockerD;
            he.AppendChild(se);
            
            base.OnNavigated(e);
        }
 
    }
}

 

 

 ThumbnailBuilder.cs

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace SmallSharpTools.WebPreview
{
    public class ThumbnailBuilder
    {

        #region "  Events  "

        public event EventHandler ExceptionCatching;

        /// 
        /// Raise the ExceptionCatching event
        /// 
        /// 
        protected virtual void OnExceptionCatching(EventArgs e)
        {
            if (ExceptionCatching != null)
            {
                ExceptionCatching(this, e);
            }
        }

        #endregion

        #region "  Methods  "

        public void CreateThumbnail(string sourceFilename, string cachedFilename, int width, int height)
        {
            if (File.Exists(sourceFilename))
            {
                FileInfo inputFile = new FileInfo(sourceFilename);
                FileInfo outputFile = new FileInfo(cachedFilename);

                if (!outputFile.Directory.Exists)
                {
                    outputFile.Directory.Create();
                }

                if (outputFile.Exists && inputFile.CreationTime < outputFile.CreationTime)
                {
                    return;
                }

                try
                {
                    using (Bitmap inBmp = new Bitmap(sourceFilename))
                    {
                        using (Bitmap outBmp = new Bitmap(width, height))
                        {
                            using (Graphics g = Graphics.FromImage(outBmp))
                            {
                                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                g.DrawImage(inBmp, 0, 0, width, height);
                            }
                            outBmp.Save(cachedFilename);
                        }
                        
                    }
                }
                catch (Exception ex)
                {
                    CurrentError = ex;
                    OnExceptionCatching(EventArgs.Empty);
                }
            }
        }

        #endregion

        #region "  Properties  "

        private Exception _currentException = null;

        public Exception CurrentError
        {
            get { return _currentException; }
            set { _currentException = value; }
        }

        #endregion

    }
}

 

SiteConfiguration.cs 

 

using System.Configuration;
using System.Web;

/// 
/// Summary description for SiteConfiguration
/// 
public class SiteConfiguration
{
    
    public static string SourceImageDirectory
    {
        get
        {
            return HttpContext.Current.Server.MapPath(
                ConfigurationManager.AppSettings["SourceImageDirectory"]);
        }
    }

    public static string CachingImageDirectory
    {
        get
        {
            return HttpContext.Current.Server.MapPath(
                ConfigurationManager.AppSettings["CachingImageDirectory"]);
        }
    }

}

 

 

PreviewBuilder.cs 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace SmallSharpTools.WebPreview
{
    public class PreviewBuilder
    {

        #region "  Variables  "

        private string _url = String.Empty;
        private string _filename = String.Empty;

        #endregion
        
        #region "  Constructor  "

        public PreviewBuilder(string url, string filename)
        {
            this._url = url;
            this._filename = filename;
            // Path.Combine(Environment.CurrentDirectory, "output.bmp");
        }

        #endregion

        #region "  Methods  "

        public void CreatePreview()
        {
            ThreadStart ts = new ThreadStart(this.DoWork);
            Thread t = new Thread(ts);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            // TODO find the proper way to wait for a thread
            // wait for the thread
            while (t.IsAlive)
            {
                Thread.Sleep(25);
            }
        }

        private void DoWork()
        {
            Bitmap bitmap = GetPreviewImage();

            bitmap.Save(_filename);
            bitmap.Dispose();
        }

        private Bitmap GetPreviewImage()
        {
            WebBrowser wb = new WebBrowserEx();
            wb.ScrollBarsEnabled = false;
            wb.Size = new Size(Width, Height);
            wb.ScriptErrorsSuppressed = true;
            wb.NewWindow += new System.ComponentModel.CancelEventHandler(wb_NewWindow);
            wb.Navigate(_url);

           
            // wait for it to load
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
            Bitmap bitmap = new Bitmap(Width, Height);
            Rectangle rect = new Rectangle(0, 0, Width, Height);
            wb.DrawToBitmap(bitmap, rect);
            return bitmap;
        }

        void wb_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        #endregion

        #region "  Properties  "

        private int _width = 1024;

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private int _height = 768;

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        #endregion

    }
}

 

WebPreviewHandler.ashx 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Caching;
using SmallSharpTools.WebPreview;

public class WebPreviewHandler : IHttpHandler 
{
    
    public void ProcessRequest (HttpContext context) 
    {
        string url = context.Request.QueryString["url"];
        
        int width;
        if (!int.TryParse(context.Request.QueryString["width"], out width))
        {
            width = 150;
        }
        int height;
        if (!int.TryParse(context.Request.QueryString["height"], out height))
        {
            height = 100;
        }
        
        string sourceFilename = GetSourceFilename(url);
        FileInfo file = new FileInfo(sourceFilename);
        
        if (!file.Exists || file.CreationTime < DateTime.Now.AddHours(-4))
        {
            PreviewBuilder pb = new PreviewBuilder(url, sourceFilename);
            pb.CreatePreview();
        }

        string cachedFilename = GetCachedFilename(sourceFilename, width, height);
        
        if (!File.Exists(cachedFilename))
        {
            ThumbnailBuilder tb = new ThumbnailBuilder();
            // handle the error by throwing it
            tb.ExceptionCatching += delegate { throw tb.CurrentError; };
            tb.CreateThumbnail(sourceFilename, cachedFilename, width, height);
        }  
        
        context.Response.ContentType = GetContentType(cachedFilename);
        context.Response.WriteFile(cachedFilename);
    }
    
    public string GetContentType(string filename)
    {       
        string contentType = "image/x-unknown";
        string ext = Path.GetExtension(filename);
        switch (ext)
        {
            case ".jpg":
                contentType = "image/jpeg";
                break;
            case ".gif":
                contentType = "image/gif";
                break;
            case ".png":
                contentType = "image/png";
                break;
            default:
                contentType = "image/jpeg";
                break;
        }
        return contentType;
    }
    
    public string GetSourceFilename(string url)
    {
        Uri uri = new Uri(url);
        string shortFilename = uri.Host.Replace(".", "_") + 
            uri.LocalPath.Replace("/", "_") + ".png";
        return Path.Combine(SiteConfiguration.SourceImageDirectory, shortFilename);
    }
    
    public string GetCachedFilename(string sourceFilename, int width, int height)
    {
        FileInfo sourceFile = new FileInfo(sourceFilename);
        string shortFilename = sourceFile.Name;
        string ext = Path.GetExtension(shortFilename);
        string replacementEnding = String.Format("-{0}x{1}", width, height) + ext;
        string cachedFilename = shortFilename.Replace(ext, replacementEnding);
        return Path.Combine(SiteConfiguration.CachingImageDirectory, cachedFilename);
    }
    
    public bool IsReusable 
    {
        get {
            return false;
        }
    }

}


Tags: , , , ,

Tag cloud

Month List