Understanding Javascript Behaviors in Web Pages by Visually Exploring the Browser
Total Page:16
File Type:pdf, Size:1020Kb
FireInsight: Understanding JavaScript Behaviors in Web Pages by Visually Exploring the Browser by Steven Xinyue Gao B.Sc., Dalhousie University, 2004 A THESIS SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF Master of Science in THE FACULTY OF GRADUATE STUDIES (Computer Science) THE UNIVERSITY OF BRITISH COLUMBIA (Vancouver) December 2009 c Steven Xinyue Gao, 2009 Abstract JavaScript is one of the most important and prevalent programming languages today. It is the language of the web browser and it has seen a surge in active devel- opment since the rise of the Ajax approach for developing highly interactive web ap- plications. Often misunderstood as a simple scripting language, JavaScript is in fact powerful and expressive. As web applications have grown in sophistication, so have their user interfaces, which are predominately implemented as client-side JavaScript. This growth in complexity has resulted in a vast array of JavaScript frameworks, best practices and design patterns. Since JavaScript applications involve creating user interfaces, adopting a visual development approach would seem a natural choice to help manage complexity and improve program understanding. One possible ap- proach is to allow the developer to visually relate semantically meaningful elements on a web page to the actual JavaScript source code that implements its behavior. This can be accomplished by leveraging context information during JavaScript ex- ecution. Additionally, control-flow graphs are dynamically generated to help the developer understand how a series of function calls might be related based on events triggered in the user interface. Inspired by work from Li and Wohlstadter, we im- plement the above approach by creating a program called FireInsight. It integrates with the Mozilla Firefox web browser and the Firebug development tool. We use an HTTP proxy to instrument JavaScript source code with our own analysis code. We analyze this approach and evaluate its effectiveness by applying FireInsight to an open-source web application called Java Pet Store 2.0. ii Contents Abstract ...................................... ii Contents ...................................... iii List of Figures .................................. v Acknowledgments ................................ vii Chapter 1 Introduction ............................ 1 1.1 ProblemandMotivation......................... 2 1.2 Contributions............................... 5 1.3 ThesisOutline .............................. 6 Chapter 2 Background ............................ 7 2.1 Evolving the Web: Ajax, JavaScript and User Interaction ...... 10 2.1.1 Iteration 1: Classic Web Application Model . 10 2.1.2 Iteration 2: Ajax Web Application Model . 13 2.2 The Client-Side: JavaScript, HTML, CSS and the DOM . 17 2.2.1 HTML............................... 17 2.2.2 CSS ................................ 19 2.2.3 JavaScriptandtheDOM . 23 2.3 TheFront-endDeveloper . 29 2.4 Program Understanding and Software Maintenance . 32 iii 2.5 RelatedWork............................... 34 2.5.1 JavaScript Frameworks and Programming Tools . 35 2.5.2 Visual Programming Tools . 38 2.5.3 JavaScript Instrumentation . 45 2.5.4 ScriptInsight........................... 48 Chapter 3 Methodology ........................... 51 3.1 Understanding the UI-JavaScript Mapping Problem . 52 3.2 Motivating Example: Java Pet Store 2.0 . 55 3.3 Extending Script Insight to Improve Program Understanding . 63 Chapter 4 Design and Implementation .................. 70 4.1 FireInsightArchitectureOverview . 71 4.2 Implementation Assumptions . 74 4.3 Firefox,FirebugandFireInsight . 75 4.3.1 DOM Mutation Graph and MxGraph . 78 4.4 JavaScript Instrumentation using an HTTP Proxy . 79 4.5 KnownLimitations............................ 83 Chapter 5 Results and Evaluation ..................... 87 5.1 FireInsightEvaluation . 88 5.1.1 Case Study: RSS News Feed . 88 5.1.2 Case Study: Catalog Browser Info Pane . 96 5.1.3 Case Study: Pet Info Overlay Pop-up . 108 5.2 ChallengesandDrawbacks. 114 5.2.1 JavaScriptFrameworks . 114 5.2.2 JavaScript Instrumentation . 117 Chapter 6 Conclusions and Future Work ................ 119 Bibliography ................................... 121 iv List of Figures 2.1 Iteration 1: A Classic Web Application Architecture . ...... 11 2.2 Client-Server Communication for the Classic Web Application Model 12 2.3 Iteration 2: An Interactive Application Architecture. ......... 15 2.4 ASimpleHTMLDocument . 18 2.5 Screenshot of the HTML Document . 19 2.6 AnHTMLDocumentwithCSScode . 21 2.7 Screenshot of the HTML Document with CSS . 23 2.8 An HTML Document with CSS code and JavaScript code . 24 2.9 HTMLandtheDOM .......................... 25 2.10 Screenshot of the HTML Document with CSS and JavaScript . 28 2.11 Mapping Behavior and Presentation of Web Page, from Browser to Implementation.............................. 30 2.12 AscreenshotofAdobeDreamweaver . 39 2.13 AscreenshotofFirebug . 42 3.1 Mapping Behavior of Web Page, from Browser to Implementation . 53 3.2 JavaPetStore2.0-RSSNewFeedFeature . 57 3.3 RSSNewsFeedDOMElement . 59 3.4 RSSNewsFeedJavaScriptBehavior . 60 3.5 FirebugHTMLInspectionMode . 61 3.6 GraphicalModelofExecutionContext . 62 v 3.7 ExecutionStack ............................. 64 3.8 JavaScript Instrumentation through Proxy . 67 4.1 FireInsightArchitecture . 72 4.2 FireInsightPlug-in ............................ 76 4.3 FireInsightHTTPProxy. 79 5.1 FireInsight Attribute View: RSS News Feed . 90 5.2 FireInsight Source Code View: RSS News Feed . 91 5.3 FireInsight Event Handler View and DMG View: RSS News Feed . 93 5.4 Browser View and FireInsight DMG View: RSS News Feed . 95 5.5 FireInsight Attribute View: RSS News Feed Part 2 . 97 5.6 FireInsightDMGView: RSSNewsFeedPart2 . 98 5.7 Case Study: Catalog Browser Info Pane . 99 5.8 FireInsight Attribute View: Catalog Browser Info Pane . 101 5.9 FireInsight Event Handler and DMG Views Part 1: Catalog Browser InfoPane ................................. 103 5.10 FireInsight Event Handler and DMG Views Part 2: Catalog Browser InfoPane ................................. 104 5.11 Case Study Final Result: Catalog Browser Info Pane . 107 5.12 Case Study: Pet Info Overlay Pop-up . 108 5.13 JavaScript Bug: Pet Info Overlay Pop-up . 110 5.14 Inspection Mode: Pet Info Overlay Pop-up . 111 5.15 Event Handlers: Pet Info Overlay Pop-up . 113 5.16 DMGView: PetInfoOverlayPop-up . 115 5.17 Handling JavaScript Frameworks . 116 vi Acknowledgments Thanks to my supervisor Eric Wohlstadter for the thoughtful advice and diligence in guiding me towards the completion of this thesis. Thanks to Loyal Chow for the many interesting discussions regarding JavaScript and web development in general. Thank you Kris De Volder for taking the time to be on my examining committee. Thanks to my parents for their love and support, without which I would never have reached this point in my academic career. Finally, thanks to my lovely Chenoa, for supporting me through all the ups and downs in finishing my Masters degree. vii Chapter 1 Introduction Web applications have evolved dramatically over the past decade. One component of the web application which has seen significant technical advancements is the user interface (UI). The widespread adoption of web programming specifications such as JavaScript and the W3C Document Object Model (DOM), gave developers the ability to create increasingly sophisticated UIs. As a result, web applications can now provide seamless and highly interactive user experiences, similar to desktop applications. Naturally, this additional interactivity has led to a corresponding rise in the UI’s complexity. Building and maintaining rich web UIs now require a greater development effort. Because JavaScript is the language of the web browser, most of the development effort is spent on JavaScript programming. As the language matured, the development community has produced numer- ous frameworks, programming tools, best practices, and design patterns to help make JavaScript applications easier to build. Frameworks, such as Prototype and Dojo [24], provide reusable JavaScript libraries as well as a system for architect- ing the JavaScript code. Best practices, such as decoupling JavaScript code from presentation code (CSS), creating build and deployment procedures for JavaScript, and compressing JavaScript code for production environments, provide a means to manage complexity and improve the performance of the UI [33]. Software, such as 1 JSLint [4] and Aptana [2], provide invaluable assistance to help developers avoid the weaknesses of the language. Other tools such as Firebug [14] and Dreamweaver [1], enable developers to program the UI using visual and interactive techniques. 1.1 Problem and Motivation However, there is currently very little research or progress in the area of JavaScript program understanding. Program understanding represents the knowledge of how the source code implements an application’s behavior. Within the domain of UI programming, this knowledge corresponds to an abstract mapping that connects the source code to the actual UI behavior that occurs in the browser. Program understanding is crucial for software maintenance. Many of to- day’s most popular interactive web applications, such as Google Maps, Facebook, Twitter, and Gmail, have constantly evolving UI components. Because these in- terfaces are implemented in JavaScript, fixing and augmenting existing JavaScript code are common activities within web development. Many software projects face constantly