Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... 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... 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
  1. <html>
  2. <head>
  3.     <title>JSON Formater</title>
  4.     <style type="text/css">
  5.     body{
  6.         margin: 0px;
  7.         padding: 0px;
  8.         overflow: hidden;
  9.     }
  10.     </style>
  11.     <script type="text/javascript">
  12.         var JSONFormater = {
  13.             indentType: "\t",
  14.             source: false,
  15.             output: "",
  16.             format: function() {
  17.                 if (document.getElementById("inputJSON").value.length == 0) {
  18.                     alert("Empty source.");
  19.                     return;
  20.                 }
  21.                 try {
  22.                     eval("JSONFormater.source = " + document.getElementById("inputJSON").value + ";");
  23.                 } catch (e) {
  24.                     alert("Invalid JSON data.");
  25.                     return;
  26.                 }
  27.                 this.indentType = this.selectedIndent();
  28.                 this.iteratorChilds(this.source, 0, false);
  29.                 this.clearTails();
  30.                 document.getElementById("inputJSON").value = this.output;
  31.                 this.output = "";
  32.             },
  33.             hasMembers: function(obj) {
  34.                 for (var m in obj) return true;
  35.                 return false;
  36.             },
  37.             isArray: function(obj) {
  38.                 return obj.constructor == Array;
  39.             },
  40.             isString: function(obj) {
  41.                 return obj.constructor == String;
  42.             },
  43.             iteratorChilds: function(obj, indent, isMember) {
  44.  
  45.                 if (this.isArray(obj)) {
  46.                     this.output += "[\n";
  47.                     for (var i = 0; i < obj.length; i++) {
  48.                         this.iteratorChilds(obj[i], indent + 1, false);
  49.                     }
  50.                     this.clearTails();
  51.                     this.output += this.currentIndent(indent);
  52.                     this.output += "],\n";
  53.                 } else if (this.hasMembers(obj)) {
  54.                     if (!isMember) this.output += this.currentIndent(indent);
  55.                     this.output += "{\n";
  56.                     for (var e in obj) {
  57.                         this.output += this.currentIndent(indent + 1);
  58.                         this.output += (this.isString(e) ? "\"" + e + "\"" : e) + ": ";
  59.                         this.iteratorChilds(obj[e], indent + 1, true);
  60.                     }
  61.                     this.clearTails();
  62.                     this.output += this.currentIndent(indent);
  63.                     this.output += "},\n";
  64.                 } else {
  65.                     if (!isMember) this.output += this.currentIndent(indent);
  66.                     this.output += this.isString(obj) ? "\"" + obj + "\"" : obj;
  67.                     this.output += ",\n";
  68.                 }
  69.             },
  70.             currentIndent: function(ident) {
  71.                 var it = "";
  72.                 for (var i = 0; i < ident; i++) {
  73.                     it += this.indentType;
  74.                 }
  75.                 return it;
  76.             },
  77.             clearTails: function() {
  78.                 if (this.output.length > 2) this.output = this.output.substring(0, this.output.length - 2) + "\n";
  79.             },
  80.             selectedIndent: function() {
  81.                 switch (document.getElementById("indentType").value) {
  82.                     case "1":
  83.                         return "\t";
  84.                     case "2":
  85.                         return "  ";
  86.                     case "3":
  87.                         return "   ";
  88.                     case "4":
  89.                         return "    ";
  90.                     case "8":
  91.                         return "        ";
  92.                 }
  93.             }
  94.         };
  95.         window.onresize = function(){
  96.             document.getElementById("inputJSON").style.height = document.body.offsetHeight-30;
  97.         };
  98.     </script>
  99.  
  100. </head>
  101. <body onload="document.getElementById('inputJSON').style.height = document.body.offsetHeight-30;">
  102.     <textarea id="inputJSON" style="width: 100%;" rows="15" style="overflow: both;"></textarea>
  103.     <div>
  104.         <input type="button" value="format" onclick="JSONFormater.format();" />
  105.         <select id="indentType">
  106.             <option value="1" selected="selected">indent with a tab character</option>
  107.             <option value="2">indent with 2 spaces</option>
  108.             <option value="3">indent with 3 spaces</option>
  109.             <option value="4">indent with 4 spaces</option>
  110.             <option value="8">indent with 8 spaces</option>
  111.         </select>
  112.     </div>
  113. </body>
  114. </html>
Url rewrite using IHttpModule
UrlRewriter.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Text.RegularExpressions;
  6.  
  7. /// <summary>
  8. /// UrlRewriter 的摘要描述
  9. /// </summary>
  10. namespace idv.modules
  11. {
  12.     public class UrlRewriter : IHttpModule
  13.     {
  14.  
  15.         #region IHttpModule 成員
  16.  
  17.         public void Dispose()
  18.         {
  19.             //throw new NotImplementedException();
  20.         }
  21.  
  22.         public void Init(HttpApplication context)
  23.         {
  24.             context.BeginRequest += new EventHandler(context_BeginRequest);
  25.         }
  26.  
  27.         void context_BeginRequest(object sender, EventArgs e)
  28.         {
  29.             HttpApplication application = sender as HttpApplication;
  30.             HttpContext context = application.Context;
  31.  
  32.             if (context.Request.Path.EndsWith("TranslatedPage.html", StringComparison.CurrentCultureIgnoreCase))
  33.             {
  34.                 string currentUrl = context.Request.Path;
  35.  
  36.                 // rewrite url [http://www.domain.com/actionValue/TranslatedPage.html]
  37.                 // to [http://www.domain.com/TranslatedPage.aspx?action=actionValue]
  38.                 string action = Regex.Match(currentUrl, "/([^/]+)/TranslatedPage.html", RegexOptions.IgnoreCase).Groups[1].Value;
  39.                 context.RewritePath("~/TranslatedPage.aspx?action=" + action);
  40.             }
  41.         }
  42.  
  43.         #endregion
  44.     }
  45. }
Web.config
  1. <httpModules>
  2.     <add name="UrlRewriter" type="idv.modules.UrlRewriter"/>
  3. </httpModules>
Bomb Sweeper
  1. <html>
  2. <head>
  3.     <title>Bomb Sweeper</title>
  4.     <style type="text/css">
  5.         body
  6.         {
  7.             background-color: #95B7DF;
  8.         }
  9.         #tableContainer td
  10.         {
  11.             width: 20px;
  12.             height: 20px;
  13.             text-align: center;
  14.             vertical-align: middle;
  15.             -moz-border-radius: 5px;
  16.             -khtml-border-radius: 5px;
  17.             -webkit-border-radius: 5px;
  18.         }
  19.     </style>
  20.  
  21.     <script type="text/javascript" src="jquery.js"></script>
  22.  
  23.     <script type="text/javascript">
  24.         var BombSweeper = {
  25.             result: true,
  26.             bombMap: false,
  27.             colSize: 0,
  28.             rowSize: 0,
  29.             locateBomb: function(rowSize, colSize, bombCount) {
  30.                 var bombArr = new Array(colSize * rowSize);
  31.                 var count = 0;
  32.                 var index;
  33.                 while (count < bombCount) {
  34.                     index = Math.floor(Math.random() * bombArr.length);
  35.                     if (!bombArr[index]) {
  36.                         count++;
  37.                         bombArr[index] = true;
  38.                     }
  39.                 }
  40.                 this.colSize = colSize;
  41.                 this.rowSize = rowSize;
  42.                 this.bombMap = bombArr;
  43.             },
  44.             getSurroundedIndex: function(x, y) {
  45.                 var indexArr = [];
  46.                 for (var i = x - 1; i <= x + 1; i++) {
  47.                     if (>= 0 && i < this.colSize) {
  48.                         for (var j = y - 1; j <= y + 1; j++) {
  49.                             if (>= 0 && j < this.rowSize) {
  50.                                 indexArr.push(* this.colSize + j);
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.                 return indexArr;
  56.             },
  57.             getSurroundedBombCount: function(arr) {
  58.                 var count = 0;
  59.                 for (var i in arr) {
  60.                     if (this.bombMap[arr[i]]) count++;
  61.                 }
  62.                 return count;
  63.             },
  64.             getTips: function(x, y) {
  65.                 var _x = parseInt(x, 10);
  66.                 var _y = parseInt(y, 10);
  67.                 if (this.bombMap[_x * this.colSize + _y]) {
  68.                     return -1;
  69.                 } else {
  70.                     return this.getSurroundedBombCount(this.getSurroundedIndex(_x, _y));
  71.                 }
  72.             },
  73.             isBomb: function(x, y) {
  74.                 var _x = parseInt(x, 10);
  75.                 var _y = parseInt(y, 10);
  76.                 return this.bombMap[_x * this.colSize + _y] == true;
  77.             }
  78.         };
  79.         $(document).ready(function() {
  80.             $("#generateTable").bind("click", function() {
  81.                 var colSize = parseInt($("#colSize").val(), 10);
  82.                 var rowSize = parseInt($("#rowSize").val(), 10);
  83.                 var bombCount = parseInt($("#bombCount").val(), 10);
  84.  
  85.                 if (isNaN(colSize) || isNaN(rowSize)) {
  86.                     $("#tableContainer").text("Not availible size.");
  87.                 } else {
  88.                     BombSweeper.locateBomb(colSize, rowSize, bombCount);
  89.  
  90.                     // rewrite value
  91.                     $("#colSize").val(colSize);
  92.                     $("#rowSize").val(rowSize);
  93.                     $("#col").text(colSize);
  94.                     $("#row").text(rowSize);
  95.                     $("#bomb").text(0);
  96.                     $("#bombEmbed").text(bombCount);
  97.  
  98.  
  99.                     // create bomb table
  100.                     $("#tableContainer").html("");
  101.                     $("<table/>").attr({
  102.                         border: 0,
  103.                         align: "center"
  104.                     }).appendTo("#tableContainer");
  105.  
  106.                     for (var i = 0; i < rowSize; i++) {
  107.                         var currentRow = $("<tr/>").appendTo("#tableContainer table");
  108.  
  109.                         for (var j = 0; j < colSize; j++) {
  110.                             var currentCell = $("<td/>").css({
  111.                                 //textAlign: "center",
  112.                                 //verticalAlign: "middle",
  113.                                 //fontSize: "12pt",
  114.                                 border: "2px outset black",
  115.                                 //width: "20px",
  116.                                 //height: "20px",
  117.                                 backgroundColor: "gray"
  118.                             }).attr({ x: i, y: j })
  119.                             .html("&nbsp;")
  120.                             //.html(BombSweeper.bombMap[i * colSize + j] ? "X" : "&nbsp;")
  121.                             .click(function(e) {
  122.                                 if (e.ctrlKey && $(this).attr("x") && $(this).attr("y")) {
  123.                                     if ($(this).attr("marked") == "true") {
  124.                                         $(this).removeAttr("marked")
  125.                                         .css({
  126.                                             color: "black",
  127.                                             fontWeight: "normal"
  128.                                         })
  129.                                         .html("&nbsp;");
  130.                                         $("#bomb").text(parseInt($("#bomb").text(), 10) - 1);
  131.                                     } else {
  132.                                         if (parseInt($("#bomb").text(), 10) < parseInt($("#bombEmbed").text(), 10)) {
  133.                                             $(this).css({
  134.                                                 color: "blue",
  135.                                                 fontWeight: "bold"
  136.                                             })
  137.                                             .attr("marked", true)
  138.                                             .text("P");
  139.                                             $("#bomb").text(parseInt($("#bomb").text(), 10) + 1);
  140.                                             if (parseInt($("#bomb").text(), 10) == parseInt($("#bombEmbed").text(), 10)) {
  141.                                                 var marked = $("#tableContainer td[marked]");
  142.                                                 for (var i = 0; i < marked.length; i++) {
  143.                                                     if (!BombSweeper.isBomb($(marked.get(i)).attr("x"), $(marked.get(i)).attr("y"))) return;
  144.                                                 }
  145.  
  146.                                                 // unbind all click event
  147.                                                 $("#tableContainer td").unbind("click");
  148.                                                 // finish game
  149.                                                 if (confirm("Congratulations!\nYou finish this game.\nWould you like to play again?")) {
  150.                                                     $("#generateTable").click();
  151.                                                 }
  152.                                             }
  153.                                         }
  154.                                     }
  155.                                 } else {
  156.                                     if (!$(this).attr("marked") && $(this).attr("x") && $(this).attr("y")) {
  157.                                         var x = parseInt($(this).attr("x"), 10);
  158.                                         var y = parseInt($(this).attr("y"), 10);
  159.                                         var tips = BombSweeper.getTips(x, y);
  160.  
  161.                                         if (tips == -1) {
  162.                                             $(this).css({
  163.                                                 border: "2px inset black",
  164.                                                 backgroundColor: "transparent",
  165.                                                 color: "red"
  166.                                             })
  167.                                             .text("X")
  168.                                             .removeAttr("x")
  169.                                             .removeAttr("y");
  170.  
  171.                                             // unbind all click event
  172.                                             $("#tableContainer td").unbind("click");
  173.                                             if (confirm("Sorry, you lose this game.\nWould you like to play again?")) {
  174.                                                 $("#generateTable").click();
  175.                                             }
  176.                                         } else {
  177.                                             $(this).css({
  178.                                                 border: "2px inset black",
  179.                                                 backgroundColor: "white"
  180.                                             })
  181.                                             .html(tips == 0 ? "&nbsp;" : tips)
  182.                                             .removeAttr("x")
  183.                                             .removeAttr("y");
  184.  
  185.                                             // extended action
  186.                                             if (tips == 0) {
  187.                                                 $("#tableContainer td[x][y][marked!='true']")
  188.                                                 .filter(function(index) {
  189.                                                     return ($(this).attr("x") == x - 1 || $(this).attr("x") == x || $(this).attr("x") == x + 1)
  190.                                                     && ($(this).attr("y") == y - 1 || $(this).attr("y") == y || $(this).attr("y") == y + 1);
  191.                                                 })
  192.                                                 .each(function(index) {
  193.                                                     $(this).click();
  194.                                                 });
  195.                                             }
  196.                                         }
  197.                                     }
  198.                                 }
  199.                             })
  200.                             .appendTo(currentRow);
  201.                         }
  202.                     }
  203.                 }
  204.             });
  205.         });
  206.     </script>
  207.  
  208. </head>
  209. <body>
  210.     <div style="text-align: center;">
  211.         <input id="colSize" type="text" size="5" value="10" />
  212.         x
  213.         <input id="rowSize" type="text" size="5" value="10" />
  214.         @
  215.         <input id="bombCount" type="text" size="5" value="30" />
  216.         <input id="generateTable" type="button" value="New Game" />
  217.     </div>
  218.     <div style="text-align: center;">
  219.         Size of grid : <span id="col">0</span><span id="row">0</span>
  220.         <br />
  221.         Marked bombs : <span id="bomb">0</span> / <span id="bombEmbed">0</span>
  222.     </div>
  223.     <div id="tableContainer">
  224.     </div>
  225.     <div style="text-align: center; font-size: 9pt;">
  226.         (To mark bomb, press ctrl key and click.)</div>
  227. </body>
  228. </html>