You are on page 1of 7

06/12/2018 Read Emirates ID in a Web Application - CodeProject

Read Emirates ID in a Web Application

Omar Mallat, 31 Oct 2018

Read Emirates ID in a web application, using JavaScript, chrome extension and desktop application

As of writing this document, the only applicable way to read emirates ID is to have a desktop application. The official SDK provided
by Emirates ID authority included some Java applet examples which will not work anymore on recent browsers. But everyone is
going now to web applications instead of desktop applications, so how can we design a workaround to achieve this?

For security reasons, Java applet is obsolete in recent browsers, and this does not end here. In Chrome, you cannot run a client
EXE from your web application. You cannot access the clipboard... Ok! you cannot directly, but you can using extensions.

Using the Code

We have to use the official SDK, so we need a desktop application; however, using Chrome extension, you can execute the
desktop application in the background. That application will read the Emirates ID data, push the data to the clipboard, then close.
Once closed, the chrome extension will read the clipboard, and send the data back to the webpage.

Look at the image below to understand the general concept: 1/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject

Now, let us start with the solution.

A. Create the Desktop Application

Create a simple console application, that

1. reads Emirates ID
2. serializes it into JSON format
3. pushes it to clipboard
4. then closes

I will not go through the creation of the EXE application, as you can find the source code on github. Also, this article is not to explain
how we read the Emirates ID from a desktop application. However, this application must use the official SDK from the Emirates ID

Code sample:

static void Main(string[] args)
var model = Controller.CreateFromEIDReader();
if (model.HasData)
Clipboard.SetText("NO DATA");

B. Authorize Application with Chrome

Chrome cannot call EXE unless it's pre-authorized. Don't worry, authorization is very simple, and it uses chrome Native Messaging,
so please refer to the official documentation. 2/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject
1. Copy the EXE application, created in the previous step, with all required DLLs to a new folder.
2. Create a json file called
3. Add a Key to the registry to define a unique name of your application, and let it point to a json file. The json file explains
what to run and who can run the application. The easiest way is to create a batch file in the same directory and add the
following code:

REG ADD "HKLM\Software\Google\Chrome\NativeMessagingHosts\"

/ve /t REG_SZ /d "" /f


%~dp0 is the current folder from where you run the command.

So here we are saying:


Hey chrome... you can run the application, and to know how please check the file

So what is there in the

Two values are important to us here:

1. path to specify the EXE file

2. allowed_origins to specify which chrome extension can call this EXE. This is the chrome extension id that you
will create right now.

"name": "",
"description": "chrome extension to read EID",
"path": "EIDSampleConsole.exe",
"type": "stdio",
"allowed_origins": [

C. Create Chrome Extension

The main file in any Chrome Extension is the manifest. Other than that, a Chrome extension can have many other files depending
on what you want to achieve. In our case, I'm using a content_script and a background process. So what are these

The background process is isolated from the web pages opened in Chrome. This means it cannot contact the Chrome tabs
directly. It has to go through the content_script. content_script can contact both the background process
and the pages opened in Chrome. Being isolated, the background process can call an EXE, and read the Clipboard data.
Both functions are not allowed directly in Chrome.


In my case, the background file is eid.js, the content_script is content_script.js, and since I use jquery, I
also add the jquery

Any Chrome extension will have a manifest.json file. It lists the behavior and the permissions of the extension. The most important
permissions required are nativeMessaging and clipboardRead. It's here also where we specify the content_script
files and the background file. You can notice the listing of jquery because I'm using some jquery in the content_script file, and
not only JavaScript. 3/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject

"name": "EID Reader",
"version": "1.0",
"manifest_version": 2,
"description": "Read Emirates ID",

"permissions": [ "contextMenus", "activeTab", "clipboardRead", "nativeMessaging" ],

"icons": {
"16": "eid16.png",
"48": "eid48.png",
"128": "eid128.png"
"background": {
"scripts": [ "eid.js" ]
"content_scripts": [
"matches": [ "http://*/*", "https://*/*", "file://*/*"],
"js": [ "content_script.js", "jquery-3.3.1.js" ],
"all_frames": true,
"run_at": "document_start"

This file is playing a role of translator. It can listen to whatever happens in your web pages, and transfer these actions to the
background process by sending messages. It can also receive messages from background process and transfer it again to the web
pages. Keep in mind that your web pages cannot access the background process directly, and it has to be through
Technically, content_script will contain two listeners: one to receive messages from web pages, and another one to receive
messages from background process.

// Listener to catch the event raised by the webpage button

document.addEventListener("EID_EVENT", function (data) {
// send message to background process to read emirates ID and send back the data
chrome.runtime.sendMessage("test", function (response) {

// Listener to catch the data coming from the background process

chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action == 'EID_DATA') {
//Parse the data and fill the form accordingly
try {
var json = $.parseJSON(msg.response);
$(json).each(function (i, val) {
$.each(val, function (key, value) {
if (key == 'EIDNumber')
if (key == 'Name')
if (key == 'Email')
if (key == 'PassportNumber')
catch (e) {
var error = "error" + e;

eid.js 4/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject
This is where we do the main processes. Here, we will call the EXE application, and here is where we read the clipboard. Here is
also where we send the data back to content_script, which will parse it and push it to web pages.

Technically, this will have a listener to receive messages from content_script. Once received, it calls the EXE application
asynchronously, and will wait until the EXE finish his task (Read Emirates ID, and put it in clipboard). Once finished, an event will be
fired, to read the clipboard and send it to content_script to parse it and push it to the web page.

var port = null;

var tabId = null;
/* listener for messages coming from the content_scrip */
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {;
var hostName = "";
port = chrome.runtime.connectNative(hostName);


function onDisconnected() {
port = null;

function SendResponse() {
//create a textarea, focus on it,
//and make a "paste" command to get the clipboard,
//then send the pasted value back to the content_script
bg = chrome.extension.getBackgroundPage();
bg.document.body.innerHTML = ""; // clear the background page
var helper = null;
if (helper == null) {
helper = bg.document.createElement("textarea"); = "absolute"; = "none";

//Focus the textarea;

// perform a Paste in the selected control, here the textarea


// Send data back to content_script

{ action: "EID_DATA", response: helper.value }, function (response) { });

Add Extension to Chrome

1. Open Chrome and type chrome://extensions/
2. Press on Load unpacked
3. Select the extension folder
4. Chrome will load the extension and give it an id.


Remember to copy the extension id and put it in the file to authorize this extension to
execute the exe application.

Test the solution

Now create a web page, add a button that raise an event, and add few inputs to be filled by the data captured from Emirates ID.

<div class="form-group"> 5/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject
<a class="btn btn-primary" href="javascript:readeid();">Read EID</a>
<div class="form-group">
<label for="txtNumber">Number</label>
<input type="text" class="form-control" id="txtNumber" placeholder="Number">
<div class="form-group">
<label for="txtName">Name</label>
<input type="text" class="form-control" id="txtName" placeholder="Name">
<div class="form-group">
<label for="txtEmail">Email address</label>
<input type="email" class="form-control" id="txtEmail" placeholder="Email">
<div class="form-group">
<label for="txtPassport">Passport</label>
<input type="text" class="form-control" id="txtPassport" placeholder="Passport">

var readeid = function () {
var event = document.createEvent('Event');

Remember, this is a workaround, not a native solution. Native solution must come from the SDK developers.

For more information, please visit the project on github.

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omar Mallat .NET Programmer
Web Developer
United Arab Emirates

Comments and Discussions

0 messages have been posted for this article Visit
Web-Application to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Omar Mallat
Web01 | 2.8.181205.1 | Last Updated 1 Nov 2018 Everything else Copyright © CodeProject, 1999-2018 6/7
06/12/2018 Read Emirates ID in a Web Application - CodeProject 7/7