Saturday, August 23, 2014

Basic Calculator Extension

Hi guyss its time to do some more action. Lets build a calculator extension with HTML JQuery and CSS.Creating chrome extension is no different thing from creating the app but there will be a slight change in manifest file and there is no need to include background.js file.

Step1: Create manifest file

{
  "manifest_version": 2,
  "name": "Calculator",
  "description": "This is a Basic Calculator Extension",
  "version": "1.0",
  "browser_action": {
    "default_icon": "calculator.png",
    "default_popup": "callayout.html"
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
browser_action tells chrome to perform some task when the user clicks it.default_icon is your apps icon.default_popup says about the popup screen to be launched when the user clicks th extension. The last json object says about the security policy of our app.Early Chrome app doesnt allow eval() in the code due to some security reasons.But later on some changes have been made to relax the eval() and some other functions.In order to relax the eval() we have to mention the policy as above.[We use eval() in our app]

Step2: Create Html file

<html>
<head>
<script src="jq.js"></script>
<script src="cal.js"></script>
<style>
table{border-style:solid;border-color:black;border-radius:4pt;border-width:2pt}
td{text-align:center;border:none}
button{background-color:#09F;border-style:none;color:white;height:25pt;width:25pt;font-size:14pt;cursor:pointer;border-radius:3pt}
#bres,#bclr{background-color:#09F;border-style:none;color:white;height:25pt;width:60pt;font-size:14pt;cursor:pointer}
input{height:20pt;width:130pt;font-size:14pt;border-style:solid;border-width:1pt;text-align:right}
div{font-size:14pt}
</style>
</head>
<body>
<table align="center" border="1" cellspacing="5">
<tr><td align="center" colspan="4"><div>
Basic Calculator</div>
</td></tr>
<tr><td align="center" colspan="4"><input id="res" readonly="readonly" type="text" /></td></tr>
<tr><td align="center" colspan="4"><div>
</div>
</td></tr>
<tr>
<td><button id="b1">1</button></td>
<td><button id="b2">2</button></td>
<td><button id="b3">3</button></td>
<td><button id="bs">+</button></td>
</tr>
<tr>
<td><button id="b4">4</button></td>
<td><button id="b5">5</button></td>
<td><button id="b6">6</button></td>
<td><button id="bd">-</button></td>
</tr>
<tr>
<td><button id="b7">7</button></td>
<td><button id="b8">8</button></td>
<td><button id="b9">9</button></td>
<td><button id="bm">*</button></td>
</tr>
<tr>
<td><button id="bl">(</button></td>
<td><button id="b0">0</button></td>
<td><button id="br">)</button></td>
<td><button id="bdiv">/</button></td>
</tr>
<tr>
<td colspan="2"><button id="bres">Result</button></td>
<td colspan="2"><button id="bclr">Clear</button></td>
</tr>
</table>
</body>
</html>
In the above code we have included the external js files(one is jquery library) and used internal style sheet. Step3: Create a javascript file.

$(document).ready(function(){
  $("#b1").click(function(){
    $("#res").val($("#res").val()+"1");
  });
  
  $("#b2").click(function(){
    $("#res").val($("#res").val()+"2");
  });
  
   $("#b3").click(function(){
    $("#res").val($("#res").val()+"3");
  });
  
  $("#b4").click(function(){
    $("#res").val($("#res").val()+"4");
  });
  
  $("#b5").click(function(){
    $("#res").val($("#res").val()+"5");
  });
  
  $("#b6").click(function(){
    $("#res").val($("#res").val()+"6");
  });
   
   $("#b7").click(function(){
    $("#res").val($("#res").val()+"7");
  });
   
   $("#b8").click(function(){
    $("#res").val($("#res").val()+"8");
  });
   
   $("#b9").click(function(){
    $("#res").val($("#res").val()+"9");
  });
   
   $("#b0").click(function(){
    $("#res").val($("#res").val()+"0");
  });
   
  $("#bl").click(function(){
    $("#res").val($("#res").val()+"(");
  });  
    
  $("#br").click(function(){
    $("#res").val($("#res").val()+")");
  });  

  $("#bs").click(function(){
    $("#res").val($("#res").val()+"+");
  });
   
   $("#bd").click(function(){
    $("#res").val($("#res").val()+"-");
  });
   
   $("#bm").click(function(){
    $("#res").val($("#res").val()+"*");
  });
   
   $("#bdiv").click(function(){
    $("#res").val($("#res").val()+"/");
  });
  
   $("#bclr").click(function(){
    $("#res").val("");
  });
   
   $("#bres").click(function(){
    $("#res").val((eval($("#res").val())));
  });
});

We have used some jquery functions in our script go through jquery tutorials in w3schools it will be handy.
Finally now load our extension into the browser in the same way as you have done to load the app previously. This time our extension will appear on the top right corner of the browser. Click on it to launch the extension.


Read More

Chrome Apps

Hi guyss how are you doing its been quite some days since i had posted, as iam busy with some personal work.Okay lets jump into something exciting.Today iam going to show you how to build  simple chrome app s and extensions. I think all you guyss are familiar with google chrome arent you? Ofcourse everyone since it has its own mark in the area of browsing and user interface and HTML5 and CSS3 support over its counterparts.It has lot of cool apps and extensions.

Lets see the basic technical requirements to build a chrome app:
  • HTML
  • JavaScript or JQuery
  • CSS
If you are familiar withe these then you are all set to fire.Okay the structure of the app consists of the following files:
  • html files
  • javascript files(Chrome app only supports external JS files)
  • css files(if necessary. you can also use internal or inline stylesheets too)
  • image files(if necessary)
  • manifest.json(it contains details which describes your app)
  • backrgound.js(it create thes event page and  sets some values to the app like height and width of window etc.)
Note that manifest.json contains info in the form of json data. Json is similar to javascript objects if you are well versed with them then you wont find much difficulty in understanding the manifest file.
Manifest.json and background.js files should be kept along with the source files.
Note that All the files should be kept in a same folder.

HelloWorld Chrome App

Step 1: Create manifest.json

{
  "name": "Hello World",
  "description": "Basic Chrome App.",
  "version": "0.1",
  "manifest_version": 2,
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": { "256": "chrome.png" }
}

}

As you can see in the above script everything is self descriptive you can find the name of the app its version its icon size and name etc.

Step 2: Create backgorund.js file ,ofcourse the name of the file could be anything as you like but following conventions will make your life easier so lets stick to background.js.

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('Helloworld.html', {
    'bounds': {
      'width': 400,
      'height': 500
    }
  });
});

in this we specify the html page to be loaded and set the height and width of the window.

Step 3: Helloworld.html

<html>
  <head>
  </head>
  <body>
    <h1 align="center">HelloWorld iam in Chrome!!</h1>
  </body>
</html>

Here in this app we dont include any CSS or JS files since the aim of our app is to say HelloWorld. Depending upon the necessicity we include those files in our app.

Execution of the app:

Step 1: 
  • Go to chrome://flags.in the address bar.
  • Fin "Experimental Extension APIs", and click its "Enable" link.
  • Relaunch Chrome.


Step 2:
  • Goto setting->tools->extensions and check DeveloperMode.
  • After that click Load unpacked extension button and navigate to your app directory and select it.



Step 3:
Navigate to chrome://apps and there you will find your app and click on that icon to launch your app.




















our app:



If you wish you can upload your app into Chrome webstore by creating developer account and paying a one time registration fee of $5.00.

In my next post i will show you how to build a basic calculator chrome extension. Cheers.
Read More
Designed By Seo Blogger Templates