Monday, March 13, 2017

Create an Animation Using Ajax

ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient, more interactive and highly-personalized Web experiences that work across all the most popular browsers.

Ajax is not a programming language or a tool, but a concept. Ajax is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh.

Ajax is “the method of exchanging data with a server, and updating parts of a web page - without reloading the entire page.

Let's have a look at Ajax,

Animation.aspx
  1. <%@ Page Language="C#" %>  
  2.     <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  3.         <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  4.             <%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>  
  5.                 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.                 <html xmlns="http://www.w3.org/1999/xhtml">  
  7.   
  8.                 <head id="Head1" runat="server">  
  9.                     <title>Control Toolkit</title>  
  10.                     <style type="text/css">  
  11.                     .panelClass {  
  12.                         background-color: lime;  
  13.                         width: 300px;  
  14.                     }  
  15.                     </style>  
  16.                     <script type="text/javascript">  
  17.                     function pageLoad()  
  18.                     {  
  19.                         var ae = $find("ae");  
  20.                         var animation = '{"AnimationName":"Sequence","AnimationChildren":[{"AnimationName":"EnableAction","Enabled":"false","AnimationChildren":[]},{"AnimationName":"Parallel","AnimationChildren":[{"AnimationName":"FadeOut","Duration":"1.5","Fps":"24","AnimationTarget":"Panel1","AnimationChildren":[]},{"AnimationName":"Resize","Width":"1000","Height":"150","Unit":"px","AnimationTarget":"Panel1","AnimationChildren":[]}]}]}';  
  21.                         ae.set_OnClick(animation);  
  22.                         ae.OnClick();  
  23.                     }  
  24.                     </script>  
  25.                 </head>  
  26.   
  27.                 <body>  
  28.                     <form id="form1" runat="server">  
  29.                         <asp:ScriptManager ID="ae" runat="server"> </asp:ScriptManager>  
  30.                         <div>  
  31.                             <asp:Panel ID="Panel1" CssClass="panelClass" runat="server"> ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient, more interactive and highly-personalized Web experiences that work across all the most popular browsers.  
  32.                                 <br /> ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient, more interactive and highly-personalized Web experiences that work across all the most popular browsers.  
  33.                                 <br /> ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient, more interactive and highly-personalized Web experiences that work across all the most popular browsers.  
  34.                                 <br /> </asp:Panel>  
  35.                             <input type="button" id="Button1" runat="server" value="Launch Animation" />  
  36.                             <cc1:AnimationExtender ID="AnimationExtender2" runat="server" TargetControlID="Button1"> </cc1:AnimationExtender>  
  37.                         </div>  
  38.                     </form>  
  39.                 </body>  
  40.   
  41.             </html>  
Guys, Please download Ajax Control Toolkit if you don't have already. 

Keep exploring. If you have any doubt, please feel free to ask me.

Wednesday, March 1, 2017

Read WhatsApp Web DOM Elements Using Chrome Extension

A Chrome extension gives Image Paths and Captions of Images which are posted in a WhatsApp Group.

Writing a Chrome extension can be a bit tricky initially, due to its API and the way you have to structure your code. 


WhatsApp Web


In this post, I want to show how you can write a simple extension that reads the Dom Elements of the WhatsApp Web page once it’s loaded. The whole lot is in Javascript, which gives you easy access to the DOM.

Features of Chrome Extension (Information Display in Console)
  • Image Paths are displayed.
  • Image Captions are displayed.
  • The Image who has no caption to display, for that "No Caption" Text is displayed.
  • Image Paths and Captions are displayed in a consecutive manner. So that End User can easily find out the Caption with respect to the Image.
Architecture of an Extension

  • Background page - not visible by the user. A long running script that handles the state of your extension. Communicates with other parts of the extension with messaging.
  • Event page - the same as a background page, but only executes in response to events you define. Preferred to background pages as they are less resource intensive.
  • Options page - UI page that allows a user to set options. An HTML page that references javascript files.
  • Browser actions/page actions - an icon either in the extension bar or the Omnibox respectively. Clicking on the icon will show an UI tab which is an HTML page that can reference javascript. These pages cannot communicate with the website resources/DOM, it needs to message the content script to do that.
  • Content scripts - javascript only. What is used to modify the page itself? The only part that can access and modify the DOM but not code loaded by the website. Files are run once for each page that matches the manifest. All the JS files in the list are loaded so you can use libraries just by including it. This is what we’ll use for our extension.
There are plenty more elements that can be used in an extension, but this list covers the core that will get most of your extension built and most other parts are for specific tasks.
Permissions
When users see ‘This extension can see and modify data on all pages’, they get nervous. So it’s best to limit where your extension will be active by explicitly setting permissions in the manifest.
You’ll also need to specify what pages your content scripts will match and run on. 
[manifest.json]
"permissions": [
    "tabs", "http://www.google.com/*"
  ],
Writing your own extension
Let’s start with the manifest. We need to state which version it is, if you’re uploading a new version to the web store, the version number must be greater than the existing one. Don’t confuse this with the manifest_version, which is always 2 and is not extension specific.
Create a new folder called ‘WhatsAppWeb’ then a new file called ‘manifest.json’ inside it and copy the following into it.
[manifest.json]
{   "manifest_version": 2,
     "name": "WhatsAppWeb",
    "description": "This extension gives Image Paths and Captions of Images which was Posted in a WhatsApp Group.",
    "version": "1.1",
    "icons": {
    "16": "daisy_16.png",
    "48": "daisy_48.png",
    "128": "daisy_128.png"
    },
    "content_scripts": [
        {"matches": ["https://web.whatsapp.com/*"],
            "js": ["jquery-2.2.0.min.js", "whatsappweb.js"],
            "run_at": "document_end" } ] 
}
You must supply 3 icons in sizes 16, 48 and 128 px. These are used on the extension page and the toolbar.
You can now load your extension into Chrome. Go to Window -> Extensions to open the extensions page. Tick developer mode on the top right which allows you to load your own extensions. Click the ‘Load unpacked extension’ button and then select the ‘WhatsAppWeb’ directory. You should now have something that looks like this:

To modify a page we need a content script. Now save a new file called whatsappweb.js and the following as the content:

[whatsappweb.js]

document.addEventListener("DOMNodeInserted", function (event) {
    var tempHTML = "<html>" + $("html").html() + "</html>";
    loadData();
});

function loadData() {
    var images = [];
    var caption = [];
    
    $("#main .message-list .msg .bubble-image img").each(function () {
        images.push($(this).attr('src'));
    })
    console.log(images);
    
    $("#main .message-list .msg .bubble-image").each(function (i) {
        if ($(this).children('.image-caption').length > 0 &&      $(this).children('.image-caption').length !== 'undefined') 
 {
         caption.push($(this).children('.image-caption').children('.emojitext').text());
        } 
        else {
          caption.push("No Caption");
        }
    })
    console.log(caption);
}

Go back to the extensions page and hit reload or press Ctrl + R (this is very important. If you ever wonder why nothing changed even though you updated the code this is probably the reason).

Go to a WhatsApp Web. Use WhatsApp on your phone to scan the code. Once you have done with it, your WhatsApp screen will be visible on your Desktop/Laptop Screen. 

Create a dummy group says for example "Test", add some participants in it. Now post a couple of images with captions as well as without captions in the same group. Ask Group Participants to post the same. 

Now press an F12 key, go to Console Tab. You get the following output.


Image Paths and Captions are displayed in a sequential manner. So that End User can easily find out the Caption with respect to the Image.

In this way, you can read WhatsApp Web Dom Elements Using Chrome Extension. I am attaching Source Code for your reference.

Source Code

Happy learning..:)