Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
JSON Foramter
<html>
<head>
    <title>JSON Formater</title>
    <style type="text/css">
    body{
        margin: 0px;
        padding: 0px;
        overflow: hidden;
    }
    </style>
    <script type="text/javascript">
        var JSONFormater = {
            indentType: "\t",
            source: false,
            output: "",
            format: function() {
                if (document.getElementById("inputJSON").value.length == 0) {
                    alert("Empty source.");
                    return;
                }
                try {
                    eval("JSONFormater.source = " + document.getElementById("inputJSON").value + ";");
                } catch (e) {
                    alert("Invalid JSON data.");
                    return;
                }
                this.indentType = this.selectedIndent();
                this.iteratorChilds(this.source, 0, false);
                this.clearTails();
                document.getElementById("inputJSON").value = this.output;
                this.output = "";
            },
            hasMembers: function(obj) {
                for (var m in obj) return true;
                return false;
            },
            isArray: function(obj) {
                return obj.constructor == Array;
            },
            isString: function(obj) {
                return obj.constructor == String;
            },
            iteratorChilds: function(obj, indent, isMember) {

                if (this.isArray(obj)) {
                    this.output += "[\n";
                    for (var i = 0; i < obj.length; i++) {
                        this.iteratorChilds(obj[i], indent + 1, false);
                    }
                    this.clearTails();
                    this.output += this.currentIndent(indent);
                    this.output += "],\n";
                } else if (this.hasMembers(obj)) {
                    if (!isMember) this.output += this.currentIndent(indent);
                    this.output += "{\n";
                    for (var e in obj) {
                        this.output += this.currentIndent(indent + 1);
                        this.output += (this.isString(e) ? "\"" + e + "\"" : e) + ": ";
                        this.iteratorChilds(obj[e], indent + 1, true);
                    }
                    this.clearTails();
                    this.output += this.currentIndent(indent);
                    this.output += "},\n";
                } else {
                    if (!isMember) this.output += this.currentIndent(indent);
                    this.output += this.isString(obj) ? "\"" + obj + "\"" : obj;
                    this.output += ",\n";
                }
            },
            currentIndent: function(ident) {
                var it = "";
                for (var i = 0; i < ident; i++) {
                    it += this.indentType;
                }
                return it;
            },
            clearTails: function() {
                if (this.output.length > 2) this.output = this.output.substring(0, this.output.length - 2) + "\n";
            },
            selectedIndent: function() {
                switch (document.getElementById("indentType").value) {
                    case "1":
                        return "\t";
                    case "2":
                        return "  ";
                    case "3":
                        return "   ";
                    case "4":
                        return "    ";
                    case "8":
                        return "        ";
                }
            }
        };
        window.onresize = function(){
            document.getElementById("inputJSON").style.height = document.body.offsetHeight-30;
        };
    </script>

</head>
<body onload="document.getElementById('inputJSON').style.height = document.body.offsetHeight-30;">
    <textarea id="inputJSON" style="width: 100%;" rows="15" style="overflow: both;"></textarea>
    <div>
        <input type="button" value="format" onclick="JSONFormater.format();" />
        <select id="indentType">
            <option value="1" selected="selected">indent with a tab character</option>
            <option value="2">indent with 2 spaces</option>
            <option value="3">indent with 3 spaces</option>
            <option value="4">indent with 4 spaces</option>
            <option value="8">indent with 8 spaces</option>
        </select>
    </div>
</body>
</html>
Url rewrite using IHttpModule
UrlRewriter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// UrlRewriter 的摘要描述
/// </summary>
namespace idv.modules
{
    public class UrlRewriter : IHttpModule
    {

        #region IHttpModule 成員

        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;

            if (context.Request.Path.EndsWith("TranslatedPage.html", StringComparison.CurrentCultureIgnoreCase))
            {
                string currentUrl = context.Request.Path;

                // rewrite url [http://www.domain.com/actionValue/TranslatedPage.html]
                // to [http://www.domain.com/TranslatedPage.aspx?action=actionValue]
                string action = Regex.Match(currentUrl, "/([^/]+)/TranslatedPage.html", RegexOptions.IgnoreCase).Groups[1].Value;
                context.RewritePath("~/TranslatedPage.aspx?action=" + action);
            }
        }

        #endregion
    }
}
Web.config
<httpModules>
    <add name="UrlRewriter" type="idv.modules.UrlRewriter"/>
</httpModules>
Bomb Sweeper
<html>
<head>
    <title>Bomb Sweeper</title>
    <style type="text/css">
        body
        {
            background-color: #95B7DF;
        }
        #tableContainer td
        {
            width: 20px;
            height: 20px;
            text-align: center;
            vertical-align: middle;
            -moz-border-radius: 5px;
            -khtml-border-radius: 5px;
            -webkit-border-radius: 5px;
        }
    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">
        var BombSweeper = {
            result: true,
            bombMap: false,
            colSize: 0,
            rowSize: 0,
            locateBomb: function(rowSize, colSize, bombCount) {
                var bombArr = new Array(colSize * rowSize);
                var count = 0;
                var index;
                while (count < bombCount) {
                    index = Math.floor(Math.random() * bombArr.length);
                    if (!bombArr[index]) {
                        count++;
                        bombArr[index] = true;
                    }
                }
                this.colSize = colSize;
                this.rowSize = rowSize;
                this.bombMap = bombArr;
            },
            getSurroundedIndex: function(x, y) {
                var indexArr = [];
                for (var i = x - 1; i <= x + 1; i++) {
                    if (i >= 0 && i < this.colSize) {
                        for (var j = y - 1; j <= y + 1; j++) {
                            if (j >= 0 && j < this.rowSize) {
                                indexArr.push(i * this.colSize + j);
                            }
                        }
                    }
                }
                return indexArr;
            },
            getSurroundedBombCount: function(arr) {
                var count = 0;
                for (var i in arr) {
                    if (this.bombMap[arr[i]]) count++;
                }
                return count;
            },
            getTips: function(x, y) {
                var _x = parseInt(x, 10);
                var _y = parseInt(y, 10);
                if (this.bombMap[_x * this.colSize + _y]) {
                    return -1;
                } else {
                    return this.getSurroundedBombCount(this.getSurroundedIndex(_x, _y));
                }
            },
            isBomb: function(x, y) {
                var _x = parseInt(x, 10);
                var _y = parseInt(y, 10);
                return this.bombMap[_x * this.colSize + _y] == true;
            }
        };
        $(document).ready(function() {
            $("#generateTable").bind("click", function() {
                var colSize = parseInt($("#colSize").val(), 10);
                var rowSize = parseInt($("#rowSize").val(), 10);
                var bombCount = parseInt($("#bombCount").val(), 10);

                if (isNaN(colSize) || isNaN(rowSize)) {
                    $("#tableContainer").text("Not availible size.");
                } else {
                    BombSweeper.locateBomb(colSize, rowSize, bombCount);

                    // rewrite value
                    $("#colSize").val(colSize);
                    $("#rowSize").val(rowSize);
                    $("#col").text(colSize);
                    $("#row").text(rowSize);
                    $("#bomb").text(0);
                    $("#bombEmbed").text(bombCount);


                    // create bomb table
                    $("#tableContainer").html("");
                    $("<table/>").attr({
                        border: 0,
                        align: "center"
                    }).appendTo("#tableContainer");

                    for (var i = 0; i < rowSize; i++) {
                        var currentRow = $("<tr/>").appendTo("#tableContainer table");

                        for (var j = 0; j < colSize; j++) {
                            var currentCell = $("<td/>").css({
                                //textAlign: "center",
                                //verticalAlign: "middle",
                                //fontSize: "12pt",
                                border: "2px outset black",
                                //width: "20px",
                                //height: "20px",
                                backgroundColor: "gray"
                            }).attr({ x: i, y: j })
                            .html("&nbsp;")
                            //.html(BombSweeper.bombMap[i * colSize + j] ? "X" : "&nbsp;")
                            .click(function(e) {
                                if (e.ctrlKey && $(this).attr("x") && $(this).attr("y")) {
                                    if ($(this).attr("marked") == "true") {
                                        $(this).removeAttr("marked")
                                        .css({
                                            color: "black",
                                            fontWeight: "normal"
                                        })
                                        .html("&nbsp;");
                                        $("#bomb").text(parseInt($("#bomb").text(), 10) - 1);
                                    } else {
                                        if (parseInt($("#bomb").text(), 10) < parseInt($("#bombEmbed").text(), 10)) {
                                            $(this).css({
                                                color: "blue",
                                                fontWeight: "bold"
                                            })
                                            .attr("marked", true)
                                            .text("P");
                                            $("#bomb").text(parseInt($("#bomb").text(), 10) + 1);
                                            if (parseInt($("#bomb").text(), 10) == parseInt($("#bombEmbed").text(), 10)) {
                                                var marked = $("#tableContainer td[marked]");
                                                for (var i = 0; i < marked.length; i++) {
                                                    if (!BombSweeper.isBomb($(marked.get(i)).attr("x"), $(marked.get(i)).attr("y"))) return;
                                                }

                                                // unbind all click event
                                                $("#tableContainer td").unbind("click");
                                                // finish game
                                                if (confirm("Congratulations!\nYou finish this game.\nWould you like to play again?")) {
                                                    $("#generateTable").click();
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    if (!$(this).attr("marked") && $(this).attr("x") && $(this).attr("y")) {
                                        var x = parseInt($(this).attr("x"), 10);
                                        var y = parseInt($(this).attr("y"), 10);
                                        var tips = BombSweeper.getTips(x, y);

                                        if (tips == -1) {
                                            $(this).css({
                                                border: "2px inset black",
                                                backgroundColor: "transparent",
                                                color: "red"
                                            })
                                            .text("X")
                                            .removeAttr("x")
                                            .removeAttr("y");

                                            // unbind all click event
                                            $("#tableContainer td").unbind("click");
                                            if (confirm("Sorry, you lose this game.\nWould you like to play again?")) {
                                                $("#generateTable").click();
                                            }
                                        } else {
                                            $(this).css({
                                                border: "2px inset black",
                                                backgroundColor: "white"
                                            })
                                            .html(tips == 0 ? "&nbsp;" : tips)
                                            .removeAttr("x")
                                            .removeAttr("y");

                                            // extended action
                                            if (tips == 0) {
                                                $("#tableContainer td[x][y][marked!='true']")
                                                .filter(function(index) {
                                                    return ($(this).attr("x") == x - 1 || $(this).attr("x") == x || $(this).attr("x") == x + 1)
                                                    && ($(this).attr("y") == y - 1 || $(this).attr("y") == y || $(this).attr("y") == y + 1);
                                                })
                                                .each(function(index) {
                                                    $(this).click();
                                                });
                                            }
                                        }
                                    }
                                }
                            })
                            .appendTo(currentRow);
                        }
                    }
                }
            });
        });
    </script>

</head>
<body>
    <div style="text-align: center;">
        <input id="colSize" type="text" size="5" value="10" />
        x
        <input id="rowSize" type="text" size="5" value="10" />
        @
        <input id="bombCount" type="text" size="5" value="30" />
        <input id="generateTable" type="button" value="New Game" />
    </div>
    <div style="text-align: center;">
        Size of grid : <span id="col">0</span>/ <span id="row">0</span>
        <br />
        Marked bombs : <span id="bomb">0</span> / <span id="bombEmbed">0</span>
    </div>
    <div id="tableContainer">
    </div>
    <div style="text-align: center; font-size: 9pt;">
        (To mark bomb, press ctrl key and click.)</div>
</body>
</html>