[How to] Block specific Websites with GeckoFX in C#

Have you ever heard of WebBrowser in .NET Application? If yes, then you know how bad is the Default WebBrowser in .NET. In Internet we can find many alternative for the .NET's Default WebBrowser. There are many, such as CefSharp or GeckoFX,....

In this post I will introduce to you GeckoFX and I will show you how to build your first Gecko Browser program with the Blocking Feature.

GeckoFX is the open source component to embedding the Gecko rendering engine from Mozilla Firefox to .NET application.

To download GeckoFX-Source (current version is GeckoFX-45) go to the GeckoFX BitBucket Wikipage https://bitbucket.org/geckofx/geckofx-45.0/wiki/Home

Or we can use "NuGet-Package Manager" to install GeckoFX-45 to your project as the picture below:

NuGet install GeckoFX


After that we'll see the Firefox folder and the 2 new Dependencies in the Project Explorer Window:

Firefox GeckoFX Project explorer

First we have to declare Gecko namespace
using Gecko;

Just one more setting, select all files from the Firefox folder and select "Always copy" or "Copy when newer" to "Output folder" as below:

Copy dll files to Output folder


Now we can use the GeckoWebbrowser. Just declare the GeckoWebBrowser variable and load all the needed dll files as the picture below:

Init GeckoFX - Declare GeckoWebBrowser variable


What is function InitBrowser? It a function to configure the WebBrowser and to add the Browser into the Form

Configure GeckoWebBrowser


The important Thing to use Blocking feature of GeckoFX is we have to enable HttpActivityObserver, so we set here UseHttpActivityObserver = true.

After UseHttpActivityObserver is enabled, we need to add a Event-Handler, in this case is ObserveHttpModifyRequest-Event from GeckoWebBrowser.

When the WebBrowser send any request to any URL the event ObserveHttpModifyRequest will be fired. We can get the URL of the request by using variable request and convert it to string with request.Uri.ToString(). In this example, I block all the Request that contain "google.com" in the URL.

Here is the code:
        public GeckoWebBrowser webBrowser;
        public Form1()
        {
            InitializeComponent();
            Xpcom.Initialize("Firefox"); // Init all Gecko DLL files in the Firefox folder inside the Project
            InitBrowser();
        }

        // Init Browser
        void InitBrowser()
        {
            webBrowser = new GeckoWebBrowser { Dock = DockStyle.Fill, // Fill all the Form
                UseHttpActivityObserver = true // This is to enable HttpActivityObserver
            };
            webBrowser.ObserveHttpModifyRequest += (obj, request) => {
                if (!request.Uri.ToString().Contains("google.com")) // if the request URL contains string "google.com"
                {
                    request.Cancel = true; // Cancel the request to the URL
                }
            };
            this.Controls.Add(webBrowser);
        }
When you have any question, feel free to leave a commend, I will answer as soon as I can. Have fun coding!

P/S: In this tutorial I installed and use GeckoFX-45 32 bit, so when build the project I have to choose x32 Platform (It will give error when you try to compile to 64 bit Application, in case you want to build 64 bit Application, just install GeckoFx45.64 in the NuGet-Package Manager)

Develop your program in CLOUD

Are you a developer? If yes, then maybe you are used to dev program on your local computer, it take time to prepare an environment to dev. I found out some SAAS website that provide a coding environment online for FREE, that's means you don't need to install IDE, and you can code everywhere with device that can access the internet, also you can customize the environment to what you need (customize the cloud VM with full sudo access).

These service are:
- Koding : https://koding.com
- Cloud9 (C9) : https://c9.io
- Codeanywhere : https://codeanywhere.com

Using these service, you can develop your website/program with any programming language/framework you want such as PHP, Python, Perl, Ruby, Wordpress, NodeJS...
Try them and leave comment of your opinion about these service.

How to Open Visual Studio 2010 Project file on Visual Studio 2008

First, you need to convert visual studio 2010 solution file (.sln) to visual studio 2008 solution file (.sln) simply follow these steps:
  1. in .sln file replace:
    1.  "Format Version 11.00" to "Format Version 10.00"
    2. "# Visual Studio 2010" to "# Visual Studio 2008"
  2. in .csproj replace
    1. "ToolsVersion=''4.0''" to "ToolsVersion=''3.5''"
    2. "Microsoft\VisualStudio\v10.0" to "Microsoft\VisualStudio\v9.0"
After that, you can Open the Solution file and work normally.
 
Note: with the WEB Project, in Web.Config file, you should remove the line "<compilation debug="true" targetFramework="4.0" />" to run on Visual Studio 2008

Java - connect to SQL Server using JDBC ODBC in win 64bit

When I do my School Project (Create an Java App connect to an DB), I found it's can't be done in windows 64bit, so I search the Internet and I found my own way to finish it in Windows 64bit. Just follow these step below.

Step 1
Run the 32-bit odbc driver using
WinKey+R, then copy-paste the below command
C:\Windows\SysWOW64\odbcad32.exe

Step 2
Make a dsn named “SQLDB” or whatever name you want to.

Step 3
Create a new project in eclipse.

Step 4
Change the jre to the java installed inside
C:\Program Files (x86)\java
Use this as “JRE System Library”


Step 5
Use the below code to connect to connect to SQL Server (It's a SQL_Connection class)
import java.sql.*;


public class SQL_Connection {
    public static Connection GetConnection()
    {
        try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection connect=DriverManager.getConnection("jdbc:odbc:SQLDB");
     return connect;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    public static int ExecuteQueryString(String querystring)
    {
        try{
            Statement st= GetConnection().createStatement();
            return st.executeUpdate(querystring);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return -1;
    }
    public static ResultSet getResultSet(String querystring)
    {
        try{
            Statement st= GetConnection().createStatement();
            ResultSet rs= st.executeQuery(querystring);
            return rs;
        }
        catch(Exception ex)
        {
            //ex.printStackTrace();
            return null;
        }
    }
    public static void Close_Connection(Connection conn)
    {
        try
        {conn.close();}
        catch(Exception ex)
        {}
    }
}
DONE!!!!

How to read emails on mail server ?

First of all there are multiple protocols to retreive mail:
POP3, IMAP, etc...
I suggest you start by familiarizing yourself with the various components that make up an e-mail system.
  • Mail Transfer Agent (Protocol: SMTP)
  • Mail Delivery Agent (Protocols: POP3, IMAP)
  • Mail User Agent (Outlook, Webmail, Thunderbird, your application)
Basically what you are trying to write is a Mail User Agent. A mail user agent has to "fetch" the mails from a Mail Delivery Agent using either POP or IMAP.
This means you will have to learn about these two protocols:
POP3 RFC: http://www.faqs.org/rfcs/rfc1939.html
IMAPv4 RFC: http://james.apache.org/server/rfclist/imap4/rfc2060.txt
Since e-mail communication happens using TCP/IP you will have to learn how to use the classes in the System.Net.Sockets namespace.
Also take a look at the TcpClient class.
Try to understand these concepts first and then I suggest you start out with POP3, this protocol is quite easy. If you have problems then with very specific TcpClient code please update your question or post a new question.
Hope this sets you on the right track.

P/S: I've complete my first program that read emails in Gmail & Download the attachments in selected emails (using VB.NET 2008).

Access data in MS-Access database using Javascript

You know that javascript is a client-side programming language. But if you want to use the local database like MS-Access in your HTML page just using javascript? Here is some code that help you access the data of MS-Access DB in HTML page using JavaScript:

Adding a Record

function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='/\dbName.mdb'");
adoRS.Open("Select * From tblName", adoConn, 1, 3);

adoRS.AddNew;
adoRS.Fields("FieldName").value = "Quentin";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}

Removing a Record

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}

Editing a Record

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);

adoRS.Edit;
adoRS.Fields("FieldName").value = "New Name";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}


Querying data:


var pad = "C:\\My Documents\\data.mdb";
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "SELECT * FROM datatable";
rs.Open(SQL, cn);
if(!rs.bof) {
rs.MoveFirst();
if(!rs.eof) {
document.write("<p>" + rs.fields(1).value + ", ");
document.write(rs.fields(2).value + ", ");
document.write(rs.fields(3).value + ".</p>");
}
}
else {
document.write("No data found");
};
rs.Close();
cn.Close();

Sponsors