<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mozilla Hacks - the Web developer blog</title>
	<atom:link href="https://hacks.mozilla.org/feed/" rel="self" type="application/rss+xml" />
	<link>https://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Tue, 17 Dec 2013 18:02:56 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>Split console, pretty-print minified JS and more &#8211;  Firefox Developer Tools Episode 28</title>
		<link>https://hacks.mozilla.org/2013/12/split-console-pretty-print-minified-js-and-more-firefox-developer-tools-episode-28/</link>
		<comments>https://hacks.mozilla.org/2013/12/split-console-pretty-print-minified-js-and-more-firefox-developer-tools-episode-28/#comments</comments>
		<pubDate>Tue, 17 Dec 2013 16:47:02 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Griffiths]]></dc:creator>
				<category><![CDATA[Developer Tools]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23384</guid>
		<description><![CDATA[Just in time for the holidays, the Firefox Developer Tools teams has some excellent new surprises for you to enjoy as you hack on holiday projects! It&#8217;s a pretty long list, so let&#8217;s get started. App Manager The App Manager continues to be a priority for the Devtools team and with Firefox 28, in addition [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Just in time for the holidays, the <a href="https://developer.mozilla.org/en/docs/Tools">Firefox Developer Tools</a> teams has some excellent new surprises for you to enjoy as you hack on holiday projects! It&#8217;s a pretty long list, so let&#8217;s get started.</p>
<h3>App Manager</h3>
<p>The <a href="https://developer.mozilla.org/en-US/Firefox_OS/Using_the_App_Manager">App Manager</a> continues to be a priority for the Devtools team and with Firefox 28, in addition to many minor fixes and improvements the team has added two new major features to assist in mobile development. These two features consist of an additional manifest editor and an embedded toolbox.</p>
<p>The Manifest Editor allows a developer to edit their app manifest directly. Using this editor you can add, edit, or delete fields in your app’s manifest.webapp and save them back to disk.  This feature provides the developer an integrated experience, as the user never has to leave the tools while debugging and improving an application.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/App-Manager-manifest.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/App-Manager-manifest-500.png" alt="" /></a></p>
<p>Also new to the App Manager is the embedded toolbox. Now when you debug your app (either using the Simulator or on device) the toolbox will be opened in the App Manager in a separate vertical &#8216;tab&#8217; that is easily distinguished by the app&#8217;s icon:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/App-Manager-toolbox.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/App-Manager-toolbox-500.png" alt="" /></a></p>
<h2>Web Console</h2>
<p>The biggest change in the Web Console is a feature we call &#8216;split console&#8217;; the web console can now be summoned from any other tool. Using split console mode is easy &#8211; if you&#8217;re working with any other tool and need access to the console, simply hit the Escape key or click on the button to toggle the split console, as you can see in this short screencast:</p>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/G2hyxhPHyXo" frameborder="0" allowfullscreen></iframe></p>
<p>This isn&#8217;t the only improvement to the console though. In addition to split console we&#8217;ve also made the following changes:</p>
<ul>
<li>CSS warnings are off by default &#8211; CSS warnings can often add hundreds of messages to the console as a page loads.</li>
<li>The console will now offer autocomplete suggestions for the current scope the debugger is in. </li>
<li>Message timestamps can be toggled on/off (the default is off)</li>
<li><code>console.exception()</code> and <code>console.assert()</code> have been added to the console api.</li>
<li>We added a dark theme for the console</li>
</ul>
<h2>Debugger</h2>
<p>In the debugger we&#8217;ve added two key features that developers have been asking for. First off we&#8217;ve added the ability to pretty-print minified JS files:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/pretty-print-minified-JS.png" alt="" /></p>
<p>This feature is particularly handy if you minify your JavaScript or need to step through a minified library served from a remote site &#8211; just click on the minification button (picture above) to get a much more nicely formatted version of the file.</p>
<p>In addition to this we&#8217;ve added a feature to the debugger that allows you to inspect the value of a variable while debugging. For any variable, simply hover over it or click on it to bring up a pop-up that displays the current value:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/Inspect-variable-while-debugging.png" alt="" /></p>
<p>The pop-up allows you to inspect not only simple types like the boolean picture above, but also objects and DOM nodes:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/inspect-dom-nodes.png" alt="" /></p>
<h2>Inspector</h2>
<p>Following along from the addition of various tooltips to the Inspector in Firefox 27, we&#8217;ve added some additional features in the inspector:</p>
<ul>
<li>A Color picker tooltip in the rule view</li>
<li>We updated style the style of tooltips to better match the dark and light themes</li>
<li>The display timeout for tooltips was tuned based on your feedback</li>
</ul>
<p>Here&#8217;s a screenshot to give you an idea what the color-picker tooltip looks like:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/color-picker-tooltip.png" alt="" /></p>
<h2>Wrapping up</h2>
<p>That&#8217;s a huge list,  but there were a couple of additional changes I&#8217;d like to call out as well:</p>
<ul>
<li>The Network Monitor now has a clear button, which should be of particular use to developers creating apps that make heavy use of web APIs:
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/network-clear.png" alt="" /></p>
</li>
<li>As a bonus for Add-on or Platform hackers, we&#8217;ve expanded the functionality of the Browser Debugger and re-named it the Browser Toolbox. When you open the toolbox you&#8217;ll see a full range of tools including not only the debugger but also the Console, Inspector, Style Editor, Profiler, Network Monitor and Scratchpad:</li>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/browser-debugger.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/browser-debugger-500.png" alt="" /></a></p>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/split-console-pretty-print-minified-js-and-more-firefox-developer-tools-episode-28/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ember Inspector on a Firefox near you</title>
		<link>https://hacks.mozilla.org/2013/12/ember-inspector-on-a-firefox-near-you-2/</link>
		<comments>https://hacks.mozilla.org/2013/12/ember-inspector-on-a-firefox-near-you-2/#comments</comments>
		<pubDate>Tue, 17 Dec 2013 11:02:35 +0000</pubDate>
		<dc:creator><![CDATA[Luca Greco]]></dc:creator>
				<category><![CDATA[Add-ons]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23182</guid>
		<description><![CDATA[&#8230; or Cross-Browser Add-ons for Fun or Profit Browser add-ons are clearly an important web browser feature, at least on the desktop platform, and for a long time Firefox was the browser add-on authors&#8217; preferred target. When Google launched Chrome, this trend on the desktop browsers domain was pretty clear, so their browser provides an [&#8230;]]]></description>
				<content:encoded><![CDATA[<h2>&#8230; or Cross-Browser Add-ons for Fun or Profit</h2>
<p>Browser add-ons are clearly an important web browser feature, at least on the desktop platform, and for a long time Firefox was the browser add-on authors&#8217; preferred target. When Google launched Chrome, this trend on the desktop browsers domain was pretty clear, so their browser provides an add-on api as well.</p>
<p>Most of the Web DevTools we are used to are now directly integrated into our browser, but they were add-ons not so long time ago, and it&#8217;s not strange that new web developer tools are born as add-ons.</p>
<p>Web DevTools (integrated or add-ons) can motivate web developers to change their browser, and then web developers can push web users to change theirs. So, long story short, it would be interesting and useful to create cross-browser add-ons, especially web devtools add-ons (e.g. to preserve the <i>web neutrality</i>).</p>
<p>With this goal in mind, I chose <a href="https://github.com/tildeio/ember-extension">Ember Inspector</a> as the target for my <strong>cross-browser devtool add-ons</strong> experiment, based on the following reasons:</p>
<ul>
<li>It belongs to an emerging and interesting web devtools family (<strong>web framework devtools</strong>)</li>
<li>It&#8217;s a pretty complex / real world Chrome extension</li>
<li>It&#8217;s mostly written in the same web framework by its own community</li>
<li>Even if it is a Chrome extension, it&#8217;s a webapp built from the app sources using grunt</li>
<li>Its JavaScript code is organized into modules and Chrome-specific code is mostly isolated in just a couple of those</li>
<p></u</p>
<h2>Plan &amp; Run Porting Effort</h2>
<p>Looking into the ember-extension git repository, we see that the add-on is built from its sources using grunt:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_grunt.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_grunt-500x126.png" alt="Ember Extension: chrome grunt build process" width="500" height="126" class="aligncenter size-large wp-image-23370" /></a></p>
<p>The extension communicates between the developer tools panel, the page and the main extension code via message passing:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_devtool.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_devtool-500x290.png" alt="Ember Extension: High Level View" width="500" height="290" class="aligncenter size-large wp-image-23372" /></a></p>
<p>Using this knowledge, planning the port to Firefox was surprisingly easy:</p>
<ul>
<li>Create new Firefox add-on specific code (register a devtool panel, control the inspected tab)</li>
<li>Polyfill the communication channel between the <strong>ember_debug</strong> module (that is injected into the inspected tab) and the devtool ember <strong>app</strong> (that is running in the devtools panel)</li>
<li>Polyfill the missing non-standard <strong>inspect</strong> function, which open the DOM Inspector on a DOM Element selected by a defined Ember View id</li>
<li>Minor tweaks (isolate remaining Chrome and Firefox specific code, fix CSS -webkit prefixed rules)</li>
</ul>
<p>In my opinion this port was particularly pleasant to plan thanks to two main design choices:</p>
<ul>
<li>Modular JavaScript sources which helps to keep browser specific code encapsulated into replaceable modules</li>
<li>Devtool panel and code injected into the target tab collaborate exchanging simple JSON messages and the <strong>protocol</strong> (defined by this add-on) is totally browser agnostic</li>
</ul>
<p>Most of the JavaScript modules which compose this extension were already browser independent, so the first step was to bootstrap a simple Firefox Add-on and register a new devtool panel.</p>
<p>Create a new panel into the DevTools is really simple, and there&#8217;s some useful docs about the topic in the <a href="https://developer.mozilla.org/en-US/docs/Tools/DevToolsAPI">Tools/DevToolsAPI</a> page (work in progress).</p>
<h3>Register / unregister devtool panel</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=dist_firefox_lib_main.js"></script></p>
<p><i>From <a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/main.js">https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/main.js</a></i></p>
<h3>Devtool panel definition</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=define_devtool_panel.js"></script></p>
<p><i>From <a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js#L26">https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js#L26</a></i></p>
<p>Then, moving to the second step, <strong>adapt</strong> the code used to create the message channels between the devtool panel and injected code running in the target tab, using content scripts and the low level content worker from the Mozilla Add-on SDK, which are well documented on the official guide and API reference:</p>
<ul>
<li><a href="https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/content-scripts/index.html">Content Script Guide</a></li>
<li><a href="https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/content/worker.html">SDK/content/workers API</a></li>
</ul>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_adapters.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_adapters-500x262.png" alt="EmberInspector - Workers, Content Scripts and Adapters" width="500" height="262" class="aligncenter size-large wp-image-23362" /></a></p>
<ul>
<li><a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/data/devtool-content-script.js">DevTool Panel Content Script</a></li>
<li><a href="https://github.com/tildeio/ember-extension/blob/master/app/adapters/firefox.js">ember_extension.js FirefoxAdapter</a></li>
<li><a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/data/content-script.js">TargetTab Content Script</a></li>
<li><a href="https://github.com/tildeio/ember-extension/blob/master/ember_debug/adapters/firefox.js">ember_debug.js FirefoxAdapter</a></li>
</ul>
<h3>DevTool Panel Workers</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=dist_firefox_workers.js"></script></p>
<p><i>From <a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js">https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js</a></i></p>
<h3>Inject ember_debug</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=devtool_panel_inject_ember_debug.js"></script></p>
<p><i>From <a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js">https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js</a></i></p>
<p>Finally hook browser specific code needed to activate the DOM Inspector on a defined DOM Element:</p>
<h3>Inspect DOM element request handler</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=devtool_panel_inspect_dom_element.js"></script></p>
<p><i>From <a href="https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js#L178">https://github.com/tildeio/ember-extension/blob/master/dist_firefox/lib/devtool-panel.js#L178</a></i></p>
<h2>Evaluate its features and dive into the exchanged messages</h2>
<p>At this point one could wonder: <code>how much useful is a tool like this?</code>, <code>Do I really need it?</code> etc.</p>
<p>I must admit that I&#8217;ve started and completed this port without being an experienced EmberJS developer, but to be able to check if all the original features were working correctly on Firefox and to really understand how this browser add-on helps EmberJS developers during app development/debugging phases (its most important use cases), I&#8217;ve started to experiment with EmberJS and I have to say that EmberJS is a very pleasant framework to work with and Ember Inspector is a really important tool to put into our tool belts.</p>
<p>I&#8217;m pretty sure that every medium or large sized JavaScript framework need this kind of DevTool; clearly it will never be an integrated one, because it&#8217;s framework-specific and we will get used to this new family of DevTool Add-ons from now on.</p>
<h3>List Ember View, Model Components and Routes</h3>
<p>The first use case is being able to immediately visualize Routes, Views/Components, Models and Controllers our EmberJS app instantiate for us, without too much webconsole acrobatics.</p>
<p>So its immediately available (and evident) when we open its panel on an EmberJS Apps active in the current browser tab:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/ember_extension_viewTree_screenshot.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/ember_extension_viewTree_screenshot-500.png" alt="Ember Inspector - ViewTree" /></a></p>
<p>Using these tables we can then inspect all the properties (even computed ones) defined by us or inherited from the ember classes in the actual object hierarchy.</p>
<p>Using an approach very similar to the <a href="https://wiki.mozilla.org/Debugger_Architecture">Mozilla Remote Debugging Protocol</a> from the integrated DevTools infrastructure (e.g. even when we use devtools locally, they exchange JSON messages over a pipe), the <code>ember_debug</code> component injected into the target tab sends the info it needs about the instantiated EmberJS objects to the <code>devtool panel</code> component, each identified by internally generated reference IDs (similar to the <a href="https://wiki.mozilla.org/Remote_Debugging_Protocol#Grips">grips concept from the Mozilla Remote Debugging Protocol</a>.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember-extension-messages-exchanged.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember-extension-messages-exchanged-500x312.png" alt="Ember Extension - JSON messages" width="500" height="312" class="aligncenter size-large wp-image-23374" /></a></p>
<p>Logging the exchanged messages, we can learn more about the <code>protocol</code>.</p>
<h4>Receive updates about EmberJS view tree info (EmberDebug -> DevtoolPanel):</h4>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_inspectedWindow_viewTree.json"></script></p>
<h4>Request inspect object (DevtoolPanel -> EmberDebug):</h4>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_objectInspector_inspectById.json"></script></p>
<h4>Receive updates about the requested Object info (DevtoolPanel -> EmberDebug):</h4>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_inspectedWindow_updateObject.json"></script></p>
<h3>Reach every EmberJS object in the hierarchy from the webconsole</h3>
<p>A less evident but really useful feature is &#8220;sendToConsole&#8221;, to be able to reach any object/property that we can inspect from the webconsole, from the tables described above.</p>
<p>When we click the <strong>&gt;$E</strong> link, which is accessible in the right split panel:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/ember_extension_sendToConsole_screenshot1.png" alt="Ember Inspector - sendToConsole" /></p>
<p>The ember devtool panel asks to ember_debug to put the defined object/property into a variable accessible globally in the target tab and named <strong>$E</strong>, then we can switch to the webconsole and interact with it freely:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/ember_extension_sendToConsole_screenshot2.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/ember_extension_sendToConsole_screenshot2-500.png" alt="Ember Inspector - sendToConsole" /></a></p>
<h4>Request send object to console (DevtoolPanel -> EmberDebug):</h4>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_objectInspector_sendToConsole.json"></script></p>
<h3>Much more</h3>
<p>These are only some of the feature already present in the <strong>Ember Inspector</strong> and more features are coming in its upcoming versions (e.g. log and inspect Ember Promises).</p>
<p>If you already use EmberJS or if you are thinking about trying it, I suggest you to give <strong>Ember Inspector</strong> a try (on both Firefox or Chrome, if you prefer), it will turn inspecting your EmberJS webapp into a fast and easy task.</p>
<h2>Integrate XPI building into the grunt-based build process</h2>
<p>The last challenge in the road to a Firefox add-on fully integrated into the ember-extension build workflow was xpi building for an add-on based on the Mozilla Add-on SDK integrated into the grunt build process:</p>
<p>Chrome crx extensions are simply ZIP files, as are Firefox XPI add-ons, but Firefox add-ons based on the Mozilla Add-on SDK needs to be built using the <a href="https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/getting-started-with-cfx.html" title="Addon SDK Developer Guide - Getting Started with cfx" target="_blank"><strong>cfx</strong> tool from the Add-on SDK package</a>.</p>
<p>If we want more cross-browser add-ons, we have to help developers to build cross-browser extensions using the same approach used by ember-extension: a webapp built using grunt which will run into a browser add-on (which provides glue code specific to the various browsers supported).</p>
<p>So I decided to move the grunt plugin that I&#8217;ve put together to integrate Add-on SDK common and custom tasks (e.g. download a defined Add-on SDK release, build an XPI, run cfx with custom parameters) into <a href="http://github.com/rpl/grunt-mozilla-addon-sdk">a separate project</a> (and npm package), because it could help to make this task simpler and less annoying.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_final.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/ember_extension_final-500x257.png" alt="Ember Extension: Firefox and Chrome Add-ons grunt build" width="500" height="257" class="aligncenter size-large wp-image-23377" /></a></p>
<h3>Build and run Ember Inspector Firefox Add-on using grunt:</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=run_grunt.sh"></script></p>
<p>Following are some interesting fragments from grunt-mozilla-addon-sdk integration into ember-extension (which are briefly documented in the <a href="https://github.com/rpl/grunt-mozilla-addon-sdk/blob/master/README.md" target="_blank">grunt-mozilla-addon-sdk repo README</a>):</p>
<h3>Integrate grunt plugin into npm dependencies: package.json</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_inspector_npm_package.json"></script></p>
<h3>Define and use grunt shortcut tasks: Gruntfile.js</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=ember_inspector_grunt_file.js"></script></p>
<h3>Configure grunt-mozilla-addon-sdk tasks options</h3>
<p><script src="https://gist.github.com/rpl/7683088.js?file=grunt_mozilla_task_options.js"></script></p>
<h2>Conclusion</h2>
<p>Especially thanks to the help from the EmberJS/EmberInspector community and its maintainers, Ember Inspector Firefox add-on is officially merged and integrated in the automated build process, so now we can use it on Firefox and Chrome to inspect our EmberJS apps!</p>
<h3>Stable:</h3>
<ul>
<li>Firefox (from AMO): <a href="https://addons.mozilla.org/en-US/firefox/addon/ember-inspector/">https://addons.mozilla.org/en-US/firefox/addon/ember-inspector/</a></li>
<li>Chrome (from Chrome WebStore): <a href="https://chrome.google.com/webstore/detail/ember-inspector/bmdblncegkenkacieihfhpjfppoconhi">https://chrome.google.com/webstore/detail/ember-inspector/bmdblncegkenkacieihfhpjfppoconhi</a></li>
</ul>
<h3>Latest Build</h3>
<ul>
<li>Firefox: <a href="http://ember-extension.s3.amazonaws.com/xpi-latest.xpi">http://ember-extension.s3.amazonaws.com/xpi-latest.xpi</a></li>
<li>Chrome: <a href="http://ember-extension.s3.amazonaws.com/ember-extension-latest.zip">grunt-mozilla-addon-sdk</a></li>
</ul>
<p>In this article we&#8217;ve briefly dissected an interesting pattern to develop cross-browser devtools add-ons, and introduced a grunt plugin that simplifies integration of Add-on SDK tools into projects built using grunt: <a href="https://npmjs.org/package/grunt-mozilla-addon-sdk">https://npmjs.org/package/grunt-mozilla-addon-sdk</a></p>
<p>Thanks to the same <strong>web first</strong> approach Mozilla is pushing in the Apps domain creating cross-browser add-ons is definitely simpler than what we thought, and we all win :-)</p>
<p>Happy Cross-Browser Extending, <br />Luca</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/ember-inspector-on-a-firefox-near-you-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Application Layout with CSS3 Flexible Box Module</title>
		<link>https://hacks.mozilla.org/2013/12/application-layout-with-css3-flexible-box-module/</link>
		<comments>https://hacks.mozilla.org/2013/12/application-layout-with-css3-flexible-box-module/#comments</comments>
		<pubDate>Mon, 16 Dec 2013 18:31:50 +0000</pubDate>
		<dc:creator><![CDATA[Simon Speich]]></dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23361</guid>
		<description><![CDATA[It has become very easy to create fluid application layouts thanks to the CSS3 Flexible Box Layout Module. In this article we are going to implement a simple application layout, which fills the whole screen, resizes with the browser window and comes with the additional bonus of a draggable splitter. Instead of the classic &#60;div&#62; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>It has become very easy to create fluid application layouts thanks to the <a href="http://www.w3.org/TR/css3-flexbox/">CSS3 Flexible Box Layout Module</a>. In this article we are going to implement a simple application layout, which fills the whole screen, resizes with the browser window and comes with the additional bonus of a draggable splitter.</p>
<p>Instead of the classic <code>&lt;div&gt;</code> elements let’s also use some <a href="http://www.w3.org/wiki/HTML_structural_elements">HTML5 structural tags</a>. This will not only make the code more semantic, but also more convenient to work with, since we can directly address the elements with a CSS type selector without having to rely on id attributes or parent-child relationships.</p>
<p>Take a look at <a href="http://www.speich.net/articles/css-flexiblebox-layout-full.html">the complete demo</a> to see how it works.</p>
<h2>First step: Add Vertical Boxes</h2>
<p>We start with only three tags (&lt;header&gt;, &lt;main&gt; and &lt;footer&gt;) in the body.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/screenshot-flexbox1.gif" alt="" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CSS3 Application Layout<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;main<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/main<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;footer<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/footer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Let’s add the CSS to make these three elements fill the space vertically. This is achieved by setting the CSS <code>display</code> property of the <code>&lt;body&gt;</code> to <code>flex</code> and the <code>flex-direction</code> property to <code>column</code>. This tells the browser to lay out the body’s children (<code>&lt;header&gt;</code>, <code>&lt;main&gt;</code> and <code>&lt;footer&gt;</code>) as vertical flexible boxes.</p>
<p>How this available space is distributed can be controlled with the <code>flex</code> shorthand property. You can read about it on <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex">MDN</a>. In this application layout though, we don&#8217;t want the size to shrink or expand proportionally. Instead the <code>&lt;header&gt;</code> and the <code>&lt;footer&gt;</code> element should have a fixed height, whereas the <code>&lt;main&gt;</code> should just fill the remaining space by setting its <code>flex</code> property to <code>auto</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;">html<span style="color: #00AA00;">,</span> body <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
body <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> flex<span style="color: #00AA00;">;</span>
    flex-direction<span style="color: #00AA00;">:</span> column<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
header <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
main <span style="color: #00AA00;">&#123;</span>
    flex<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
footer <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.speich.net/articles/css-flexiblebox-layout1.html">Show demo</a></p>
<h2>Second Step: Horizontal Boxes</h2>
<p>Let’s add three more elements (<code>&lt;nav&gt;</code>, <code>&lt;article&gt;</code> and <code>&lt;aside&gt;</code>) inside the <code>&lt;main&gt;</code> element. But this time we want them to fill the space inside the <code>&lt;main&gt;</code> element horizontally instead of vertically.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/screenshot-flexbox2.gif" alt="" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;main<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nav<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/nav<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;article<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/article<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aside<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/aside<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/main<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;footer<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/footer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This is achieved by setting the <code>display</code> property of the <code>&lt;main&gt;</code> element also to <code>flex</code>, but the <code>flex-direction</code> property to <code>row</code> (this is the default). The <code>&lt;nav&gt;</code> and <code>&lt;aside&gt;</code> element should have a fixed width, while the <code>&lt;article&gt;</code> should just fill the remaining space: this is achieved in the same kind of manner as before:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;">main <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> flex<span style="color: #00AA00;">;</span>
    flex-direction<span style="color: #00AA00;">:</span> row<span style="color: #00AA00;">;</span>
    flex<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
nav <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
article <span style="color: #00AA00;">&#123;</span>
    flex<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
aside <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.speich.net/articles/css-flexiblebox-layout2.html">Show demo</a></p>
<p>Thats all. Resize your browser window and enjoy the flexible application layout.</p>
<h2>Next step: CSS Refinements</h2>
<p>But wait. When there is a lot of content, an element can become smaller than specified and also scrollbars can appear.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/screenshot-flexbox-overflow.gif" alt="" /></p>
<p>Therefore, we need to add a <code>min-width</code> property to all elements where we added a <code>width</code> property. We also should set the <code>overflow</code> property of the <code>&lt;body&gt;</code> and the <code>&lt;main&gt;</code> element to <code>hidden</code> as well as the <code>overflow-y</code> of <code>&lt;article&gt;</code> and <code>&lt;aside&gt;</code> to <code>auto</code> to only show scrollbars where we want them.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> flex<span style="color: #00AA00;">;</span>
	flex-direction<span style="color: #00AA00;">:</span> column<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
header <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
footer <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
main <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> flex<span style="color: #00AA00;">;</span>
	flex-direction<span style="color: #00AA00;">:</span> row<span style="color: #00AA00;">;</span>
	flex<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span> grey<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
nav <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
article <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span> grey<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
	flex<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	overflow-x<span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
	overflow-y<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
aside <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
	overflow-x<span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
	overflow-y<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><em>Note: This does probably not work in Safari yet. You might be able to get it to work by using the <code>-webkit-</code> prefix.</em></p>
<h2>Final Step: Throw a little JavaScript into the Mix</h2>
<p>As the final step, we want the user to be able to resize the <code>&lt;aside&gt;</code> element when dragging it with the mouse. For that we add a <code>&lt;div&gt;</code> element as a splitter, which will serve as the drag handle.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;main<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nav<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/nav<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;article<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/article<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;splitter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aside<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/aside<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/main<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;footer<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/footer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>We set the width of the handle to 4px and give the <code>cursor</code> attribute a value of <code>col-resize</code>, to show the user that this element can be resized East-West.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.splitter</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> grey<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> col-resize<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>All what’s left now is to add a little JavaScript, that enables moving the splitter.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> window<span style="color: #339933;">,</span> d <span style="color: #339933;">=</span> document<span style="color: #339933;">,</span> splitter<span style="color: #339933;">;</span>
&nbsp;
splitter <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    lastX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
    leftEl<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    rightEl<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
    init<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>handler<span style="color: #339933;">,</span> leftEl<span style="color: #339933;">,</span> rightEl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">leftEl</span> <span style="color: #339933;">=</span> leftEl<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">rightEl</span> <span style="color: #339933;">=</span> rightEl<span style="color: #339933;">;</span>
&nbsp;
        handler.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousedown'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            evt.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">/* prevent text selection */</span>
&nbsp;
            self.<span style="color: #660066;">lastX</span> <span style="color: #339933;">=</span> evt.<span style="color: #660066;">clientX</span><span style="color: #339933;">;</span>
&nbsp;
            w.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> self.<span style="color: #660066;">drag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            w.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseup'</span><span style="color: #339933;">,</span> self.<span style="color: #660066;">endDrag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    drag<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> wL<span style="color: #339933;">,</span> wR<span style="color: #339933;">,</span> wDiff <span style="color: #339933;">=</span> evt.<span style="color: #660066;">clientX</span> <span style="color: #339933;">-</span> splitter.<span style="color: #660066;">lastX</span><span style="color: #339933;">;</span>
&nbsp;
        wL <span style="color: #339933;">=</span> d.<span style="color: #660066;">defaultView</span>.<span style="color: #660066;">getComputedStyle</span><span style="color: #009900;">&#40;</span>splitter.<span style="color: #660066;">leftEl</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getPropertyValue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wR <span style="color: #339933;">=</span> d.<span style="color: #660066;">defaultView</span>.<span style="color: #660066;">getComputedStyle</span><span style="color: #009900;">&#40;</span>splitter.<span style="color: #660066;">rightEl</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getPropertyValue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wL <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>wL<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> wDiff<span style="color: #339933;">;</span>
        wR <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>wR<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> wDiff<span style="color: #339933;">;</span>
        splitter.<span style="color: #660066;">leftEl</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> wL <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
        splitter.<span style="color: #660066;">rightEl</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> wR <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
&nbsp;
        splitter.<span style="color: #660066;">lastX</span> <span style="color: #339933;">=</span> evt.<span style="color: #660066;">clientX</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    endDrag<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        w.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> splitter.<span style="color: #660066;">drag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        w.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseup'</span><span style="color: #339933;">,</span> splitter.<span style="color: #660066;">endDrag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
splitter.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'splitter'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> d.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> d.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>Note: For some reason the resizing doesn’t work in IE11 (, Safari?) or Chrome 31. It seems that it has something to do with the <code>display: flex;</code> property value.</em></p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/application-layout-with-css3-flexible-box-module/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSS Variables in Firefox Nightly</title>
		<link>https://hacks.mozilla.org/2013/12/css-variables-in-firefox-nightly/</link>
		<comments>https://hacks.mozilla.org/2013/12/css-variables-in-firefox-nightly/#comments</comments>
		<pubDate>Sun, 15 Dec 2013 05:22:03 +0000</pubDate>
		<dc:creator><![CDATA[Chris Heilmann]]></dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox Nightly]]></category>
		<category><![CDATA[Standards]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23483</guid>
		<description><![CDATA[As reported by Cameron McCormack, Firefox Nightly (version 29) now supports CSS variables. You can get a quick overview in this short screencast: You can define variables in a context with a var- prefix and then implement them using the var() instruction. For example: :root &#123; var-companyblue: #369; var-lighterblue: powderblue; &#125; &#160; h1 &#123; color: [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>As reported by <a href="http://mcc.id.au/blog/2013/12/variables">Cameron McCormack</a>, Firefox Nightly (version 29) now supports <a href="http://dev.w3.org/csswg/css-variables/">CSS variables</a>. You can get a quick overview in this <a href="http://www.youtube.com/watch?v=AYCYzSC4qFU">short screencast</a>:</p>
<p><iframe width="500" height="375" src="//www.youtube-nocookie.com/embed/AYCYzSC4qFU?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>You can define variables in a context with a <code>var-</code> prefix and then implement them using the <code>var()</code> instruction. For example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #3333ff;">:root </span><span style="color: #00AA00;">&#123;</span>
  var-companyblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#369</span><span style="color: #00AA00;">;</span> 
  var-lighterblue<span style="color: #00AA00;">:</span> powderblue<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h1 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>companyblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
h2 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>lighterblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Header on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Subheader on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This defines the two variables <code>companyblue</code> and <code>lighterblue</code> for the root element of the document which results in (<a href="http://jsfiddle.net/codepo8/rrmWV/1/">you can try it here</a> using <a href="http://nightly.mozilla.org/">Firefox Nightly</a>): </p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/cssvariables-1.jpg.jpeg" alt="" width="395" height="108" class="alignnone size-full wp-image-23494" /></p>
<p>Variables are scoped, which means you can overwrite them:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #3333ff;">:root </span><span style="color: #00AA00;">&#123;</span>
  var-companyblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#369</span><span style="color: #00AA00;">;</span>
  var-lighterblue<span style="color: #00AA00;">:</span> powderblue<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.partnerbadge</span> <span style="color: #00AA00;">&#123;</span>
  var-companyblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#036</span><span style="color: #00AA00;">;</span>
  var-lighterblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#cfc</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h1 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>companyblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
h2 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>lighterblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Header on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Subheader on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;partnerbadge&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Header on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Subheader on page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Using these settings, headings inside an element with a class of <code>partnerbadge</code> will now <a href="http://jsfiddle.net/codepo8/rrmWV/2">get the other blue settings</a>:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/cssvariables-2.jpg.jpeg" alt="" width="396" height="226" class="alignnone size-full wp-image-23493" /></p>
<p>Variables can be any value you want to define and you can use them like any other value, for example inside a <code>calc()</code> calculation. You can also reset them to other values, for example inside a media query. <a href="http://jsfiddle.net/codepo8/rrmWV/3">This example</a> shows many of these possibilities.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #3333ff;">:root </span><span style="color: #00AA00;">&#123;</span>
  var-companyblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#369</span><span style="color: #00AA00;">;</span>
  var-lighterblue<span style="color: #00AA00;">:</span> powderblue<span style="color: #00AA00;">;</span>
  var-largemargin<span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>  
  var-smallmargin<span style="color: #00AA00;">:</span> calc<span style="color: #00AA00;">&#40;</span>var<span style="color: #00AA00;">&#40;</span>largemargin<span style="color: #00AA00;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  var-borderstyle<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
  var-headersize<span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.partnerbadge</span> <span style="color: #00AA00;">&#123;</span>
  var-companyblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#036</span><span style="color: #00AA00;">;</span>
  var-lighterblue<span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#369</span><span style="color: #00AA00;">;</span>
  var-headersize<span style="color: #00AA00;">:</span> calc<span style="color: #00AA00;">&#40;</span>var<span style="color: #00AA00;">&#40;</span>headersize<span style="color: #00AA00;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  transition<span style="color: #00AA00;">:</span> 0.5s<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #a1a100;">@media (max-width: 400px) {</span>
  <span style="color: #6666ff;">.partnerbadge</span> <span style="color: #00AA00;">&#123;</span>
     var-borderstyle<span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>  
     <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Applying the variables */</span>
body <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'open sans'</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
h1 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>companyblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>largemargin<span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>headersize<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
h2 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>lighterblue<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>smallmargin<span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> calc<span style="color: #00AA00;">&#40;</span>var<span style="color: #00AA00;">&#40;</span>headersize<span style="color: #00AA00;">&#41;</span> - <span style="color: #933;">5px</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.partnerbadge</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>smallmargin<span style="color: #00AA00;">&#41;</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> var<span style="color: #00AA00;">&#40;</span>borderstyle<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/cssvariables-3.jpg.jpeg" alt="" width="397" height="223" class="alignnone size-full wp-image-23492" /></p>
<p>Try resizing the window to less than 400 pixels to see the mediaQuery change in action.</p>
<p>An initial implementation of CSS Variables has just landed in Firefox Nightly, which is currently at version 29 and after the February 3 merge, in Firefox Aurora. There are still a few parts of the specification which still need to be supported before the can go into the release cycle of Firefox Beta and Firefox. Cameron has the details on that: </p>
<blockquote><p>The only part of the specification that has not yet been implemented is the <code>CSSVariableMap</code> part, which provides an object that behaves like an ECMAScript <code>Map</code>, with <code>get</code>, <code>set</code> and other methods, to get the values of variables on a <code>CSSStyleDeclaration</code>.  Note however that you can still get at them in the DOM by using the <code>getPropertyValue</code> and <code>setProperty</code> methods, as long as you use the full property names such as <code>"var-theme-colour-1"</code>.</p>
<p>The work for this feature was done in <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=773296">bug 773296</a>, and my thanks to <a href="http://dbaron.org/">David Baron</a> for doing the reviews there and to <a href="https://twitter.com/ebassi">Emmanuele Bassi</a> who did some initial work on the implementation.  If you encounter any problems using the feature, please <a href="https://bugzilla.mozilla.org/enter_bug.cgi?product=Core&amp;component=CSS%20Parsing%20and%20Computation&amp;cc=cam@mcc.id.au&amp;blocked=773296">file a bug</a>!</p></blockquote>
<p>For now, have fun playing with CSS variables in Nightly and tell us about issues you find.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/css-variables-in-firefox-nightly/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Monster Madness &#8211; creating games on the web with Emscripten</title>
		<link>https://hacks.mozilla.org/2013/12/monster-madness-creating-games-on-the-web-with-emscripten/</link>
		<comments>https://hacks.mozilla.org/2013/12/monster-madness-creating-games-on-the-web-with-emscripten/#comments</comments>
		<pubDate>Thu, 12 Dec 2013 15:00:31 +0000</pubDate>
		<dc:creator><![CDATA[Jeremy Stieglitz]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[IndexedDB]]></category>
		<category><![CDATA[WebRTC]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23253</guid>
		<description><![CDATA[When our engineering teams at Trendy Entertainment &#038; Nom Nom Games decided on the strategy of developing one of our new Unreal Engine 3 games &#8212; Monster Madness Online &#8212; as a cross-platform title, we knew that a frictionless multiplayer web browser version would be central to this experience. The big question, however, was determining [&#8230;]]]></description>
				<content:encoded><![CDATA[<blockquote><p><em>When our engineering teams at Trendy Entertainment &#038; Nom Nom Games decided on the strategy of developing one of our new Unreal Engine 3 games &#8212; <a href="http://monstermadness.com" title="Monster Madness Online">Monster Madness Online</a> &#8212; as a cross-platform title, we knew that a frictionless multiplayer web browser version would be central to this experience. The big question, however, was determining what essential technologies to utilize in order to bring our game onto the web. As a C++ oriented developer, we determined quickly that rewriting the game engine from the ground-up was out of the question. We’d need a solution that would allow us to port our existing code in an efficient manner into a format usable in the browser&#8230;</em></p>
</blockquote>
<p><em>TL;DR? Watch the video!</em><br />
<iframe width="500" height="281" src="//www.youtube.com/embed/uKZ050Ut0QU?rel=0" frameborder="0" allowfullscreen></iframe></p>
<h2>Playing the Field</h2>
<p>We looked hard at the various options in front of us: FlasCC (a GCC Flash compiler), Google’s NaCl, a custom native C++ extension, or Mozilla’s Emscripten &#038; asm.js. </p>
<p>In our tests, Flash ran slowly and had inconsistent behaviors between Pepper (Chrome) and Adobe’s plugin version. Combined with the increasingly onerous plugin requirement, we opted to look elsewhere for a more seamless, forward-thinking approach.</p>
<p>NaCl had the issue of requiring a walled-garden distribution site that would separate us from direct contact with our users, and also being processor-specific. pNaCL eliminated the walled-garden requirement and added dynamic code compilation support, but still had the issues of being processor-specific code necessitating in our view device-specific testing, and a potentially long startup time as the code would be linked on first run. Finally, only working in Chrome would be a dealbreaker for our desire to have our game run in all major browsers. </p>
<p>A custom plugin/extension with C++ would require lots of testing &#038; maintenance efforts on our part to run across different browsers, processor architectures, and operating systems, and such an installation requirement would likely scare away many potential players.</p>
<p>As it turned out, for our team’s purposes the Emscripten compiler &#038; asm.js proved to be the best solution to these challenges, and when combined with a set of other new-ish Web API’s, enabled the browser to become a fully featured platform for instant high-end 3D gaming. This just took a little trial &#038; error to figure out exactly how we’d piece it together&#8230;and that’s what we’ll be reviewing here!</p>
<h2>First Steps into a Brave New World</h2>
<p>We Trendy game engineers are primarily old-school C++ programmers, so it was something of a revelation that Emscripten could compile our existing application (built on Epic Game’s Unreal Engine 3) into asm.js optimized Javascript with little to no changes. </p>
<p>The primary Unreal Engine 3-specific code tweaks that were necessary to get the project to compile &#038; run with Emscripten, were essentially&#8230; 1, 2, 3:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #0000dd;">1</span>.
<span style="color: #666666; font-style: italic;">// Esmcripten needs 4 byte alignment</span>
FNameEntry<span style="color: #339933;">*</span> Allocate<span style="color: #009900;">&#40;</span> INT Size <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">#if EMSCRIPTEN</span>
       Size <span style="color: #339933;">=</span> Align<span style="color: #009900;">&#40;</span> Size<span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">#endif</span>
&nbsp;
    ......
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000dd;">2</span>.
<span style="color: #666666; font-style: italic;">// Script execution: llvm needs aligned data</span>
<span style="color: #339933;">#if EMSCRIPTEN </span>
    <span style="color: #339933;">#define XFER(T)</span>
    <span style="color: #009900;">&#123;</span>
        T Temp<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Ar.<span style="color: #202020;">IsLoading</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            appMemcpy<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>Temp<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>Script<span style="color: #009900;">&#40;</span>iCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        Ar <span style="color: #339933;">&lt;&lt;</span> Temp<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Ar.<span style="color: #202020;">IsSaving</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            appMemcpy<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>Script<span style="color: #009900;">&#40;</span>iCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>Temp<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        iCode <span style="color: #339933;">+=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">#else</span>
    <span style="color: #339933;">#define XFER(T) { Ar &lt;&lt; *(T*)&amp;Script(iCode); iCode += sizeof(T); }</span>
<span style="color: #339933;">#endif</span>
&nbsp;
<span style="color: #0000dd;">3</span>.
<span style="color: #666666; font-style: italic;">// This function needs to synchronously complete IO requests for single-threaded Emscripten IO to work, so added a ServiceRequestsSynchronously() to the end of it which flushes &amp; blocks till the IO request finishes.</span>
FAsyncIOSystemBase<span style="color: #339933;">::</span><span style="color: #202020;">QueueIORequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>No really, that was about it! Within a day of fiddling with it, we had our game’s Javascript ‘executable’ compiled &#038; running in the browser. Crashing, due to not having a graphics API implememented &#8212; but running with log output! Thankfully, we already had Unreal Engine3’s OpenGL ES2 version of the rendering subsystem ready to utilize, so porting the renderer to WebGL only took another day. </p>
<p>WebGL appeared to essentially have a superset of features compared to OpenGL ES2, so the shaders and methods used matched up by simply changing some API calls. In fact, we were able to do improvements by making use of WebGL’s floating point render targets for certain postprocessing effects, such as edge outlining and dynamic shadows. </p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/EmscriptenGamePost.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/EmscriptenGamePost-500x281.jpg" alt="EmscriptenGamePost" width="500" height="281" class="alignnone size-large wp-image-23338" /></a><br />
<em>Postprocessing makes everything prettier!</em></p>
<h2>But how’s it run?</h2>
<p>Now we had something rendering in the browser, and with a quick change to capture input, we could start playing the game and analyzing its performance. What we found was very encouraging: straight ‘out of the box’, in Firefox the asm.js version of the game was getting nearly 33% of the performance of the native executable. And this was comparing a single-threaded web application to the multi-threaded native executable (so really, not a fair comparison! ;). This was about 2x the performance we saw with our quick Flash port (which we still utilize as a fallback for older browsers that don’t yet support asm.js, though we eventually hope to deprecate entirely).</p>
<p>Its performance in Chrome was less astonishing, more towards 20% of native performance, but still within our target margins: namely, can it run on a 2011-model Macbook Air at 45-60 FPS (with Vsync disabled)? The answer, thankfully, was yes. We hope Google will continue to improve the performance of asm.js on their browser over time. But as it currently stands, we believe unless you’re making the browser version of ‘Crysis’ with this tech (which may not be far off), it seems you have enough performance even in Chrome to do most kinds of web games.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/BrowserFramerate.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/BrowserFramerate-500x330.jpg" alt="BrowserFramerate" width="500" height="330" class="alignnone size-large wp-image-23339" /></a><br />
<em>60 FPS on an old Macbook Air</em></p>
<h2>Putting the Pieces into Place</h2>
<p>So within a week from starting, we had turned our Unreal Engine 3 PC game into a well-running, graphically-rich web game. But where to take it from here? Well, it’d still need: Audio, Networking, Streaming, and Storage. Let’s discuss the various techniques used for each of these systems. </p>
<h3>Audio</h3>
<p>This was a no-brainer, as there is only really one robust standardized web audio system apart from Flash: WebAudio. Again, this API matched up pretty well to its mobile cousin, OpenSL, for which we already had an integration. So once we switched out the various calls, we had . </p>
<p>There was an apparent issue in Mac Chrome where sounds flagged “looping” would sometimes never become destroyed, so we implemented a Chrome-specific hack to manually loop the sound, and filed a bug report with Google. Ah well, one thing we’ve seen with browser API’s is there’s not a 100% guarantee that every browser will implement the functionality to perfect specification, but it gets the job done!</p>
<h3>Networking</h3>
<p>This proved a little trickier. First, we investigated WebRTC as used in the <a href="https://developer.mozilla.org/en-US/demos/detail/bananabread" title="Bananabread demo">Bananabread demo</a>, but WebRTC of course is for browser-to-browser communication which is actually not what we were looking to do. Our online game service uses a server-and-client architecture with centralized infrastructure, and so WebSockets is the API to utilize in that case. The tricky part is that we have to handle all the WebSockets incoming and outgoing data in JavaScript buffers, and then pass that along to the “C++” Emscripten-compiled game. </p>
<p>With some callbacks, this worked out, but we also had to take our UDP game server code and place the WebSockets TCP-style layer onto it &#8212; some iteration was necessary to get the packets to be formatted in exactly the way that WebSockets expects, but once we did that, our browser game was communicating with our backend-hosted Linux dedicated game servers with no problems!</p>
<h3>Streaming &#038; Storage</h3>
<p>One advantage to being on the web is easy access to the browser’s asynchronous downloading functionalities to stream-in content. We certainly made use of this with our game, with the initial download clocking in at under 10 MB. Everything else streams in on-demand as you play using standard browser http download requests: Textures, Sound Effects, Music, even Skeletal Meshes and Level Packages. But the bigger question is how to reliably store this content. We don’t want to just rely on the browser cache, since it’s not good for guaranteed immediate gameplay loading as we can’t pre-query whether something exists on disk in the regular browser cache or not. </p>
<p>For this, we used the IndexedDB API, which lets us asynchronously save and retrieve data objects from a secure abstracted storage location. It works in both Chrome and Firefox, though it’s still finicky as the database can occasionally become corrupted (perhaps if terminated during async writes) and has to be regenerated. In the worst case, this simply results in a re-download of content the user already had received. </p>
<p>We’re currently looking into this issue, but that aside, IndexedDB certainly works well and has the advantage of providing our application standard file IO functionality, useful to store content that we download. (UPDATE:  Firefox Nightly build as of 12/10 seems to automatically reset the IndexedDB storage if this happens and it may not recur.)</p>
<h2>Play it Now, and Embrace the Future!</h2>
<p>While we still have more profiling and tweaking to, as we’re just now starting to use Firefox’s VTune support to symbolically profile the asm.js performance within the browser. Even still, we’re pretty pleased with where the things currently stand. But don’t take our word for it, please try it yourselves right here, no installation or sign-up required:</p>
<p><a href="http://www.playverse.com/Anonplayer/0-a2aadd1b76e14d0e848ea1de18dca4e8" title="Try our demo test anonymously In-browser Here!">Try our demo test anonymously In-browser Here!</a><br />
<em>(Please bear with us if our game servers limit access under load, we’re still testing our backend scalability!)</em></p>
<p>We at Trendy envision a day when anybody can play any game no matter where they are or what device they happen to have, without friction or gateways or middlemen. With the right combination of these cutting-edge web technologies, that day can be today. We hope other enterprising game developers will join us in reaching players directly through the web, which thanks to Emscripten &#038; asm.js, may well become the most powerful and far-reaching “game console” of all!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/monster-madness-creating-games-on-the-web-with-emscripten/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>The Side Projects of Mozillians: JSFiddle and Meatspac.es</title>
		<link>https://hacks.mozilla.org/2013/12/the-side-projects-of-mozillians-jsfiddle-and-meatspac-es/</link>
		<comments>https://hacks.mozilla.org/2013/12/the-side-projects-of-mozillians-jsfiddle-and-meatspac-es/#comments</comments>
		<pubDate>Wed, 11 Dec 2013 13:22:39 +0000</pubDate>
		<dc:creator><![CDATA[Louis Stowasser]]></dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23210</guid>
		<description><![CDATA[At Mozilla, we are happy to get the chance to work with a lot of talented people. Therefore, as an on-going series, we wanted to take the opportunity to highlight some of the exciting projects Mozillians work on in their spare time. JSFiddle JSFiddle is a tool to write web examples (in HTML, JavaScript and [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>At Mozilla, we are happy to get the chance to work with a lot of talented people. Therefore, as an on-going series, we wanted to take the opportunity to highlight some of the exciting projects Mozillians work on in their spare time.</p>
<h2>JSFiddle</h2>
<p><em><a href="http://jsfiddle.net">JSFiddle</a> is a tool to write web examples (in HTML, JavaScript and CSS) called ‘fiddles’. They can be saved and shared with others or embedded in a website which is perfect for blogs, documentation or tutorials. Created by <a href="https://twitter.com/zalun">Piotr Zalewa</a>.</em></p>
<p><a href="http://jsfiddle.net"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/687474703a2f2f692e696d6775722e636f6d2f463271764532442e706e67.png" alt="JSFiddle" width="128" height="128" class="alignnone size-full wp-image-23212" /></a></p>
<p>Piotr: I wanted a tool that could help me check if my frontend code was working. I was active on the MooTools scene at the time and we needed a tool to support our users who had questions about the framework and specific bugs to solve. The community is the best motivation. There are about 2,000 developers creating and watching fiddles right now! Many big projects are using JSFiddle for docs (MooTools, HighCharts) or bug requests (jQuery).</p>
<p>I&#8217;m always logged in on the #mootools IRC channel and one day we had a small competition to see who could be the first to answer support questions with only one line of JavaScript code. A user asked a non-trivial question which needed to be answered with both HTML and JavaScript. Our usual workflow was to write an HTML file, run it locally in the browser, copy the code to a Pastebin site then share the link. No one knew of a tool that could do this. The next day I had a prototype created in the evening and it was well accepted. The working but ugly version was completed shortly after. <a href="https://twitter.com/oskar">Oskar Krawczyk</a> joined as a designer and the project was ready to be shown to the world.</p>
<p>It started as Django and MySQL on the server side with MooTools as a frontend framework. Since then the only major change was adding Memcache. Currently we run JSFiddle on 12 servers sponsored by DigitalOcean. 2 database servers, 3 application servers, 2 Memcache, then static files and development servers. I would ideally like to have the database structured in a way that would be easier to scale. The database is huge and updating tables takes a lot of time.</p>
<p>JSFiddle was designed in the time when most of the JavaScript libraries were running under one framework only. We want to allow users to mix frameworks and add more languages. At the moment you can write in HTML, JavaScript, Coffeescript, CSS and SCSS but I would like to support more languages. We&#8217;ve got a full hat of ideas to be implemented but I think it’s better to provide improvements than promises.</p>
<h2>Meatspac.es</h2>
<p><em><a href="https://chat.meatspac.es">Meatspac.es</a> is a single public channel chat app that generates animated GIFs of users from their camera once they submit a new message. Created by <a href="https://twitter.com/ednapiranha">Jen Fong</a> with GIF library support added by <a href="https://twitter.com/supersole">Sole Penadés</a>.</em></p>
<p><a href="https://chat.meatspac.es"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/icon-128.png" alt="Meatspac.es" width="128" height="128" class="alignnone size-full wp-image-23213" /></a></p>
<p>Jen: I&#8217;ve been working on various quirky chat apps that involved some form of embedded media so this was an idea I had about getting users to interact beyond typing by posing for the camera and doing a little movement. I also really like GIFs and the fact that they work everywhere. I had been playing with WebRTC here and there and Sole was working on her <a href="https://developer.mozilla.org/en-US/Apps/Reference_apps/rtcamera">RTCamera</a> app when I thought: “Could we combine the two worlds? Chat and GIFs?”.</p>
<p>For the web server I used Nginx which proxies to a long running Node process using Express. The messages and GIFs are stored temporarily in LevelDB with a TTL (time-to-live) that deletes the message, including the GIFs stored as Base64 blobs, after 10 minutes. On the client-side, it uses jQuery, some GIF library files and updates with WebSockets with an AJAX fallback.</p>
<p>The biggest challenge of the project was surprisingly not code related! It was largely keeping up with all the craziness when a flood of people started using the chat, tweeting at me and contacting me. I first mentioned it publicly at ‘RealTimeConf’ in Portland a few weeks prior then started tweeting about it. After that a bunch of people checked it out, and someone posted it on Hacker News where even more people came (around 8,000 people on the heaviest day). It was mentioned on Twitter and various sources for a few days after.</p>
<p>People can be really creative during their GIF creation. It was also interesting to watch people give each other humorous &#8216;-bro&#8217; nicknames; both women and men. They would always ask others what their name should be rather than giving themselves a name.</p>
<p>I am now working on a similar app but for one to many GIF chatting for Firefox OS called chatspaces. Anyone who is interested in contributing can watch the <a href="https://github.com/meatspaces/meatspace-chat">repository</a> and check the README for what to contribute.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/the-side-projects-of-mozillians-jsfiddle-and-meatspac-es/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Mozilla Developer Network has a New Face</title>
		<link>https://hacks.mozilla.org/2013/12/the-mozilla-developer-network-has-a-new-face/</link>
		<comments>https://hacks.mozilla.org/2013/12/the-mozilla-developer-network-has-a-new-face/#comments</comments>
		<pubDate>Mon, 09 Dec 2013 20:26:07 +0000</pubDate>
		<dc:creator><![CDATA[David Walsh]]></dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[MDN]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23284</guid>
		<description><![CDATA[Last summer the Mozilla Developer Network (MDN) underwent a massive platform change, moving from a hosted third-party solution to our own custom Django application code-named Kuma. That move laid the ground work for our latest major MDN upgrade: a complete front-end redesign, included many new features as well as usability and accessibility enhancements. Let me [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Last summer the <a href="https://developer.mozilla.org/en-US/">Mozilla Developer Network (MDN)</a> underwent a massive platform change, moving from a hosted third-party solution to our own custom Django application code-named <a href="http://github.com/mozilla/kuma">Kuma</a>.  That move laid the ground work for our latest major MDN upgrade:  a complete front-end redesign, included many new features as well as usability and accessibility enhancements.  Let me provide you with a quick overview of what you can expect to see on the new MDN and what features we&#8217;re cooking up for the future!</p>
<h2>New MDN Features</h2>
<h3>Increased Commitment to Search</h3>
<p>The majority of MDN users are looking to find documentation the moment they land on the MDN homepage, so we&#8217;ve placed search front and center:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/MDNHomepage.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/MDNHomepage-500.png" alt="" /></a></p>
<p>We&#8217;ve also added search filters to the mix, allowing users to narrow down search results to their specific needs:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/New_Search.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/New_Search-500.png" alt="" /></a></p>
<p>From a technical perspective, we&#8217;ve moved to Elasticsearch for search, allowing us to continue making indexing and filtering improvements, as well as add new search features at will.  We anticipate fine-tuning search as we receive feedback so we&#8217;ll continue the push to get you to better documentation faster.</p>
<h3>Ease in Navigation</h3>
<p>Getting from document to document was a pain point in the previous design, so we&#8217;ve fixed that in two ways.  The first was creating Content Zones, a method for creating navigation for a given topic.  We&#8217;ve started with the most prominent parts of MDN, including <a href="https://developer.mozilla.org/en-US/Apps">App Center</a>, <a href="https://developer.mozilla.org/en-US/Firefox">Firefox</a>, <a href="https://developer.mozilla.org/en-US/Firefox_OS">Firefox OS</a>, <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Marketplace">Firefox Marketplace</a>, <a href="https://developer.mozilla.org/en-US/Add-ons">Add-ons</a>, and <a href="https://developer.mozilla.org/en-US/Persona">Persona</a>:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/Firefox_OS.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/Firefox_OS-500.png" alt="" /></a></p>
<h4>Content Zones</h4>
<p>MDN&#8217;s new Content Zones provide a complete collection of documentation about a given topic, encompassing the very basics of a topic to API details and advanced techniques.  We&#8217;ll be kicking off with the following zones:</p>
<h5><a href="https://developer.mozilla.org/en-US/Firefox_OS">Firefox OS</a></h5>
<p>Highlights of the Firefox OS zone include:</p>
<ul>
<li>A detailed Platform Guide</li>
<li>Build and Install details</li>
<li>Hacking Firefox OS</li>
<li>App Design &amp; Development</li>
</ul>
<h5><a href="https://developer.mozilla.org/en-US/Marketplace">Firefox Marketplace</a></h5>
<p>Highlights of this zone include:</p>
<ul>
<li>App submission and review</li>
<li>App publishing and monetization</li>
<li>Marketplace API information</li>
</ul>
<h5><a href="https://developer.mozilla.org/en-US/Apps">App Center</a></h5>
<p>Highlights of the App Center zone include:</p>
<ul>
<li>Quickstart Guide</li>
<li>Design and Build tips</li>
<li>App publishing guidelines</li>
<li>API references</li>
</ul>
<h5><a href="https://developer.mozilla.org/en-US/Persona">Persona</a></h5>
<p>Highlights of the Persona zone include:</p>
<ul>
<li>Guide to using Persona on your site</li>
<li>Becoming an identify provider</li>
<li>Details on the Persona project</li>
</ul>
<h5><a href="https://developer.mozilla.org/en-US/Firefox">Firefox</a></h5>
<p>Highlights of the Firefox zone include:</p>
<ul>
<li>A complete Firefox Add-on overview</li>
<li>Information on Firefox internals</li>
<li>Detailed instructions for building Firefox and contributing</li>
</ul>
<h5><a href="https://developer.mozilla.org/en-US/Add-ons">Add-ons</a></h5>
<p>Highlights of the Add-ons zone include:</p>
<ul>
<li>XUL extension information</li>
<li>Best practice tips</li>
<li>Theming</li>
<li>Add-on publishing guidelines</li>
</ul>
<h4>&#8220;See Also&#8221; Links</h4>
<p>We&#8217;ve also implemented &#8220;See Also&#8221; links which may appear in any wiki page, linking to documents which may be relevant based on the document you&#8217;re currently viewing.</p>
<p>Both the zone subnavigation and &#8220;See Also&#8221; link sidebar widgets are built from basic link lists in the wiki document, so adding links and shuffling navigation is easy for anyone looking to contribute to MDN.  These link lists can also be built using MDN&#8217;s macro language, Kumascript, and our writing team has done a great job automating &#8220;See Also&#8221; links so that contributors can save on the manual labor of hunting down other relevant documents.</p>
<h4>Top level navigation</h4>
<p>In the top level navigation, you will have access to five distinct areas:</p>
<ul>
<li>The above-mentioned Content Zones</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web">Web Platform</a>, including direct links to more information on technologies, references and guides</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Developer_Program">Developer Program</a> &#8211; To be able to help developers and establish long-term relationships and channels, we have created the Mozilla Developer Program. We have a lot of plans and ideas for iteratively expanding the Program, and we want you involved as we do so! So, sign up! You will get a membership, be able to subscribe to our newsletter and get access to features over time as we roll them out.</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Tools">Tools</a> &#8211; more information on the Firefox Developer Tools and their features</li>
<li><a href="https://developer.mozilla.org/en-US/demos/">Demos</a>, being a direct link to the Demo Studio</li>
</ul>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/Dev_Program.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/Dev_Program-500.png" alt="" /></a></p>
<h3>Enhanced Kumascript Macro Features</h3>
<p><a href="https://github.com/mozilla/kumascript">Kumascript</a>, MDN&#8217;s dynamic macro language, was also outfitted with the ability to read external RSS feeds.   At present MDN is using the feed reader capability to pull forum posts from StackOverflow and blog posts from the Mozilla Hacks blog.  Check out the <a href="https://developer.mozilla.org/en-US/docs/Template:mdn:common">MDN:Common macro</a> to view the <code>fetchJSONResource</code> and <code>fetchHTTPResource</code> methods which aid in displaying feed content in wiki documents.</p>
<h2>Future Features</h2>
<p>This visual redesign is just the beginning of our push to make MDN more dynamic and usable.  The MDN development and UX teams have plenty more coming in 2014.  Here are a few peeks into what you can expect to see!</p>
<h3>Dynamic Search Filtering</h3>
<p>To improve the efficiency in user search, we plan to implement hashtag-prefixed text filtering which may be added in the initial search &#8212; doing so will prevent the need for additional filtering when the user lands on the search results page.</p>
<p><img src="http://static.squarespace.com/static/5015397584aea6ed68d41dca/t/52790476e4b0c331152075b7/1383662714993/hhabstritt_commandquery2.png?format=500w" alt="" /></p>
<p>Holly Habstritt Gaal has <a href="http://hollyhabstritt.com/blog/2013/11/5/command-query-a-better-filter-and-search-for-developers-on-mdn">detailed this query system</a> in detail on her <a href="http://hollyhabstritt.com/blog/">blog</a>.  Check out her blog post to see implementation details.</p>
<h3>Docs Navigator</h3>
<p>So you&#8217;ve completed a search and you click the first link you thought would be applicable, but you want to move onward and view other results.  Instead of backing out to the search results page again, the wiki page (if the user came from search) will display a doc navigator to move to the next or previous result, or you can view the entire list of results from your original search:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/12/SearchNavigator.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/SearchNavigator-500.png" alt="image" /></a></p>
<p>Just another handy way of finding what you need faster!</p>
<h3>Demo Studio and Dev Derby Redesign</h3>
<p>A redesign to the MDN <a href="https://developer.mozilla.org/en-US/demos">Demo Studio</a> and Dev Derby will be coming shortly.  We have an <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=925893">excellent design in review</a> and we hope to roll that out in early 2014.</p>
<p>If you have a suggestion or find any bugs within the new MDN, <a href="http://mzl.la/mdn-post-redesign-feedback">please let us know</a>.</p>
<p>Look forward to more from MDN in 2014 and beyond. The MDN platform promises to expand and improve the way we view, write, and experience documentation and web technologies!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/the-mozilla-developer-network-has-a-new-face/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Holiday gaming competition with Mozilla and Goo Technologies</title>
		<link>https://hacks.mozilla.org/2013/12/holiday-gaming-competition-with-mozilla-and-goo-technologies/</link>
		<comments>https://hacks.mozilla.org/2013/12/holiday-gaming-competition-with-mozilla-and-goo-technologies/#comments</comments>
		<pubDate>Mon, 09 Dec 2013 12:24:59 +0000</pubDate>
		<dc:creator><![CDATA[Robert Nyman [Editor]]]></dc:creator>
				<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23282</guid>
		<description><![CDATA[The web has come a long way, and especially lately it is amazing what great opportunities this means for game creators. To use open technologies to reach as many users as possible across multiple web browsers and platforms. Looking at what WebGL, Web Audio API, Emscripten, asm.js and more have brought to the table, and [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>The web has come a long way, and especially lately it is amazing what great opportunities this means for game creators. To use open technologies to reach as many users as possible across multiple web browsers and platforms.</p>
<p>Looking at what <a href="https://developer.mozilla.org/en-US/docs/Web/WebGL">WebGL</a>, <a href="https://developer.mozilla.org/en-US/docs/Web_Audio_API">Web Audio API</a>, <a href="https://github.com/kripken/emscripten/wiki">Emscripten</a>, <a href="http://asmjs.org/">asm.js</a> and more have brought to the table, and what we&#8217;ve covered in articles here on Mozilla Hacks about <a href="https://hacks.mozilla.org/category/games-2/">gaming</a>, <a href="https://hacks.mozilla.org/category/webgl/">WebGL</a>, <a href="https://hacks.mozilla.org/category/audio/">Audio</a> etc, we are very excited to see the progress for gaming on the web.</p>
<p>Thinking about all this, we wanted to find an interesting way to encourage you to see what kind of games you could create! Together with <a href="http://www.gootechnologies.com/">Goo Technologies</a>, we&#8217;re bringing you <a href="http://create.gootechnologies.com/mozilla-xmas-contest">Goo’s Game Creator Challenge</a>.</p>
<p><a href="http://create.gootechnologies.com/mozilla-xmas-contest"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/12/mozilla-goo-xmas-contest.png" alt=""></a></p>
<p>You will be using the Goo platform to create your games, which consists of:</p>
<ul>
<li><a href="http://www.gootechnologies.com/products/engine/">Goo Engine</a> – a 3D JavaScript gaming engine entirely built on WebGL/HTML5</li>
<li><a href="http://www.gootechnologies.com/products/create/">Goo Create</a> – a visual editing tool running on top of the engine</li>
</ul>
<h3>Categories</h3>
<p>There are three different categories you can enter in:</p>
<ul>
<li><strong>Best Amateur Interactive Game Scene</strong> &#8211; Create a visually stunning interactive game scene using only the built in object primitives (simple 3D shapes, such as cubes, spheres, and so forth, that can be manipulated and combined together to create characters or other objects in a scene). This category is for those with little to no previous experience of game creation.</li>
<li><strong>Best <strong>Desktop Game</strong></strong> &#8211; Create an immersive game level for the desktop browser using Goo Create in combination with the Goo Engine JavaScript API.</li>
<li><strong>Best Mobile Game</strong> &#8211; Create an immersive game level for the mobile Web using Goo Create in combination with Goo Engine JavaScript API – extra credit will be awarded for cross-platform games that also work on desktop.</li>
</ul>
<h2>Getting started</h2>
<p>If you need inspiration to get started, a good place to start is <a href="http://www.gootechnologies.com/learn/create/goon">Build an interactive scene in Goo Create</a> or look at the Creating a Goon in Goo Create video:</p>
<p><iframe src="//player.vimeo.com/video/77723187?title=0&amp;byline=0&amp;portrait=0" width="500" height="313" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></p>
<p>Other good resources are:</p>
<ul>
<li><a href="http://www.gootechnologies.com/learn/create/santa/">Build JetPack Santa using primitives in Goo Create</a></li>
<li><a href="http://www.gootechnologies.com/learn/engine/tutorials/christmas-special-example-project/">Use Goo Create together with Goo Engine JS API for a desktop- or mobile game</a></li>
</ul>
<p>Also, if you need help, you can go to the <a href="http://www.gootechnologies.com/community/">forums for Goo Technologies</a>.</p>
<h2>Prizes</h2>
<p>As with any challenge, there are prizes! For the people entering the <strong>Best Amateur Interactive Game Scene</strong> category, the top 5 contributions will win:</p>
<ul>
<li>$1,000 in cash</li>
<li>5 year access to Goo Create Pro ($2900 value)</li>
</ul>
<p>For the <strong>Best Desktop Game</strong> and <strong>Best Mobile Game</strong> categories, the prizes are:</p>
<ul>
<li>A trip for two people to the <a href="http://www.gdconf.com/">GDC Conference in San Francisco</a>, scheduled for March 17 – 21, 2014 or to <a href="http://www.gamescom-cologne.com/en/gamescom/home/index.php">GamesCom </a> in Cologne, August 13-17 (maximum value $10,000) or $5,000 cash, at the winner’s option.</li>
<li>5 year access to Goo Create Pro ($2900 value)</li>
<li>Guest post on the Mozilla Apps blog</li>
</ul>
<h3>Judges</h3>
<p>Judging the challenge are people from Rovio (know for Angry Birds and more), Mozilla (Brendan Eich, CTO and inventor of JavaScript, Vlad Vukicevic, Engineering Director and inventor of WebGL) and Goo Technologies (Rikard Herlitz, CTO). They will pick the winners by January 17th.</p>
<h2>Deadline for entry</h2>
<p>Deadline for submitting entries is <strong>Tuesday, January 14 at midnight PT</strong>. Submit your entry by sending the url of your finished scene or game to <a href="mailto:mozgoocreate@mozilla.com">mozgoocreate@mozilla.com.</a></p>
<h2>For more information:</h2>
<ul>
<li><a href="http://gooengine.tumblr.com/post/69076624934/mozgoocreate">Read more details, guidance on the three categories and FAQ</a></li>
<li><a href="https://wiki.mozilla.org/Game_Creator_Challenge_-Contest_Terms_and_Conditions">Read the Official Rules</a></li>
<li><a href="http://www.mozilla.org/privacy-policy.html">Read the Mozilla Privacy Policy</a> and the <a href="http://about.gootechnologies.com/legal/privacy">Goo Privacy Policy</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/holiday-gaming-competition-with-mozilla-and-goo-technologies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protecting your Firefox OS App code</title>
		<link>https://hacks.mozilla.org/2013/12/protecting-your-firefox-os-app-code/</link>
		<comments>https://hacks.mozilla.org/2013/12/protecting-your-firefox-os-app-code/#comments</comments>
		<pubDate>Thu, 05 Dec 2013 16:05:34 +0000</pubDate>
		<dc:creator><![CDATA[Harald Kirschner]]></dc:creator>
				<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Minification]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[Protection]]></category>
		<category><![CDATA[Wev Service]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23216</guid>
		<description><![CDATA[One question we get asked quite often about developing web applications using HTML5, CSS3, and JavaScript, is about how to conceal code. Reasons range from preventing cheating, protecting intellectual property, hiding encryption algorithms and others. Since JavaScript is executed on the client side, even on Firefox OS, the users can get access the code if [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>One question we get asked quite often about developing web applications using HTML5, CSS3, and JavaScript, is about how to conceal code. Reasons range from preventing cheating, protecting intellectual property, hiding encryption algorithms and others. Since JavaScript is executed on the client side, even on Firefox OS, the users can get access the code if they have the skills and spend the time. But don&#8217;t get the wrong idea! Applications on Android, iOS and most mobile platforms can be reverse  engineered with effort and the right tools.</p>
<p>We will present some measures that make it harder for copycats to reveal your application&#8217;s code. In various degrees they will increase the effort, skills and tools required to get the code.</p>
<h2>Compressing your code</h2>
<p>This is the starting point for most developers. Compressing your code reduces the file size by removing what the browser doesn&#8217;t need to run your application. It starts by stripping comments and unnecessary white space. This is a good first step as comments usually reveal a lot of information on how the code is working.</p>
<p>The next level is shortening names of local variables and functions, the so called mangling. As this removes the meaning on those symbols, it further reduces readability.</p>
<p>More advanced compression tools, like <a href="http://lisperator.net/uglifyjs/">UglifyJS</a>, will actually parse your JavaScript to analyze and transform it. This allows not just renaming local symbols but also rewriting conditions and loops into shorter expressions, joining variable declarations (hoisting) and removing unreachable code. The resulting file is not only smaller and less readable but in many cases also faster.</p>
<p>You can take this one step further with the Google Closure optimizer in Advanced mode. The tool allows settings for all the aforementioned transformations. Advanced mode is the most aggressive and requires <a href="https://developers.google.com/closure/compiler/docs/api-tutorial3">greater consideration</a> when used, to ensure that the output runs just as the input. It will rename, inline and even remove functions, properties and variables, so you need to make sure to either reference all symbols correctly or annotate them. The extra work pays off as the result is highly compressed and even gains a performance boost. As an example, let&#8217;s see what can happen to this code: </p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> shareImage <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#share-image&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">function</span> share<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> imgToShare <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#image-to-share&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>imgToShare.<span style="color: #660066;">naturalWidth</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Image failed to load, can't be shared&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Create dummy canvas</span>
    <span style="color: #000066; font-weight: bold;">var</span> blobCanvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    blobCanvas.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> imgToShare.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
    blobCanvas.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> imgToShare.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Get context and draw image</span>
    <span style="color: #000066; font-weight: bold;">var</span> blobCanvasContext <span style="color: #339933;">=</span> blobCanvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    blobCanvasContext.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>imgToShare<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Export to blob and share through a Web Activitiy</span>
    blobCanvas.<span style="color: #660066;">toBlob</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>blob<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> sharingImage <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;share&quot;</span><span style="color: #339933;">,</span>
        data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
          type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;image/*&quot;</span><span style="color: #339933;">,</span>
          number<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
          blobs<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>blob<span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  shareImage.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> share<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once we&#8217;ll run this code through <a href="http://closure-compiler.appspot.com/home">Google Closure</a>, the code will look like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> b<span style="color: #339933;">=</span>document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#share-image&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>        
c<span style="color: #339933;">=</span>document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#image-to-share&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>b<span style="color: #339933;">&amp;&amp;</span>c<span style="color: #339933;">&amp;&amp;</span>       
<span style="color: #009900;">&#40;</span>b.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">&lt;</span>c.<span style="color: #660066;">naturalWidth</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>      
<span style="color: #000066; font-weight: bold;">var</span> a<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>a.<span style="color: #660066;">width</span><span style="color: #339933;">=</span>c.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>     
a.<span style="color: #660066;">height</span><span style="color: #339933;">=</span>c.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>a.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       
a.<span style="color: #660066;">c</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>name<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;share&quot;</span><span style="color: #339933;">,</span>       
data<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;image/*&quot;</span><span style="color: #339933;">,</span>b<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>a<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>         
<span style="color: #000066; font-weight: bold;">else</span> alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Image failed to load, can't be shared&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Of course, the further you are writing custom logic, the more the code will be obfuscated, as the tools cannot change names from reserved word, or core functions. There are many tools online, and you can  choose the one that fit your need. Most web frameworks offer interchangeable libraries and build steps that bundle and compress your JavaScript sources. For smaller projects your IDE, like Sublime Text, can be extended with <a href="https://sublime.wbond.net/search/minify"  title="List of Minifying packages for  SublimeText">packages</a> available.</p>
<h2>Using Emscripten</h2>
<p>Did you remember that we said JavaScript is interpreted code? Well, you can bend that rule. <a href="https://github.com/kripken/emscripten/wiki" title="Emscripten site">Mozilla&#8217;s Emscripten tool</a> compiles C and C++ to JavaScript. The output isn&#8217;t JavaScript as you know it. It behaves more like native code and manages its own memory (stack and heap) in pre-allocated typed arrays. This reduces garbage collection and makes inspecting the memory harder. The output also benefits from asm.js, a highly optimizable subset of JS, that runs on all modern browsers. If you are moving your app from native to the web you can just convert your existing libraries. But even if that doesn&#8217;t apply, moving your protected code into a C/C++ library that you compile to JavaScript is the best option to get blazing performance with the added benefits of obfuscation.</p>
<h2>Using a web service</h2>
<p>Compression and compilation isn&#8217;t the safest solution as  people still have access to your code: it&#8217;s just a bit harder to understand the logic. On the other side, one technique that moves the code to a place you have more control over is to use a web service. Your secret logic would run as a server-side script that your application will call when needed. Someone might be able to inspect the in- and outgoing data but there is no way they will be able to view your logic without access to your server. The major downside is that this feature will be only available when the user is online.</p>
<p>Let&#8217;s see a simple example on how you can achieve this with a Firefox OS app, and a Node.js script: let&#8217;s say we would like to hide our super exceptional fantastic code to do the addition to two numbers. Here is a simple node.js script (the goal is not to teach node.js, but show you a working basic example): </p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;use strict&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> express <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'express'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> app <span style="color: #339933;">=</span> express<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Set up express to parse body</span>
app.<span style="color: #660066;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">json</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
app.<span style="color: #660066;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">urlencoded</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
app.<span style="color: #660066;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">multipart</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Only one route for web service</span>
app.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Convert parameters from request body</span>
  <span style="color: #000066; font-weight: bold;">var</span> firstNumber <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">body</span>.<span style="color: #660066;">firstNumber</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> secondNumber <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">body</span>.<span style="color: #660066;">secondNumber</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isNaN<span style="color: #009900;">&#40;</span>firstNumber<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> isNaN<span style="color: #009900;">&#40;</span>secondNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// Bad request, fail with 400 status</span>
      <span style="color: #000066; font-weight: bold;">return</span> req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> firstNumber <span style="color: #339933;">+</span> secondNumber<span style="color: #339933;">;</span>
  res.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'result'</span><span style="color: #339933;">:</span> result<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now in your app, you&#8217;ll call this script by sending the values you want to add, and the script will return you the result. Of course, users can see the call you make, and the results you&#8217;ll get, but they won&#8217;t be able to see the magic behind the hidden function.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;use strict&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> firstNumber <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> secondNumber <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> FormData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstNumber&quot;</span><span style="color: #339933;">,</span> firstNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
data.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;secondNumber&quot;</span><span style="color: #339933;">,</span> secondNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>mozSystem<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;http://fharper-nodejs.nodejitsu.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
xhr.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Parse the response and do something with it</span>
    <span style="color: #000066; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>response.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
xhr.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Magic failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This code is just showing the web service pattern and not best practices. As any server, it still needs security measures to prevent exploits. Make sure you mask the request and response data by using a secure HTTPS connection. Control client access to your service by checking the Referrer and Origin HTTP headers but don&#8217;t trust what you see as they can be faked. You can limit replaying request with malicious parameters by using adding a secret time-limited or single-use token. As you can see, having your code in a web service is keeping it off the client but opens different attack vectors you need to consider.</p>
<h2>Be proactive, open your code</h2>
<p>Of course, the best approach is to open your code: make your application open source, choose the right license, and show to the rest of the world that you were the first to create this piece of art. Choose the technology you want: you will make it harder to copy your code, but there is never a way to hide your idea. You need to be proactive: you need to be one of the best in your domain. Show your expertise, innovate, add features faster than the competitor do, be relevant for your market, and believe in what you do.  How many knock offs of Angry Birds can you find on the web or on any  marketplace/store? I don&#8217;t have the exact number, but I can definitely  say with a lot of confidence, that it&#8217;s many! Rovio had an amazing idea, and they were the first to make it a reality: they are always  innovating with brand new levels, new games with the characters we like, and more. You, your children, myself, we want to play to the original one, not the copy. Replace Angry Birds with any app or games you like, and you&#8217;ll understand what I mean.</p>
<p>You have access to an amazing platform that open the web like never before: take advantage of it, build that app you want, and make your idea a reality!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/protecting-your-firefox-os-app-code/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>How the Manana app was built</title>
		<link>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/</link>
		<comments>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/#comments</comments>
		<pubDate>Tue, 03 Dec 2013 09:40:23 +0000</pubDate>
		<dc:creator><![CDATA[Alberto Granzotto]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox OS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23160</guid>
		<description><![CDATA[We saw Firefox OS as a great opportunity and a challenge to deliver a product true to the values of open web and best standards. We found it exciting to be able to deliver an app that will offer a smooth UX even for people using lower end devices. As many of the users have [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>We saw Firefox OS as a great opportunity and a challenge to deliver a product true to the values of open web and best standards. We found it exciting to be able to deliver an app that will offer a smooth UX even for people using lower end devices. As many of the users have a very limited data plan we decided that a proper reading app offering the possibility to enjoy their favourite articles offline and without unnecessary distractions would be a key product. And so we decided to build Manana.</p>
<p>Here is the story of how we did it.</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/01-Splashpage.png" /></p>
<p>On a technological side, we decided to go vanilla but with a little help from <a href="http://zeptojs.com/">ZeptoJS</a> to handle the DOM manipulation. We also integrated Web Activities to hook into the sharing options in the browser, and to share the selected links via email. We are using also the Readability API to have a nice formatted version of the link. The app is simple and that&#8217;s how we wanted it to be. but we have a great plus: it is internationalized in English, Spanish, Portuguese, Polish, German, Italian. Hungarian and Greek are coming soon.</p>
<p>You can <a href="https://marketplace.firefox.com/app/manana">install Manana from the Firefox Marketplace</a>.</p>
<p><a href="https://marketplace.firefox.com/app/manana"><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/11/manana-marketplace.png" /></a></p>
<h2>Building the layout and managing transitions between the views</h2>
<p>To provide a branded UI/UX, we use a small framework built in-house. With some clever CSS properties and a few lines of JavaScript (to handle the visibility of the Views) the whole thing was quite easy to set up. Basically, we have each View arranged like cards in a deck. Every time we need one of them, we pull it out while the old one goes in the deck again (becoming hidden). When the animation is ended, we capture the <code>animationend</code> event, so right after that we remove the class that brings the animation properties and at the same time we add a <code>current</code> class, which lets the card stay in place and be visible. The old one is hidden automatically since that&#8217;s its default state.</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/03-Drawer.png" /></p>
<p>Below you will find a small example of how it works with a <code>moveRight</code> animation.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/how-Views-move.gif"><img title="Showing the way Views move" alt="Showing the way Views move" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/animation-gif-app.gif" /></a></p>
<h2>Optimize CSS3 transitions</h2>
<p>Every smartphone comes with at least a small GPU, so that the CPU doesn’t have to do all the graphical calculations. It’s very much in fashion to take advantage of the Hardware Acceleration and so we do. There are some CSS3 properties that are automatically handled by the GPU, but others are not &#8211; usually the ones that are related to 3D. This is because the element (tied to the animated property) is <em>promoted</em> to a new layer that doesn’t suffer from repaints imposed by other properties (like <code>left</code>, for example. But this can change in the future).</p>
<p><strong>tl;dr</strong> Where you can, avoid expensive CPU work, especially on big or complicated elements. Here is a small extract of how we implemented such properties.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* moveLeftIn directive */</span>
<span style="color: #6666ff;">.moveLeftIn</span> <span style="color: #00AA00;">&#123;</span>
    animation<span style="color: #00AA00;">:</span> 0.5s moveLeftIn cubic-bezier<span style="color: #00AA00;">&#40;</span>…<span style="color: #00AA00;">&#41;</span> forwards<span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 1 */</span>
    backface-visibility<span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 2 */</span>
    transform<span style="color: #00AA00;">:</span> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #933;">99%</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 3 */</span>
    <span style="color: #808080; font-style: italic;">/* 1. `forwards` let the animation stop at the last keyframe */</span>
    <span style="color: #808080; font-style: italic;">/* 2. avoid some possible glitches caused by 3D rendering */</span>
    <span style="color: #808080; font-style: italic;">/* 3. take advantage of the GPU with 3D-related properties */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* moveLeftIn animation */</span>
<span style="color: #a1a100;">@keyframes moveLeftIn {</span>
    <span style="color: #933;">0%</span>   <span style="color: #00AA00;">&#123;</span> transform<span style="color: #00AA00;">:</span> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #933;">99%</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">999</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
    <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#123;</span> transform<span style="color: #00AA00;">:</span> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>   <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">999</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h2>Hooking to the Firefox OS specific Web Activities</h2>
<p>One of our favourite APIs of the WebAPIs, is <a href="https://developer.mozilla.org/en-US/docs/WebAPI/Web_Activities">Web Activities</a>. An application emits an Activity (for example, <em>save bookmark</em>) then the user is prompted to chose among other applications that handles that activity. At the moment it is possible to register an application as an activity handler through the app manifest (<em>declaration registration</em>), but in the future this could be done inside the app itself (<em>dynamic registration</em>).</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/04-SaveToManana.png" /></p>
<p>Saving a link from the browser to Manana is an example of Web Activity (<em>save-bookmark</em>). Registering an activity is trivial:</p>
<h3>manifest.webapp</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="json" style="font-family:monospace;">&quot;activities&quot;: {
    &quot;save-bookmark&quot;: {
      &quot;disposition&quot;: &quot;inline&quot;,
        &quot;index.html#/savebookmark&quot;,
        &quot;returnValue&quot;: true
    }
},</pre></td></tr></table></div>

<p><strong>returnValue</strong> is of great importance, otherwise the handler application (Manana in our case) will never return to the caller (Browser). The handling in the app is also very easy:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">navigator.<span style="color: #660066;">mozSetMessageHandler</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;activity&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>activity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ... <span style="color: #660066;">check</span> <span style="color: #000066; font-weight: bold;">if</span> activity is savebookmark
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> activity.<span style="color: #660066;">source</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
&nbsp;
    ... <span style="color: #000066; font-weight: bold;">do</span> stuff with activity data
&nbsp;
    activity.<span style="color: #660066;">postResult</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;saved&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/05-ArticleSaved.png" /></p>
<p>The applications included in your phone are part of <a href="https://github.com/mozilla-b2g/gaia">Gaia</a>, the UI of Firefox OS, and you can actually browse the source. They provide a lot of examples on how to use WebAPIs. If you are interested in the <strong>save-bookmark</strong> activity, have a look at the <a href="https://github.com/mozilla-b2g/gaia/tree/master/apps/homescreen">Homescreen app source code</a>.</p>
<p>Manana users can also share their resources via email, directly from the app. To achieve that, we&#8217;ve implemented the <code>email</code> Web Activity. It&#8217;s easy to pre-populate the email subject and body, just follow the <code>mailto</code> URI scheme and you are done.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">DB.<span style="color: #660066;">Get</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>rs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> subject       <span style="color: #339933;">=</span> rs.<span style="color: #660066;">title</span><span style="color: #339933;">,</span>
        body          <span style="color: #339933;">=</span> rs.<span style="color: #660066;">url</span><span style="color: #339933;">,</span>
        shareActivity <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;new&quot;</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mail&quot;</span><span style="color: #339933;">,</span>
                url <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mailto:?body=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
                      <span style="color: #3366CC;">&quot;&amp;subject=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Retrieving and storing data with IndexedDB</h2>
<p>IndexedDB is a client side NoSQL DB. It has some nice features which make it easier working with app updates and is easy to integrate in your app, even if you&#8217;re using a framework like AngularJS. Setting up models is fairly easy and extensively documented on the web and in Gaia Apps.</p>
<p>You don&#8217;t have a quota limit, although the Firefox OS Simulator seems to have a limit when dealing with datasets larger than 5mb. Each database is tied to an app and a version. Only one version can run at a time, which makes it easier not to break compatibility, especially if you rely on some external API.</p>
<p>Since it is asynchronous, dealing with IndexedDB operations requires having callback. Below is an example on how we deal with the action of adding a link:</p>
<ol>
<li>Call Link.Add function, passing a link object and a success callback</li>
<li>Link.Add get a DB connection by calling getDB
<ul>
<li>In the case a DB does not exist, the <strong>onupgradeneeded</strong> callback is executed
<ul>
<li>Create DB</li>
<li>Create collections</li>
<li>Create indexes</li>
</ul>
</li>
<li>getDB check if the connection is already open, otherwise it opens a new one</li>
</ul>
</li>
<li>Link.Add fills the missing property of links with default values</li>
<li>We call the objectStore <strong>put</strong> method to save/update new data into the collection
<ul>
<li>If the Link object we are going to save already contains a primary key attribute, IndexedDB does an update</li>
<li>Otherwise, an insert</li>
</ul>
</li>
<li>If the <strong>put</strong> method is successful it calls a success callback, returning the primary key in <strong>event.target.result</strong></li>
</ol>
<p>Below is an example of DB action defined in our models: (for the sake of brevity we omitted error checking code)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Add<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> onsuccess<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    getDB<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>db<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> transaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;links&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;readwrite&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                objectStore <span style="color: #339933;">=</span> transaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;links&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                request <span style="color: #339933;">=</span> objectStore.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            request.<span style="color: #660066;">onsucces</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                onsuccess<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// e.target.result is the id of the new Link</span>
            <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>This is how you call it from the other part of the app:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> newLink <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://zombo.com&quot;</span><span style="color: #339933;">,</span>
               title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Welcome to zombo com&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Link.<span style="color: #660066;">Add</span><span style="color: #009900;">&#40;</span>newLink<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>linkId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#/read/:&quot;</span> <span style="color: #339933;">+</span> linkId<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Enhance localization with Markdown support</h2>
<p>Localization is a key feature in our app. Manana currently supports 6 languages (soon it will be 8). There is no official recommendation, but after some research we decided to use the same <a href="https://hacks.mozilla.org/2013/08/localizing-firefox-os-apps/">technique</a> that Gaia is using. This is a great <em>feature</em> of working with Firefox OS: the whole source code is there, and you can get inspiration by reading it.</p>
<p>L10n has been used in two different cases: to translate the <code>text</code> content of DOM elements and to translate internal help pages. To make the Localization process as simple as possible, we avoided putting HTML tags in the strings and in the internal help pages; we used markdown instead. By design, we avoided having strings in the Javascript code, all messages and alerts are contained in the HTML.</p>
<p>To support markdown we added some glue between the l10n.js library and <a href="https://github.com/chjj/marked">marked.js</a>.</p>
<p>To load the help resources, Manana first does a lookup in the current installation to find the file named <code>&lt;resourceName&gt;.&lt;locale&gt;.md</code>. If the lookup fails, the fallback is the English version of the resource. Once the file is retrieved succesfully, the content is applied to the <code>marked</code> function to generate the HTML, and then added to the DOM.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> resourceInit<span style="color: #009900;">&#40;</span>$oldView<span style="color: #339933;">,</span> $newView<span style="color: #339933;">,</span> resource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> fallback  <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/locales/resources/&quot;</span> <span style="color: #339933;">+</span> resource <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.en.md&quot;</span><span style="color: #339933;">,</span>
        localized <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/locales/resources/&quot;</span> <span style="color: #339933;">+</span> resource <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">+</span> getLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.md&quot;</span><span style="color: #339933;">,</span>
        renderMd  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.js-subpage&quot;</span><span style="color: #339933;">,</span> $newView<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>marked<span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        renderError  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.js-subpage&quot;</span><span style="color: #339933;">,</span> $newView<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ouch, cannot find: &quot;</span> <span style="color: #339933;">+</span> resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    getFile<span style="color: #009900;">&#40;</span>localized<span style="color: #339933;">,</span>
            renderMd<span style="color: #339933;">,</span>
            <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> getFile<span style="color: #009900;">&#40;</span>fallback<span style="color: #339933;">,</span> renderMd<span style="color: #339933;">,</span> renderError<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>A note on retrieving local files with a XMLHttpRequest</h2>
<p>Retrieving files asynchronously from your local app is as easy as doing a <code>XMLHttpRequest</code>, but there is a case that took us some time to sort out. If the local file you are retrieving is missing, the request won&#8217;t fire the <code>success</code> callback (of course), but neither the <code>error</code> callback. It will just hang there forever.</p>
<p>After some research, we noticed a <code>try { ... } catch</code> block in the <code>l10n.js</code> library. Basically, &#8220;in Firefox OS with the <code>app://</code> protocol, trying to XHR a non-existing URL will raise an exception.&#8221;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> getFile <span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> success<span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/plain;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span> <span style="color: #339933;">||</span> xhr.<span style="color: #660066;">status</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
                success<span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span>
                error <span style="color: #339933;">&amp;&amp;</span> error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> error<span style="color: #339933;">;</span>
    xhr.<span style="color: #660066;">ontimeout</span> <span style="color: #339933;">=</span> error<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Here it is!</span>
    <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        error <span style="color: #339933;">&amp;&amp;</span> error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Keeping the UI synchronized with the Database</h2>
<p>One of the most challenging aspect of an HTML app is synchronization of state between a data source (db, file, api, etc) and the UI. Frameworks like Backbone and Angular make your life easier but they are not easy beasts to tame. Going vanilla is fun, mostly because JavaScript gives you a lot of flexibility when deciding how to do things.</p>
<p>We really like OO programming by message passing, so we came up with a message based architecture. The <code>body</code> element acts as our global dispatcher while view root element is the local dispatcher. Global events get registered when the application starts and live as long as the app. Local events get registered when entering a view and live until we leave that view.</p>
<p>An example of global events are the CRUD operations we want to perform on our model.</p>
<p>Each action on the UI which changes the state of models is tied to a global event, registered on <code>document.body</code>. On entering a new view, we call a constructor function, which executes some initialization code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> events <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// global events are never unbinded</span>
        global<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;save-link&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;remove-link&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            ...
&nbsp;
            <span style="color: #3366CC;">&quot;update-link-collecetion&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            ...
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// view specific events are unbinded everytime we leave that view</span>
        home<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;update-link-collection-complete&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> $caller <span style="color: #339933;">=</span> e.<span style="color: #660066;">detail</span>.$caller<span style="color: #339933;">;</span>
&nbsp;
                ...
            <span style="color: #009900;">&#125;</span>  
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        ...
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> homeInit<span style="color: #009900;">&#40;</span>$oldView<span style="color: #339933;">,</span> $newView<span style="color: #339933;">,</span> dispatch<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// $oldView is the previous view</span>
        <span style="color: #006600; font-style: italic;">// $newView is the current view</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// dispatch in an helper which wrap </span>
        <span style="color: #006600; font-style: italic;">// document.body.dispatchEvent(new CustomEvent(...))</span>
        dispatch<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;update-link-collection&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Leaving a view calls a destructor on that view, and unbinds all the local events. At the end of the day we can open the app source code without spending hours trying to figure out how things work because of framework related black magic.</p>
<h2>Integration with git</h2>
<p>When it comes to Git, our hearts melt. We love git. Seriously. Git and Vim are the best pieces of software ever written. Love.</p>
<p>Ok, lets get serious again.</p>
<p>Our application is composed from two components, the app itself and an API Server. The API server is a Golang API proxy to Readability API. So our app directory layout looks like this:</p>
<pre>manana/
 |- distrib.sh
 |- srv/
 |- app/</pre>
<p><strong>distrib.sh</strong> is a simple bash script which perform three simple tasks:</p>
<ul>
<li>Install a post-{commit, merge, rebase} hook, if not already installed</li>
<li>Update <strong>app/js/revision.js</strong> writing the current git revision hash.</li>
<li>Create a clean zip containing the app, ready for the app store or your device.</li>
</ul>
<p>This makes our lives easier.</p>
<h2>Conclusion. What Next?</h2>
<p>Manana relies on the Readability API to get the stripped down version of the web page. Readability is doing a really good job, but we&#8217;d like to experiment with other solutions, like parsing the page directly in the device. It&#8217;s a hard job, but it&#8217;d be cool to do it on the client side, and some work has already <a href="http://code.google.com/p/arc90labs-readability/source/browse/trunk/js/readability.js">been done</a>.</p>
<p>We had a great time developing for Firefox OS. The most important thing for us, techwise, is that we developed a webapp, not an <strong>OS-specific app</strong>. We can use all the know-how we gained in the past years, all the great open source libraries we want, and we can even use the browser debug tools we use every day.</p>
<p>To help newcomers in building a Firefox OS app, we have created <a href="http://tmp.devcharm.com/pages/10-resources-you-need-to-develop-a-FirefoxOS-app">a list of resources</a> we found indispensable.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Firefox and FireCAT as a Platform for Ethical Hacking</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/#comments</comments>
		<pubDate>Thu, 28 Nov 2013 09:19:46 +0000</pubDate>
		<dc:creator><![CDATA[Maximiliano Soler]]></dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23129</guid>
		<description><![CDATA[Some years ago &#8211; in early 2007, while working as freelancers &#8211; we were challenged to do a penetration test on a web application. It was really simple but had a condition-based methodology, and therefore was impossible to use any automated tool; we could only use a web browser. Our solution was to convert Firefox [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Some years ago &#8211; in early 2007, while working as freelancers &#8211; we were challenged to do a penetration test on a web application. It was really simple but had a condition-based methodology, and therefore was impossible to use any automated tool; we could only use a web browser.</p>
<p>Our solution was to convert Firefox into a Platform for Ethical Hacking.</p>
<h2>How did we do it?</h2>
<p>We searched the Web, looking for a solution that satisfied the requirements and obligations to not use automated tools. We found many, a lot of them being add-ons and extensions. Many of them were unknown, unnoticed and unofficial.</p>
<p>Having compiled some useful findings, our next challenge was to create something that maintained the essence of Mozilla Firefox: open, free and easy for everyone. The project was named <a href="http://firecat.toolswatch.org">FireCAT (Firefox Catalog of Auditing exTensions)</a></p>
<p><a href="http://firecat.toolswatch.org"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/Firecat.png" alt="" /></a></p>
<h2>What is FireCAT?</h2>
<p>FireCAT is a mind map organized into different categories and subcategories with a variety of add-ons available. These are themed around application security auditing and assessment, adding news functionality, customizing your browser and providing the ability to adapt it to different user&#8217;s need.</p>
<h3>Current Status</h3>
<p>The <a href="http://firecat.toolswatch.org/news.html">latest version is 2.0</a>, and was presented at Black Hat Arsenal during 2011. From the web site it is possible to list the content and browse the categories, directly accessing the add-ons on <a href="https://addons.mozilla.org/en-US/firefox/">Mozilla Add-ons</a>; you can also download the entire mind map with the categories:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_000_Mindmap.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_000_Mindmap-500.png" alt="" /></a></p>
<h3>What&#8217;s New?</h3>
<ul>
<li>Current Version: 2.0</li>
<li>Add-ons: > 90</li>
<li>Categories: 7</li>
<li>Subcategories: 19</li>
</ul>
<p>FireCAT v2.0 &#8211; <a href="http://firecat.toolswatch.org/the_catalog.html">Firefox Catalog of Auditing exTensions<br />
</a></p>
<ul>
<li>Information Gathering
<ul>
<li>Whois</li>
<li>Location Info</li>
<li>Enumeration &amp; Fingerprint</li>
<li>Data Mining</li>
<li>Googling &amp; Spidering</li>
</ul>
</li>
<li>Proxies &amp; Web Utilities</li>
<li>Editors</li>
<li>Network Utilities
<ul>
<li>Intrusion Detection System</li>
<li>Sniffers</li>
<li>Wireless</li>
<li>Passwords</li>
<li>Protocols &amp; Applications</li>
</ul>
</li>
<li>Misc
<ul>
<li>Tweaks &amp; Hacks</li>
<li>Encryption / Hashing</li>
<li>Antivirus &amp; Malware Scanner</li>
<li>Anti Spoof</li>
<li>Anti-phishing / Pharming / Jacking</li>
<li>Automation</li>
<li>Logs / History</li>
<li>Backup &amp; Synchronization</li>
<li>Protection</li>
</ul>
</li>
<li>IT Security Related</li>
<li>Application Auditing</li>
</ul>
<h2>What can we find in the categories?</h2>
<p>The 7 main categories are as follows:</p>
<h3>Information Gathering</h3>
<p>An important part of the process of conducting vulnerability assessment or penetration testing is related to obtaining information in a passive or active way about our objective, to be analyzed later.</p>
<p>From the Firefox platform, we could identify IP addresses, query DNS, verify HTTP headers, generate reports of domains via Whois, use different search engines, and identify applications and versions from Web Server, modules, etc.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_001_Show-IP.png" alt="" /></p>
<p>&#8220;Show IP&#8221; shows the IP address(es) of the current page in the status bar. It also allows querying of custom services by IP (right mouse button) and hostname (left mouse button), like whois, netcraft, etc.</p>
<h3>Proxies &amp; Web Utilities</h3>
<p>Sometimes it&#8217;s necessary to use proxies to connect with different hosts or through networks for exaple using TOR to hide our real IP, analyze time responses, detect load balancer, etc.</p>
<h4>Example</h4>
<p>The User Agent Switcher extension adds a menu and a toolbar button to switch the user agent of the browser:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_002_User-Agent-Switcher.png" alt="" /></p>
<h3>Editors</h3>
<p>From the ubiquitous FireBug to different JavaScript or XML editors, the available editors can help us to visualize and modify content as required.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_003_JavaScript_Deobfuscator.png" alt="" /></p>
<p>The JavaScript Deobfuscator will show you what JavaScript gets to run on a web page, even if it is obfuscated and generated on the fly.</p>
<h3>Network Utilities</h3>
<p>In this section you can use and connect with applications and services such as FTP, DNS and databases in real time.</p>
<h3>Miscellaneous</h3>
<p>This section contains a lot of useful add-ons, for example tools to convert your ASCII into md5, sha1, base64, hex, etc., and methods to identify the WOT (Web of Trust) in a web site, guage reputation and ratings, modify your HTTP Referer, automate actions through Macros, and make backups of your configuration and installed addons.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_004_Certificate-Patrol.png" alt="" /></p>
<p>The Certificate Patrol add-on reveals when certificates are updated, so you can ensure it was a legitimate change.</p>
<h3>IT Security Related</h3>
<p>Sometimes it is necessary to use search engines to find information within some Web sites, content related to errors, configurations, and news vulnerabilities by CVE.</p>
<h3>Application Auditing</h3>
<p>The HackBar is probably the best known addon in this group, but I recommend you take a deeper look because many of them are useful. For example, Exploit-Me&#8217;s suite to perform vulnerability analysis of cross-site scripting and SQL Injection.</p>
<h4>Example</h4>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_005_SQLite-Manager.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_005_SQLite-Manager-500.png" alt="" /></a></p>
<p>The SQLite Manager allows you to manage any SQLite database on your computer.</p>
<h2>Recommended add-ons</h2>
<p>The top ten recommended add-ons would be:</p>
<ul>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3899">HackBar</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/2464">FoxyProxy</a></li>
<li><a href="http://labs.securitycompass.com/index.php/exploit-me">Exploit Me</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/966">Tamper Data</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3863">iMacros for Firefox</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/5817">SQLite Manager</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3829">Live HTTP Headers</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/6196">PassiveRecon</a></li>
</ul>
<h2>Final Words</h2>
<p>I invite you to explore the different add-ons and extensions available, identifying which ones are most interesting and helpful for your work, and their relative performance and compatibilities. Firefox was a great help during our work!</p>
<p>If you decide to install a large amount of add-ons, memory usage might be a problem: consider this carefully.</p>
<p>Feel free to send us your comments and recommendations for new options to add to the mind map!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing In-App Payments in Your Firefox OS App</title>
		<link>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/</link>
		<comments>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/#comments</comments>
		<pubDate>Tue, 26 Nov 2013 20:29:36 +0000</pubDate>
		<dc:creator><![CDATA[James Long]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Payments]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=21950</guid>
		<description><![CDATA[At Mozilla, we have been working on bringing payments to the web. We think it is important that developers have an easy way to monetize their apps. It also gives us the chance to offer deeper platform integration, allowing things like carrier billing in addition to credit/debit cards. We&#8217;ve talked about in-app payments before in [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>At Mozilla, we have been working on bringing payments to the web. We think it is important that developers have an easy way to monetize their apps. It also gives us the chance to offer deeper platform integration, allowing things like carrier billing in addition to credit/debit cards.</p>
<p>We&#8217;ve talked about in-app payments before in <a href="https://hacks.mozilla.org/2013/04/introducing-navigator-mozpay-for-web-payments/">navigator.mozPay() For Web Payments</a>, but this is an overview from an app developer&#8217;s standpoint. This also includes a few updates, such as real payments being live in the Firefox Marketplace. Payments are different than simply being a paid app, which charges the user once on the initial purchase. In-app payments is a different workflow, and allows you to charge the customer for specific items within your app.</p>
<h2>Architecture Overview</h2>
<p>Payments are only enabled on Firefox OS devices for now. We are working on enabling them across other platforms, even desktop Firefox, but that is a ways off. For the rest of this article, we will assume the Firefox OS environment.</p>
<p>The <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Overview">MDN page for in-app payments</a> has a good overview of the process, but here&#8217;s an even quicker overview. You need a server to generate the payments requests, because otherwise a hacker could easily change the prices to 0. The server creates <a href="http://openid.net/specs/draft-jones-json-web-token-07.html">JWT</a> tokens, passes them to your app, and your app calls <code>navigator.mozPay</code> with the token. The user will be taken to a built-in payments workflow, and when finished, your server will be notified. The <a href="https://hacks.mozilla.org/2013/04/introducing-navigator-mozpay-for-web-payments/">previous Hacks article on payments</a> goes into the lower-level mechanics and reasons for payments more thoroughly.</p>
<p>It&#8217;s up to you to handle the rest of the workflow: updating the app when an item is successfully purchased, linking purchases to accounts, etc.</p>
<h2>Getting a Simulation Key</h2>
<p>In order to use payments, you’ll need use the app key and secret generated for you by the Marketplace. If you just want to simulate payments, you can easily <a href="https://marketplace.firefox.com/developers/in-app-keys/">grab a testing key and secret</a>. The only thing you need to do is create an account if you haven&#8217;t done so already.</p>
<p>You will only be able to simulate payments with that key. When you are ready to use real money, you need to submit your app to the Marketplace, setup payments, and obtain a real key and secret. All of this is explained in more detail below.</p>
<h2>Setting up the Server-Side Code</h2>
<p>The first thing you need to do is set up the server-side code. A &#8220;mozpay&#8221; module exists in <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Code_libraries">a few different languages</a> which implements the payment workflow for you. If it does not exist in your programming language, you&#8217;ll need to use a <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Code_libraries">JWT module</a> and do some more legwork. Check the rather <a href="https://github.com/mozilla/mozpay-js/blob/master/lib/mozpay.js">simple code</a> from <a href="https://github.com/mozilla/mozpay-js">mozpay-js</a> for an example of what you need to do.</p>
<p>I wrote a game for Firefox OS called <a href="https://github.com/jlongster/webfighter">Webfighter</a> which uses <a href="http://nodejs.org/">node.js</a> as the backend, so I used <a href="https://github.com/mozilla/mozpay-js">mozpay-js</a> on the server-side. Webfighter is a shooter with a store where you can buy more weapons and ships, and it&#8217;s a good reference for developers to see actual code for working with payments. Examples below are taken from Webfighter. <a href="https://github.com/jlongster/webfighter">You can check out Webfighter&#8217;s source here.</a></p>
<p>First, require and configure the mozpay module:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> pay <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mozpay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'settings'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
pay.<span style="color: #660066;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    mozPayKey<span style="color: #339933;">:</span> settings.<span style="color: #660066;">payKey</span><span style="color: #339933;">,</span>
    mozPaySecret<span style="color: #339933;">:</span> settings.<span style="color: #660066;">paySecret</span><span style="color: #339933;">,</span>
    mozPayAudience<span style="color: #339933;">:</span> <span style="color: #3366CC;">'marketplace.firefox.com'</span><span style="color: #339933;">,</span>
    mozPayType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'mozilla/payments/pay/v1'</span><span style="color: #339933;">,</span>
    mozPayRoutePrefix<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/mozpay'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The <code>settings</code> module is just a file with JSON that specifies configuration of the app. It&#8217;s nice to separate the configuration so that you can easily swap it out in different environments, like development and production.</p>
<p>Use the key and secret that you got from the Marketplace. You should only change the audience and type options if you are using a different payment server than Mozilla&#8217;s. The routePrefix is optional and specifies the URL prefix for postbacks from the payment server.</p>
<p>Next you need to define a route that creates the data used for the payment transaction. The client is the one making the actual payments request (so that you don&#8217;t have to handle credit card details, and it&#8217;s possible to even be charged through carrier billing). You just need to create an object that represents a purchase. This example uses <a href="http://expressjs.com/">express.js </a>as the http server.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> purchaseQueue <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/sign-jwt'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> token <span style="color: #339933;">=</span> <span style="color: #3366CC;">'o'</span> <span style="color: #339933;">+</span> <span style="">Date</span>.<span style="color: #660066;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> item <span style="color: #339933;">=</span> getItem<span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">body</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> jwt <span style="color: #339933;">=</span> pay.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        id<span style="color: #339933;">:</span> item.<span style="color: #660066;">name</span><span style="color: #339933;">,</span>
        name<span style="color: #339933;">:</span> item.<span style="color: #660066;">name</span><span style="color: #339933;">,</span>
        description<span style="color: #339933;">:</span> item.<span style="color: #660066;">description</span><span style="color: #339933;">,</span>
        icons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'64'</span><span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> item.<span style="color: #660066;">icon</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        pricePoint<span style="color: #339933;">:</span> item.<span style="color: #660066;">price</span><span style="color: #339933;">,</span>
        productData<span style="color: #339933;">:</span> token<span style="color: #339933;">,</span>
        postbackURL<span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'mozpay/postback'</span><span style="color: #339933;">,</span>
        chargebackURL<span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'mozpay/chargeback'</span><span style="color: #339933;">,</span>
        simulate<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> result<span style="color: #339933;">:</span> <span style="color: #3366CC;">'postback'</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Keep track of which JWT objects we are waiting on</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>token<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'processing'</span><span style="color: #339933;">;</span>
&nbsp;
    res.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        jwt<span style="color: #339933;">:</span> jwt<span style="color: #339933;">,</span>
        token<span style="color: #339933;">:</span> token
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice the <code>settings</code> object again; it&#8217;s just a dict that has some configuration properties about our app. <code>settings.url</code> is the base URL where our app is hosted, so that all of the passed URLs are absolute.</p>
<p>We call <code>getItem</code> to get the details about an item (which we don&#8217;t define here). I found it easiest to define a JSON file containing info about all the available items. You can see the <a href="https://github.com/jlongster/webfighter/blob/master/store.js">JSON for available items in Webfighter</a> on GitHub.</p>
<p>There are <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Set_up_your_server_to_sign_JWTs">several fields for a purchase request</a>. <code>id</code> is just a unique identifer, and we simply use the product name. Other fields describe the product, such as <code>icons</code> which lists icons of various sizes. The <code>pricePoint</code> is a number that maps to a specific price for each available region, and you should look at the <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/App_pricing#Pricing_with_price_points">price point table</a> to find the one you need. For example, a price point of 10 indicates a price of $0.99 in the US.</p>
<p>The <code>simulate</code> parameter tells the payments system to just simulate the request. If it&#8217;s passed, no real charges are made and you will be taken through a simulated workflow. This is useful for development. <strong>Just remember</strong> to remove this flag in production.</p>
<p>You can store anything you want to in <code>productData</code>, which you can later use when handling events. Here I generated an arbitrary token to track requests, and I store it in the global object <code>purchaseQueue</code>. We use this when we handle events.</p>
<p>Next, subscribe to the <code>postback</code> event from the mozpay module:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">pay.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'postback'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// the payment was successful</span>
    <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> data.<span style="color: #660066;">request</span><span style="color: #339933;">;</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>req.<span style="color: #660066;">productData</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'success'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can also subscribe to the <code>chargeback</code> event. Chargebacks happen when a customer disputes a charge and gets the money back. This could happen a month later, and is difficult to process. Webfighter keeps track of chargebacks, but the app does not check for them and revoke items. You would need to check periodically or on startup if the user still has access to all purchased items.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">pay.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chargeback'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> data.<span style="color: #660066;">request</span><span style="color: #339933;">;</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>req.<span style="color: #660066;">productData</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'chargeback'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it for the server-side.</p>
<h2>Using mozPay On the Client Side</h2>
<p>On the client-side, you need to post to <code>/sign-swt</code> and use <code>mozPay</code> to initiate the purchase. In the following code, we check if <code>mozPay</code> is available and if it&#8217;s not we just give the user the item. Remember, it&#8217;s just a test app! Once a purchase is initiated, we start polling the server until we get a successful purchase.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> buy<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Purchase an item by requesting a JWT object from the</span>
    <span style="color: #006600; font-style: italic;">// server, and posting it to the mozPay API</span>
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/sign-jwt'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> name<span style="color: #339933;">:</span> name <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">mozPay</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">mozPay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>res.<span style="color: #660066;">jwt</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            req.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mozPay error: '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">error</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                clearPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Poll to see when the payment is complete</span>
            startPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'in-app payments unavailable, so giving it to you for free'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            onPurchase<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here are the polling functions which wait until the server responds with a successful purchase. Note that we only check for a successful purchase. The user will be notified of any errors within the native payment screen, and when they close the screen an error event is fired on the <code>mozPay</code> request object, which we handle and stop the polling. See the above code with <code>req.onerror</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pollQueue<span style="color: #009900;">&#40;</span>token<span style="color: #339933;">,</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Poll the server every second to see the status of our</span>
    <span style="color: #006600; font-style: italic;">// payment request</span>
&nbsp;
    $.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/purchaseQueue?token='</span> <span style="color: #339933;">+</span> token<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>res <span style="color: #339933;">==</span> <span style="color: #3366CC;">'success'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            onPurchase<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            clearPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> pollTimer<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> startPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    pollTimer <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pollQueue<span style="color: #009900;">&#40;</span>res.<span style="color: #660066;">token</span><span style="color: #339933;">,</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> clearPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>pollTimer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        clearInterval<span style="color: #009900;">&#40;</span>pollTimer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    pollTimer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We call <code>onPurchase</code> when an item is successfully purchased and should be enabled for the user. You should create that function and write your app-specific code.</p>
<h2>Real payments: submitting app to the Firefox Marketplace</h2>
<p>When you are ready to run real payments, remove the <code>simulate</code> field when the server creates the payment request, and <a href="https://marketplace.firefox.com/developers/submit/">submit your app</a> to the Firefox Marketplace. It needs to be submitted so that you can hook up real payments.</p>
<h3>Get paid: configuring bank account details and payment options</h3>
<p>Once your app is submitted, <a href="https://marketplace.firefox.com/developers/submissions">edit your app</a> and click on &#8220;Compatibility &amp; Payments&#8221; on the left side and select &#8220;Paid / In-App&#8221;. Under &#8220;Payment Accounts&#8221;, and add your bank account. One thing that tripped me up was needing the SWIFT code for my bank. In the future, Marketplace will hopefully autofill it, but for now you can look it up on the <a href="http://www.theswiftcodes.com/">theswiftcodes.com</a>.</p>
<p>Under &#8220;Price &amp; Countries&#8221; you can set the price for your app. You may just want &#8220;Free with in-app payments&#8221;, which is what Webfighter is. Additionally, you can selectively choose which region your app is available in. Apps with payments are only available in regions with them enabled.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/prices-and-countries.png"><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/11/prices-and-countries-500.png" /></a></p>
<h3>Use a real key &amp; secret</h3>
<p>Now click the &#8220;In-App Payments&#8221; link in the left sidebar. You&#8217;ll see a screen where you can grab a new key &amp; secret for real payments. Use these in the <code>configure</code> call on the server. Never share your secret.</p>
<p>At this time of writing, carrier billing is only available in specific countries. Check out the <a href="https://marketplace.firefox.com/developers/docs/payments/status">Payments Status page</a> for where it&#8217;s available. When it is not available, the user will be presented with a credit card screen.</p>
<h2>Go make some money with your app!</h2>
<p>We want you to be successful in participating in the app ecosystem and economy. Let us know what your experience is like! We are happy to answer any questions over on the <a href="https://lists.mozilla.org/listinfo/dev-marketplace">marketplace mailing list</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Firefox OS Security: Part 2 – User Experience and Security Updates</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/#comments</comments>
		<pubDate>Thu, 21 Nov 2013 16:59:18 +0000</pubDate>
		<dc:creator><![CDATA[Chris Heilmann]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Videoseries]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23110</guid>
		<description><![CDATA[When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? How can a browser-based app be secure without making the UX suffer by asking the user to react to a lot of &#8220;do [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? How can a browser-based app be secure without making the UX suffer by asking the user to react to a lot of &#8220;do you want to allow this?&#8221;. In this, the second of a two part video series Christian Heilmann (<a href="http://twitter.com/codepo8" >@codepo8</a>), principal evangelist of Mozilla, talks to Michael Coates (<a href="https://twitter.com/_mwc" >@_mwc</a>), chair of <a href="http://twitter.com/OWASP" >@OWASP</a> Board about the user experience when it comes to installing and using apps and the security update model for Firefox OS.</p>
<p><a href="https://www.youtube.com/watch?v=YHx2Ne7vBB8">You can watch the video on YouTube.</a></p>
<p><iframe width="500" height="281" src="//www.youtube-nocookie.com/embed/YHx2Ne7vBB8?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>Firefox OS was built on top of the technologies that power the Web. Following Mozilla’s security practices and knowledge from over 10 years of securing Firefox, Firefox OS is engineered as a multi-tiered system that protects users while delivering the power of the mobile web. The design ensures users are in control of their data and developers have APIs and technologies at their disposal to unlock the power of the Web.</p>
<p>Additional links for more information:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Secure_Architecture" >Firefox OS security architecture</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Permissions_Management_and_Enforcement" >Firefox OS app permission model</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Application_security" >Application security</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Sandboxed_Data" >Data sandboxing</a></li>
<li><a href="https://blog.mozilla.org/futurereleases/2013/07/19/mozillas-heartbeat-quarterly-firefox-os-releases/" >Firefox OS release schedule</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Handling click-to-activate plugins using JavaScript</title>
		<link>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/</link>
		<comments>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/#comments</comments>
		<pubDate>Thu, 21 Nov 2013 16:03:28 +0000</pubDate>
		<dc:creator><![CDATA[Chris Mills]]></dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[click-to-activate]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23072</guid>
		<description><![CDATA[From Firefox 26 onwards — and in the case of insecure Flash/Java in older Firefox versions — most plugins will not be automatically activated. We therefore can no longer plugins starting immediately after they have been inserted into the page. This article covers JavaScript techniques we can employ to handle plugins, making it less likely [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>From Firefox 26 onwards — and in the case of insecure Flash/Java in older Firefox versions — <a href="https://blog.mozilla.org/futurereleases/2013/09/24/plugin-activation-in-firefox/">most plugins will not be automatically activated</a>. We therefore can no longer plugins starting immediately after they have been inserted into the page. This article covers JavaScript techniques we can employ to handle plugins, making it less likely that affected sites will break.</p>
<h2>Using a script to determine if a plugin is installed</h2>
<p>To detect if a plugin is actually installed, we can query <a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.mimeTypes"><code>navigator.mimeTypes</code></a> for the plugin MIME type we intend to use, to differentiate between plugins that are not installed and those that are click-to-activate. For example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> isJavaAvailable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'application/x-java-applet'</span> <span style="color: #000066; font-weight: bold;">in</span> navigator.<span style="color: #660066;">mimeTypes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note: Do not iterate through <code><a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.mimeTypes">navigator.mimeTypes</a></code> or <code><a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.plugins">navigator.plugins</a></code>, as enumeration may well be removed as a privacy measure in a future version of Firefox.</p>
<h2>Using a script callback to determine when a plugin is activated</h2>
<p>The next thing to be careful of is scripting plugins immediately after instances are created on the page, to avoid breakage due to the plugin not being properly loaded. The plugin should make a call into JavaScript after it is created, using <code><a href="https://developer.mozilla.org/docs/Gecko_Plugin_API_Reference/Scripting_plugins">NPRuntime</a></code> scripting:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pluginCreated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">callPluginMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span> <span style="color: #000066;">data</span>=<span style="color: #ff0000;">&quot;somedata.mytype&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;callback&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;pluginCreated()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Note that the &#8220;callback&#8221; parameter (or something equivalent) must be implemented by your plugin. This can be done in Flash using the <code>flash.external.ExternalInterface</code> API, or in Java using the <code>netscape.javascript</code> package.</p>
<h2>Using properties on the plugin to determine when it activated</h2>
<p>When using a plugin that doesn&#8217;t allow us to specify callbacks and we can&#8217;t modify it, an alternative technique is to test for properties that the plugin should have, using code constructs like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myNotification&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Waiting for the plugin to activate!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">myProperty</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myNotification'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">callPluginMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Plugin not activated yet.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setTimeout<span style="color: #009900;">&#40;</span>checkPlugin<span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Making plugins visible on the page</h2>
<p>When a site wants the user to enable a plugin, the primary indicator is that the plugin is visible on the page, for example:</p>
<p><img alt="Screenshot of the silverlight plugin activation on the Netflix website." src="https://mdn.mozillademos.org/files/6305/silverlight-CtA.png" /></p>
<p>If a page creates a plugin that is very small or completely hidden, the only visual indication to the user is the small icon in the Firefox location bar. Even if the plugin element will eventually be hidden, pages should create the plugin element visible on the page, and then resize or hide it only after the user has activated the plugin. This can be done in a similar fashion to the callback technique we showed above:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pluginCreated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// We don't need to see the plugin, so hide it by resizing</span>
  <span style="color: #000066; font-weight: bold;">var</span> plugin <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">callPluginMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Give the plugin an initial size so it is visible --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span> <span style="color: #000066;">data</span>=<span style="color: #ff0000;">&quot;somedata.mytype&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;callback&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;pluginCreated()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>Note:</strong> For more basic information on how plugins operate in Firefox, read <a href="https://support.mozilla.org/en-US/kb/why-do-i-have-click-activate-plugins">Why do I have to click to activate plugins?</a> on support.mozilla.org.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Pond &#8211; building a multi-platform HTML5 game</title>
		<link>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/</link>
		<comments>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/#comments</comments>
		<pubDate>Wed, 20 Nov 2013 23:47:32 +0000</pubDate>
		<dc:creator><![CDATA[Zoli Kahan]]></dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23046</guid>
		<description><![CDATA[Introducing The Pond The Pond is a multi-platform HTML5 game (source code) that explores minimalistic design and resolution independent gameplay. The Pond isn&#8217;t about reaching a high score, or about buying weapon upgrades. It&#8217;s about relaxing and exploring a beautiful world. It is available on all these platforms/in all these stores: In making The Pond [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://thepond.zolmeister.com/"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/the-pond.png" alt="The Pond" /></a></p>
<h2>Introducing The Pond</h2>
<p><a href="http://thepond.zolmeister.com/">The Pond</a> is a multi-platform HTML5 game (<a href="https://github.com/Zolmeister/pond">source code</a>) that explores minimalistic design and resolution independent gameplay. The Pond isn&#8217;t about reaching a high score, or about buying weapon upgrades. It&#8217;s about relaxing and exploring a beautiful world.</p>
<p>It is available on all these platforms/in all these stores:</p>
<ul class="the-pond-stores-list">
<li><a href="https://marketplace.firefox.com/app/the-pond/" title="Firefox Marketplace"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/firefox-marketplace.png" alt="Firefox Marketplace" /></a></li>
<li><a href="https://play.google.com/store/apps/details?id=com.Zolmeister.ThePond" title="Google Play"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/google-play-store.png" alt="Google Play" /></a></li>
<li><a href="https://chrome.google.com/webstore/detail/the-pond/aonjkompolbfgipkpgmcgiakfghibjmm" title="Chrome Web Store"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/chrome-store.png" alt="Chrome Web Store" /></a></li>
<li><a href="http://www.amazon.com/Zolmeister-The-Pond/dp/B00FROOKHI" title="Amazon App store"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/amazon.png" alt="Amazon App store" /></a></li>
<li><a href="http://clay.io/game/thepond" title="Clay.io"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/clay.io_.png" alt="Clay.io" /></a></li>
<li><a href="https://www.pokki.com/app/The-Pond" title="Pokki"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/Pokki.png" alt="Pokki" /></a></li>
</ul>
<p>In making The Pond I came across many performance obstacles which I will explore in detail (especially when optimizing the codebase for mobile).</p>
<h2>Tools</h2>
<p>Before I begin, I would like to mention the two tools that made coding The Pond both efficient and highly enjoyable: Light Table and CocoonJS.</p>
<p><a href="http://www.lighttable.com/">Light Table</a> is an IDE (still in alpha) which provides an integrated development environment for real-time javascript code injection. This means that javascript edited within the editor can be previewed without reloading the page. If we look at the shape of the fish in the game we notice that it is comprised of <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier curves</a>. Instead of trying to find an editor for creating Bézier curves, I simply estimated a basic shape and modified the variables in real-time until I was satisfied with it&#8217;s look and feel.</p>
<p><a href="http://www.ludei.com/tech/cocoonjs">CocoonJS</a> on the otherhand provides a canvas optimized compatibility layer for improved performance on mobile devices. Not only does it optimize, it also provides an interface for exporting our application to many devices (Android, iOS, Amazon (android), Pokki, and Chrome Web Store).</p>
<h2>Physics</h2>
<p>The Pond may seem simple on the outside, but on the inside it&#8217;s full of performance optimizations and responsive features. As we resize the game, it updates and re-optimizes itself to render less objects and spawn less fish, and if that&#8217;s not enough the framerate degrades smoothly to keep physics in check. This is thanks to the use of a <strong>fixed interval physics time step</strong>. <a href="http://gameprogrammingpatterns.com/game-loop.html">Gameprogrammingpatterns.com</a> provides a good explanation for how to do this and why it matters, but honestly the code makes the most sense:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> MS_PER_UPDATE <span style="color: #339933;">=</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Time between physics calculations</span>
<span style="color: #000066; font-weight: bold;">var</span> lag <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// accumulate lag over frames</span>
<span style="color: #000066; font-weight: bold;">var</span> previousTime <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// used for calculating the time delta</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// main game loop</span>
<span style="color: #000066; font-weight: bold;">function</span> draw<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  requestAnimFrame<span style="color: #009900;">&#40;</span>draw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// immidiately queue another frame</span>
  lag <span style="color: #339933;">+=</span> time <span style="color: #339933;">-</span> previousTime<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// add time delta</span>
  previousTime <span style="color: #339933;">=</span> time<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> MAX_CYCLES <span style="color: #339933;">=</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// prevent infinite looping/hanging on slow machines</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// physics calculations</span>
  while<span style="color: #009900;">&#40;</span>lag <span style="color: #339933;">&gt;=</span> MS_PER_UPDATE <span style="color: #339933;">&amp;&amp;</span> MAX_CYCLES<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// user input, movement, and animation calculations</span>
    physics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lag <span style="color: #339933;">-=</span> MS_PER_UPDATE<span style="color: #339933;">;</span>
    MAX_CYCLES<span style="color: #339933;">--;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if we exhausted our cycles, the client must be lagging</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>MAX_CYCLES <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// adaptive quality</span>
    lowerQuality<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if 5 frames behind after update, jump</span>
  <span style="color: #006600; font-style: italic;">// this prevents an infinite input lag from ocurring</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lag<span style="color: #339933;">/</span>MS_PER_UPDATE <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">75</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    lag <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// draw to canvas</span>
  paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What&#8217;s important to notice here is that physics is not calculated based on the time delta, instead it&#8217;s calculated at a fixed 18ms interval. This is important because it means that any client lag will not be reflected in physics calculations, and that slower machines will simply lose framerate.</p>
<h3>Dynamic Quality</h3>
<p>The next optimization we notice is the <code>lowerQuality()</code> function, which adaptively decreases the render quality of the game. The way this works is simply by re-sizing the drawing canvas (it&#8217;s still full screen, it simply gets streched out), which in-turn leads to reduced spawns and collisions.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> resizeWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// quality is a global variable, updated by lowerQuality()</span>
  $canv.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">innerWidth</span> <span style="color: #339933;">*</span> quality<span style="color: #339933;">/</span><span style="color: #CC0000;">10</span>
  $canv.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">innerHeight</span> <span style="color: #339933;">*</span> quality<span style="color: #339933;">/</span><span style="color: #CC0000;">10</span>
  ctx <span style="color: #339933;">=</span> $canv.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span>
  ctx.<span style="color: #660066;">lineJoin</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'round'</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// resize HUD elements, and reduce spawning</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>GAME.<span style="color: #660066;">state</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'playing'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    GAME.<span style="color: #660066;">spawner</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>
    GAME.<span style="color: #660066;">levelBar</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>
    GAME.<span style="color: #660066;">levelBalls</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ASSETS.<span style="color: #660066;">loaded</span><span style="color: #009900;">&#41;</span> drawMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Spawning</h3>
<p>Now, we&#8217;ve been talking about reducing spawning to improve performance so let me explain how that happens. The spawning algorithm works by creating a virtual grid sized based on the window size. As the player travels from one grid zone to another, the adjacent zones are populated with enemies:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/grid-spawner.png" alt="Grid Spawner" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Spawner.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">spawn</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>zone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// spawn 1-3  fish per 500sqpx, maybe larger maybe smaller than player</span>
  <span style="color: #006600; font-style: italic;">// 0.5 chance that it will be bigger/smaller</span>
  <span style="color: #000066; font-weight: bold;">var</span> mult <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">*</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">*</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> mult<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// spawn coordinates random within a zone</span>
    <span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> zone<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span>
    <span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> zone<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span>
    <span style="color: #000066; font-weight: bold;">var</span> size <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">player</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">+</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">player</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">-</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// spawn a new fish</span>
    <span style="color: #000066; font-weight: bold;">var</span> fish <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Fish<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #339933;">-</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fishes</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fish<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> zone
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The last piece of the puzzle is removing enemies as they move far enough away:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// if far enough away from player, remove</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance<span style="color: #009900;">&#40;</span>fish<span style="color: #339933;">,</span> player<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="">Math</span>.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  fish.<span style="color: #660066;">dead</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Collisions</h3>
<p>The next performance optimization lies with the collision code. Colliding irregularly shaped objects can be extremely difficult and resource intensive. One option is to do color based collision (scan for overlapping colors), but that is much too slow. Another option might be to mathematically calculate Bézier curve collisions, however this is not only CPU intensive, it is also quite difficult to code. I finally opted for an approximation approach using circles. Basically I calculate the position of circles within each fish and detect circle collision among the fish. Boolean circle collision is extremely efficient, as it simply requires measuring the distance between objects. This ends up looking like this (debug mode):</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/collisions-debug-model.png" alt="debug mode" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Fish.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">collide</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>fish<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the fish has been killed and is being removed or it is far away</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dying</span> <span style="color: #339933;">||</span> fish.<span style="color: #660066;">dying</span> <span style="color: #339933;">||</span> distance<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> fish<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">5</span> <span style="color: #339933;">+</span> fish.<span style="color: #660066;">size</span><span style="color: #339933;">*</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// there are 6 circles that make up the collision box of each fish</span>
  <span style="color: #000066; font-weight: bold;">var</span> c1<span style="color: #339933;">,</span> c2
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circles</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    c1 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circles</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> j<span style="color: #339933;">=-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> fish.<span style="color: #660066;">circles</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>j <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      c2 <span style="color: #339933;">=</span> fish.<span style="color: #660066;">circles</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// check if they touch</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance<span style="color: #009900;">&#40;</span>c1<span style="color: #339933;">,</span> c2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> c2.<span style="color: #660066;">r</span> <span style="color: #339933;">+</span> c1.<span style="color: #660066;">r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We also avoid unnecessary collision calculations by only checking the fish that are visible (or near-visible):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">abs</span><span style="color: #009900;">&#40;</span>fish2.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> player.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> $canv.<span style="color: #660066;">width</span> <span style="color: #339933;">&amp;&amp;</span> <span style="">Math</span>.<span style="color: #660066;">abs</span><span style="color: #009900;">&#40;</span>fish2.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> player.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// check</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Drawing</h2>
<p>After getting the physics+ out of the way, it&#8217;s time to optimize drawing operations. Many games use sprite maps for animation (<a href="http://www.zolmeister.com/2013/09/senshi-mmo-battle-royale-inspired-html5.html">Senshi</a> for example) which can be highly optimized. Unfortunately our fish are dynamically generated so we must find other ways to optimizing drawing. First lets use Chrome&#8217;s javascript profiler to identify bottlenecks:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/The-Pond-CPU-profile.png" alt="The Pond CPU profile" /></p>
<p>What we see here is that <code>stroke</code> is using a lot of resources. Truth be told, <code>fill</code> used to be there too. This is because both were called heavily when drawing fish. The game looked a bit like this:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/The-Pond-Old-rendering.png" alt="The Pond - Old rendering" /></p>
<p>After removing <code>fill</code> I saw a huge performance increase, and the game looked much better. The reason the <code>drawImage</code> function is up there as well is because I take advantage of <a href="https://hacks.mozilla.org/2013/05/optimizing-your-javascript-game-for-firefox-os/">offscreen canvas</a> rendering. Each fish is drawn on its own offscreen canvas which is then rendered onto the larger visible canvas. This is also what allowed me to easily explode the fish into particles by reading pixel data:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Fish.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">toParticles</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> particles <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// read canvas pixel data</span>
  <span style="color: #000066; font-weight: bold;">var</span> pixels <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">ctx</span>.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> pixels.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">36</span> <span style="color: #339933;">*</span> <span style="">Math</span>.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #339933;">/</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>isMobile <span style="color: #339933;">?</span> <span style="color: #CC0000;">6</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// black pixel - no data</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>r <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>g <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">continue</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Math to calculate position</span>
    <span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> i<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span> <span style="color: #339933;">%</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span> <span style="color: #339933;">/</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> relativePos <span style="color: #339933;">=</span> rot<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dir</span><span style="color: #009900;">&#41;</span>
    x<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> relativePos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>
    y<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> relativePos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> col <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Color<span style="color: #009900;">&#40;</span>r<span style="color: #339933;">,</span> g<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> dir <span style="color: #339933;">=</span> directionTowards<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> y<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
    particles.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">new</span> Particle<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> col<span style="color: #339933;">,</span> target<span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #339933;">/</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> particles
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>The End</h2>
<p>In the end the performance optimizations paid off and made the game feel more polished and playable even on lower-end mobile devices.</p>
<p>If you enjoyed this post, I regularly blog about my development projects over at <a href="http://www.zolmeister.com/">http://zolmeister.com</a>.</p>
<p><a href="http://thepond.zolmeister.com/">The Pond</a> awaits exploring&#8230;</p>
<style>
.the-pond-stores-list {list-style: none;}
.the-pond-stores-list li {display: inline-block; width: 30%; text-align: center;}
.the-pond-stores-list {list-style: none;}
</style>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Launching developer Q&amp;A on Stack Overflow</title>
		<link>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/</link>
		<comments>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/#comments</comments>
		<pubDate>Tue, 19 Nov 2013 21:56:42 +0000</pubDate>
		<dc:creator><![CDATA[Robert Nyman [Editor]]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23091</guid>
		<description><![CDATA[One thing that is very important for us at Mozilla is the need to directly interact with you developers and help you with challenges and issues while developing using open technologies. We are now happy to announce our presence on Stack Overflow! Stack Overflow is one of &#8211; if not the &#8211; most well-known question [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>One thing that is very important for us at Mozilla is the need to directly interact with you developers and help you with challenges and issues while developing using open technologies. We are now happy to announce our presence on Stack Overflow!</p>
<p>Stack Overflow is one of &#8211; if not the &#8211; most well-known question and answer sites among developers, and we believe it&#8217;s very important to go where developers are and choose to have their discussions while developing.</p>
<p>Initially our main focus is on Firefox OS app development and Open Web Apps in general. There are three tags that we are monitoring:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/tagged/firefox">firefox</a></li>
<li><a href="http://stackoverflow.com/questions/tagged/firefox-os">firefox-os</a></li>
<li><a href="http://stackoverflow.com/questions/tagged/html5-apps">html5-apps</a></li>
</ul>
<p>All tags and posts are available at <a href="http://stackoverflow.com/r/mozilla">our landing page on Stack Overflow: http://stackoverflow.com/r/mozilla</a></p>
<h2>Take part!</h2>
<p>We&#8217;d love for you to participate with both questions and answers, so we together can collaborate and make the Open Web and open technologies the first choice for developers!</p>
<p><a href="http://stackoverflow.com/r/mozilla"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/stack-overflow.png" alt="" /></a></p>
<p>A little extra motivation might be that if you answer a lot of questions, you&#8217;ll show up in the Top Users page for that tag, e.g. <a href="http://stackoverflow.com/tags/firefox-os/topusers">http://stackoverflow.com/tags/firefox-os/topusers</a></p>
<p>Resources that might be good for you about HTML5, Apps, Developer Tools and more are:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5">HTML5 on the Mozilla Developer Network (MDN)</a></li>
<li><a href="https://developer.mozilla.org/en-US/Apps">App Center on MDN</a></li>
<li><a href="https://developer.mozilla.org/en-US/Firefox_OS">Firefox OS on MDN</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/tools">Firefox Developer Tools on MDN</a></li>
</ul>
<p>See you on Stack Overflow!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Firefox OS Security: Part 1 &#8211; The Web Security Model</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/#comments</comments>
		<pubDate>Thu, 14 Nov 2013 16:06:08 +0000</pubDate>
		<dc:creator><![CDATA[Chris Heilmann]]></dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[Videoseries]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23080</guid>
		<description><![CDATA[When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? In this two part video series Christian Heilmann (@codepo8), principal evangelist of Mozilla, talks to Michael Coates (@_mwc), chair of @OWASP Board about [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? In this two part video series Christian Heilmann (<a href="http://twitter.com/codepo8">@codepo8</a>), principal evangelist of Mozilla, talks to Michael Coates (<a href="https://twitter.com/_mwc">@_mwc</a>), chair of <a href="http://twitter.com/OWASP">@OWASP</a> Board about all things security in Firefox OS.</p>
<p>Firefox OS was built on top of the technologies that power the Web. Following Mozilla&#8217;s security practices and knowledge from over 10 years of securing Firefox, Firefox OS is engineered as a multi-tiered system that protects users while delivering the power of the mobile web. The design ensures users are in control of their data and developers have APIs and technologies at their disposal to unlock the power of the Web.</p>
<p>Watch the following video where we talk more about the security design and controls present in Firefox OS. In this, the first of two videos on Firefox OS security, we&#8217;ll cover items such as the multi-tiered architecture, the permission model, run time decision making, protection of users data and the update model. <a href="https://www.youtube.com/watch?v=9T1PH85SC0U">You can watch the video on YouTube</a>.</p>
<p><iframe width="500" height="281" src="//www.youtube-nocookie.com/embed/9T1PH85SC0U?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>Additional links for more information:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Secure_Architecture">Firefox OS security architecture</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Permissions_Management_and_Enforcement">Firefox OS app permission model</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Application_security">Application security</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Sandboxed_Data">Data sandboxing</a></li>
<li><a href="https://blog.mozilla.org/futurereleases/2013/07/19/mozillas-heartbeat-quarterly-firefox-os-releases/">Firefox OS release schedule</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Introducing the Whiteboard Drum &#8211; WebRTC and Web Audio API magic</title>
		<link>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/</link>
		<comments>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/#comments</comments>
		<pubDate>Thu, 14 Nov 2013 07:18:08 +0000</pubDate>
		<dc:creator><![CDATA[Tatsuya Shinyagaito]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[WebRTC]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22982</guid>
		<description><![CDATA[Browser functionality has expanded rapidly, way beyond merely &#8220;browsing&#8221; a document. Recently, Web browsers finally gained audio processing abilities with the Web Audio API. It is powerful to the point of building serious music applications. Not only that, but it is also very interesting when combined with other APIs. One of these APIs is getUserMedia(), [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Browser functionality has expanded rapidly, way beyond merely &#8220;browsing&#8221; a document. Recently, Web browsers finally gained audio processing abilities with the <a href="https://developer.mozilla.org/en-US/docs/Web_Audio_API">Web Audio API</a>. It is powerful to the point of building serious music applications.</p>
<p>Not only that, but it is also very interesting when combined with other APIs. One of these APIs is <code>getUserMedia()</code>, which allows us to capture audio and/or video from the local PC&#8217;s microphone / camera devices. <a href="http://www.g200kg.com/whiteboarddrum/">Whiteboard Drum</a> (<a href="https://github.com/g200kg/WhiteboardDrum">code on GitHub</a>) is a music application, and a great example of what can be achieved using Web Audio API and <code>getUserMedia()</code>.</p>
<p>I demonstrated Whiteboard Drum at the Web Music Hackathon Tokyo in October. It was a very exciting event on the subject of Web Audio API and Web MIDI API. Many instruments can collaborate with the browser, and it can also create new interfaces to the real world.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/20131020hack1.jpeg" alt="" /></p>
<p>I believe this suggests further possibilities of Web-based music applications, especially using Web Audio API in conjunction with other APIs. Let&#8217;s explain how the key features of Whiteboard Drum work, showing relevant code fragments as we go.</p>
<h2>Overview</h2>
<p>First of all, let me show you a picture from the Hackathon:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/hackathon.jpg" alt="" /></p>
<p>And a easy movie demo:</p>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/aFLKXIravec?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>As you can see, Whiteboard Drum plays a rhythm according to the matrix pattern on the whiteboard. The whiteboard has no magic; it just needs to be pointed at by a WebCam. Though I used magnets in the demo, you can draw the markers using pen if you wish. Each row represents the corresponding instruments of Cymbal, Hi-Hat, Snare-Drum and Bass-Drum, and each column represents timing steps. In this implementation, the sequence has 8 steps. The color blue will activate the grid normally, and the red will activate with accent.</p>
<p>The processing flow is:</p>
<ol>
<li>The whiteboard image is captured by the WebCam</li>
<li>The matrix pattern is analysed</li>
<li>This pattern is fed to the drum sound generators to create the corresponding sound patterns</li>
</ol>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/desc1.png" alt="" /></p>
<p>Although it uses nascent browser technologies, each process itself is not so complicated. Some key points are described below.</p>
<h2>Image capture by getUserMedia()</h2>
<p><code>getUserMedia()</code> is a function for capturing video/audio from webcam/microphone devices. It is a part of <a href="http://www.webrtc.org/">WebRTC</a> and a fairly new feature in web browsers. Note that the user&#8217;s permission is required to get the image from the WebCam. If we were just displaying the WebCam image on the screen, it would be trivially easy. However, we want to access the image&#8217;s raw pixel data in JavaScript for more processing, so we need to use <code>canvas</code> and the <code>createImageData()</code> function.</p>
<p>Because pixel-by-pixel processing is needed later in this application, the captured image&#8217;s resolution is reduced to 400 x 200px; that means one rhythm grid is 50 x 50 px in the rhythm pattern matrix.</p>
<p>Note: Though most recent laptops/notebooks have embedded WebCams, you will get the best results on Whiteboard Drum from an external camera, because the camera needs to be precisely aimed at the picture on the whiteboard. Also, the selection of input from multiple available devices/cameras is not standardized currently, and cannot be controlled in JavaScript. In Firefox, it is selectable in the permission dialog when connecting, and it can be set up from &#8220;contents setup&#8221; option of the setup screen in Google Chrome.</p>
<h3>Get the WebCam video</h3>
<p>We don&#8217;t want to show these parts of the processing on the screen, so first we hide the <code>video</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;video</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;video&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Now to grab the video:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">video <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;video&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #339933;">=</span>navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #339933;">||</span>navigator.<span style="color: #660066;">webkitGetUserMedia</span><span style="color: #339933;">||</span>navigator.<span style="color: #660066;">mozGetUserMedia</span><span style="color: #339933;">;</span>
navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;video&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        video.<span style="color: #660066;">src</span><span style="color: #339933;">=</span> window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        video.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Camera Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Capture it and get pixel values</h3>
<p>We also hide the <code>canvas</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;canvas</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;capture&quot;</span> <span style="color: #000066;">width</span>=400 <span style="color: #000066;">height</span>=200 <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/canvas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Then capture our video data on the <code>canvas</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> Capture<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ctxcapture.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    imgdatcapture<span style="color: #339933;">=</span>ctxcapture.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The video from the WebCam will be drawn onto the <code>canvas</code> at periodic intervals.</p>
<h2>Image analyzing</h2>
<p>Next, we need to get the 400 x 200 pixel values with <code>getImageData()</code>. The analyzing phase analyses the 400 x 200 image data in an 8 x 4 matrix rhythm pattern, where a single matrix grid is 50 x 50 px. All necessary input data is stored in the <code>imgdatcapture.data</code> array in RGBA format, 4 elements per pixel.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> pixarray <span style="color: #339933;">=</span> imgdatcapture.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> step<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> px <span style="color: #339933;">=</span> x <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>invert<span style="color: #009900;">&#41;</span>
        step<span style="color: #339933;">=</span><span style="color: #CC0000;">7</span><span style="color: #339933;">-</span>x<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        step<span style="color: #339933;">=</span>x<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> py <span style="color: #339933;">=</span> y <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> lum <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> red <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> dx <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> dx <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>dx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> dy <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> dy <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>dy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>py <span style="color: #339933;">+</span> dy<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">400</span> <span style="color: #339933;">+</span> px <span style="color: #339933;">+</span> dx<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
                lum <span style="color: #339933;">+=</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">3</span> <span style="color: #339933;">+</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">6</span> <span style="color: #339933;">+</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                red <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lum <span style="color: #339933;">&lt;</span> lumthresh<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>red <span style="color: #339933;">&gt;</span> redthresh<span style="color: #009900;">&#41;</span>
                rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span>
                rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span>
            rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is honest pixel-by-pixel analysis of grid-by-grid loops. In this implementation, the analysis is done for the luminance and redness. If the grid is &#8220;dark&#8221;, the grid is activated; if it is red, it should be accented.</p>
<p>The luminance calculation uses a simplified matrix — R * 3 + G * 6 + B — that will get ten times the value &#8211; meaning &#8211; which means getting the value of range 0 to 2550 for each pixel. And the redness R &#8211; B is a experimental value because all that is required is a decision of Red or Blue. The result is stored in the <code>rhythmpat</code> array, with a value of 0 for nothing, 1 for blue or 2 for red.</p>
<h2>Sound generation through the Web Audio API</h2>
<p>Because the <a href="https://developer.mozilla.org/en-US/docs/Web_Audio_API">Web Audio API</a> is a very cutting edge technology, it is not yet supported by every web browser. Currently, Google Chrome/Safari/Webkit-based Opera and Firefox (25 or later) support this API. <strong>Note:</strong> Firefox 25 is the latest version released at the end of October.</p>
<p>For other web browsers, I have developed a polyfill that falls back to Flash: <a href="https://github.com/g200kg/WAAPISim">WAAPISim, available on GitHub</a>. It provides almost all functions of the Web Audio API to unsupported browsers, for example Internet Explorer.</p>
<p>Web Audio API is a large scale specification, but in our case, the sound generation part requires just a very simple use of the Web Audio API: load one sound for each instrument and trigger them at the right times. First we create an audio context, taking care of vendor prefixes in the process. Prefixes currently used are webkit or no prefix.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">audioctx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">AudioContext</span><span style="color: #339933;">||</span>window.<span style="color: #660066;">webkitAudioContext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we load sounds to buffers via XMLHttpRequest. In this case, different sounds for each instrument (bd.wav / sd.wav / hh.wav / cy.wav) are loaded into the <code>buffers</code> array:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> buffers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> loadidx <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">&quot;samples/bd.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/sd.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/hh.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/cy.wav&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> LoadBuffers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    req.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> files<span style="color: #009900;">&#91;</span>loadidx<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">responseType</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;arraybuffer&quot;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">response</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            audioctx.<span style="color: #660066;">decodeAudioData</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">response</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                buffers<span style="color: #009900;">&#91;</span>loadidx<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>b<span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>loadidx <span style="color: #339933;">&lt;</span> files.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
                    LoadBuffers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The Web Audio API generates sounds by routing graphs of nodes. Whiteboard Drum uses a simple graph that is accessed via <code>AudioBufferSourceNode</code> and <code>GainNode</code>. <code>AudioBufferSourceNode</code> play back the AudioBuffer and route to destination(output) directly (for normal *blue* sound), or route to destination via the <code>GainNode</code> (for accented *red* sound). Because the <code>AudioBufferSourceNode</code> can be used just once, it will be newly created for each trigger.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/nodes.png" alt="" /></p>
<p>Preparing the <code>GainNode</code> as the output point for accented sounds is done like this.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">gain<span style="color: #339933;">=</span>audioctx.<span style="color: #660066;">createGain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gain.<span style="color: #660066;">gain</span>.<span style="color: #660066;">value</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
    gain.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>audioctx.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And the trigger function looks like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> Trigger<span style="color: #009900;">&#40;</span>instrument<span style="color: #339933;">,</span>accent<span style="color: #339933;">,</span>when<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> src<span style="color: #339933;">=</span>audioctx.<span style="color: #660066;">createBufferSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src.<span style="color: #660066;">buffer</span><span style="color: #339933;">=</span>buffers<span style="color: #009900;">&#91;</span>instrument<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>accent<span style="color: #009900;">&#41;</span>
        src.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>gain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        src.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>audioctx.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span>when<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>All that is left to discuss is the accuracy of the playback timing, according to the rhythm pattern. Though it would be simple to keep creating the triggers with a <code>setInterval()</code> timer, it is not recommended. The timing can be easily messed up by any CPU load.</p>
<p>To get accurate timing, using the time management system embedded in the Web Audio API is recommended. It calculates the <code>when</code> argument of the <code>Trigger()</code> function above.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// console.log(nexttick-audioctx.currentTime);</span>
while<span style="color: #009900;">&#40;</span>nexttick <span style="color: #339933;">-</span> audioctx.<span style="color: #660066;">currentTime</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0.3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
        Trigger<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> p<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> nexttick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>step <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span>
        step <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    nexttick <span style="color: #339933;">+=</span> deltatick<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In Whiteboard Drum, this code controls the core of the functionality. <code>nexttick</code> contains the accurate time (in seconds) of the next step, while <code>audioctx.currentTime</code> is the accurate current time (again, in seconds). Thus, this routine is getting triggered every 300ms &#8211; meaning look ahead to 300ms in the future (triggered in advance while nextticktime &#8211; currenttime &lt; 0.3). The commented <code>console.log</code> will print the timing margin. While this routine is called periodically, the timing is collapsed if this value is negative.</p>
<p>For more detail, here is a helpful document: <a href="http://www.html5rocks.com/en/tutorials/audio/scheduling/">A Tale of Two Clocks &#8211; Scheduling Web Audio with Precision</a></p>
<h2>About the UI</h2>
<p>Especially in music production software like DAW or VST plugins, the UI is important. Web applications do not have to emulate this exactly, but something similar would be a good idea. Fortunately, the very handy <a href="https://github.com/g200kg/webaudio-controls">WebComponent library webaudio-controls</a> is available, allowing us to define knobs or sliders with just a single HTML tag.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/controls.png" alt="" /></p>
<p><strong>NOTE</strong>: webaudio-controls uses Polymer.js, which sometimes has stability issues, causing unexpected behavior once in a while, especially when combining it with complex APIs.</p>
<h2>Future work</h2>
<p>This is already an interesting application, but it can be improved further. Obviously the camera position adjustment is an issue. Analysis can be more smarter if it has an auto adjustment of the position (using some kind of marker?), and adaptive color detection. Sound generation could also be improved, with more instruments, more steps and more sound effects.</p>
<p>How about a challenge?</p>
<p>Whiteboard Drum is available at <a href="http://www.g200kg.com/whiteboarddrum/">http://www.g200kg.com/whiteboarddrum/</a>, and the <a href="https://github.com/g200kg/WhiteboardDrum">code is on GitHub</a>.</p>
<p>Have a play with it and see what rhythms you can create!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Live editing WebGL shaders with Firefox Developer Tools</title>
		<link>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/</link>
		<comments>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/#comments</comments>
		<pubDate>Tue, 12 Nov 2013 17:19:17 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Griffiths]]></dc:creator>
				<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23013</guid>
		<description><![CDATA[If you’ve seen Epic Games’ HTML5 port of ‘Epic Citadel’, you have no doubt been impressed by the amazing performance and level of detail. A lot of the code that creates the cool visual effects you see on screen are written as shaders linked together in programs &#8211; these are specialized programs that are evaluated [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>If you’ve seen Epic Games’ HTML5 port of <a href="http://www.unrealengine.com/html5/">‘Epic Citadel’</a>, you have no doubt been impressed by the amazing performance and level of detail. A lot of the code that creates the cool visual effects you see on screen are written as <em>shaders</em> linked together in <em>programs</em> &#8211; these are specialized programs that are evaluated directly on the GPU to provide high performance real-time visual effects.</p>
<p>Writing Vertex and Fragment shaders are an essential part of developing 3D on the web even if you are using a library, in fact the <a href="https://blog.mozilla.org/futurereleases/2013/05/02/epic-citadel-demo-shows-the-power-of-the-web-as-a-platform-for-gaming/">Epic Citadel demo</a> includes over 200 shader programs. This is because most rendering is customised and optimised to fit a game’s needs. Shader development is currently awkward for a few reasons:</p>
<ul>
<li>Seeing your changes requires a refresh</li>
<li>Some shaders are applied under very specific conditions</li>
</ul>
<p>Here is a screencast that shows a how to manipulate shader code using a relatively simple WebGL demo:</p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/hnoKqFuJhu0" frameborder="0" allowfullscreen></iframe></p>
<p>Starting in Firefox 27 we&#8217;ve introduced a new tool called the &#8216;Shader Editor&#8217; that makes working with shader programs much simpler: the editor lists all shader programs running in the WebGL context, and you can live-edit shaders and see immediate results without interrupting any animations or state. Additionally editing shaders should <strong>not</strong> impact WebGL performance.</p>
<h2>Enabling the Shader Editor</h2>
<p>The Shader Editor is not shown by default, because not all the web pages out there contain WebGL, but you can easily enable it:</p>
<ol>
<li>Open the Toolbox by pressing either F12 or Ctrl/Cmd + Shift + I.</li>
<li>Click on the &#8216;gear&#8217; icon near the top edge of the Toolbox to open the &#8216;Toolbox Options&#8217;.</li>
<li>On the left-hand side under &#8216;Default Firefox Developer Tools&#8217; make sure &#8216;Shader Editor&#8217; is checked. You should immediately see a new &#8216;Shader Editor&#8217; Tool tab.</li>
</ol>
<h2>Using the Shader Editor</h2>
<p>To see the Shader Editor in action, just go to a WebGL demo <a href="http://learningwebgl.com/lessons/lesson04/index.html">such as this one</a> and open the toolbox. When you click on the shader editor tab, you&#8217;ll see a reload button you will need to click in order to get the editor attached to the WebGL context. Once you&#8217;ve done this you&#8217;ll see the Shader Editor UI:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-loaded.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-loaded-500.png" alt="The WebGL Shader Editor" /></a></p>
<ul>
<li>On the left you have a list of programs, a vertex and fragment shader corresponds to each program and their source is displayed and syntax highlighted in the editors on the right.</li>
<li>The shader type is displayed underneath each editor.</li>
<li>Hovering a program highlights the geometry drawn by its corresponding shaders in red – this is useful for finding the right program to work on.</li>
<li>Clicking on the eyeball right next to each program hides the rendered geometry (useful in the likely case an author wants to focus solely on some geometry but not other, or to hide overlapping geometry).</li>
<li>The tool is responsive when docked to the side.</li>
</ul>
<h2>Editing Shader Programs</h2>
<p>The first thing you&#8217;ll notice about Shader program code is that it is <strong>not</strong> JavaScript. For more information on how Shader programs work, I highly recommend you start with the WebGL demo on the <a href="http://www.khronos.org/webgl/wiki/Tutorial#Creating_the_Shaders">Khronos wiki</a> and/or Paul Lewis&#8217; excellent <a href="http://www.html5rocks.com/en/tutorials/webgl/shaders/">HTML5 Rocks post</a>. There also some great long standing tutorials on the <a href="http://learningwebgl.com/blog/?page_id=1217">Learning WebGL</a> blog. The Shader Editor gives you direct access to the programs so you can play around with how they work:</p>
<ul>
<li>Editing code in any of the editors will compile the source and apply it as soon as the user stops typing;</li>
<li>If an error was made in the code, the rendering won’t be affected, but an error will be displayed in the editor, highlighting the faulty line of code; hovering the icon gutter will display a tooltip describing the error.</li>
</ul>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-error-info.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-error-info-500.png" alt="Errors in shaders" /></a></p>
<p>Learn more about the <a href="https://developer.mozilla.org/en-US/docs/Tools/Shader_Editor/">Shader Editor on the Mozilla Developer Network</a>.</p>
<p>Here is a second screencast showing how you could directly edit the shader programs in the Epic Citadel demo:</p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/Mwi_1bNft1o" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Make your Firefox OS app feel alive with video and audio</title>
		<link>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/</link>
		<comments>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/#comments</comments>
		<pubDate>Thu, 07 Nov 2013 14:44:21 +0000</pubDate>
		<dc:creator><![CDATA[Frédéric Harper]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22949</guid>
		<description><![CDATA[Firefox OS applications aren&#8217;t just about text: there is no better way to make your app feel alive than adding some videos or audio to it. Let&#8217;s explore different ways we can use as developers to enhance our mobile masterpiece. Audio and video HTML tags Since we are talking about HTML, it makes total sense [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Firefox OS applications aren&#8217;t just about text: there is no better way to make your app feel alive than adding some videos or audio to it. Let&#8217;s explore different ways we can use as developers to enhance our mobile masterpiece.</p>
<h2>Audio and video HTML tags</h2>
<p>Since we are talking about HTML, it makes total sense to think about using the <code>&lt;audio&gt;</code>, and <code>&lt;video&gt;</code> tag to play those media in your Firefox OS app. If you want to add a video in your application, just use this code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;video</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://v2v.cc/~j/theora_testsuite/320x240.ogg&quot;</span> controls<span style="color: #000000; font-weight: bold;">&gt;</span></span>
  Your browser does not support the video element.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In this code example, the user will see a video player with controls, and will have the opportunity to start the video. If your application is running in a browser not supporting the <code>video</code> tag, the user will see the text between the tag. It&#8217;s still a good practice to do so, even if your primary target is a Firefox OS app, because since it uses HTML5, someone may access it from another browser if it&#8217;s a hosted app. Note that you can use other <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video" title="Mozilla Developer Network article on HTML video tag">attributes</a> for this element.</p>
<p>As for the audio tag, it&#8217;s basically the same.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;audio</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;demo&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/music/audio.mp3&quot;</span> autoplay loop<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/audio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In this example, the audio will start automatically, and will play the audio file, in a loop, from the relative path: it&#8217;s perfect for background music if you are building a game. Note that you can add other <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio" title="Mozilla Developer Network article on HTML audio tag">attributes</a> to this element too.</p>
<p>Of course, using those elements without JavaScript give you basic features, but no worries, you can programmatically control them with code. Once you have your HTML element, like the <code>audio</code> example you just saw, you can use JavaScript to play, pause, change the volume, and more.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Play the Audio</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Pause the Audio</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">volume</span><span style="color: #339933;">+=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Increase Volume</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">volume</span><span style="color: #339933;">-=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Decrease Volume</span></pre></td></tr></table></div>

<p>You can read more on what you can do with those two elements in the <a title="Documentation on the HTML video, and audio tag from the Mozilla Developer Network" href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video">Mozilla Developer Network documentation</a>. You also want to give a closer look to the <a href="https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats" title="Media formats supported by the HTML audio and video elements on Mozilla Developer Network">supported format list</a>.</p>
<h2>Use audio while the screen is locked</h2>
<p>Maybe you are building a podcast app, or at least you need to be able to play audio while the screen is locked? There is a way to do it by using the <code>audio</code> tag. You simply need to add the <code>mozaudiochannel</code> attribute with the value of <code>content</code> to your actual tag.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>audio mozaudiochannel<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;content&quot;</span> preload<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;none&quot;</span> 
  src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg&quot;</span> 
  autoplay<span style="color: #339933;">&gt;&lt;/</span>audio<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Actually, it&#8217;s not quite true as this code won&#8217;t work as is. You also need to add a permission to the manifest file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;">&quot;permissions&quot;: {
  &quot;audio-channel-content&quot;:{
    &quot;description&quot;:&quot;Use the audio channel for the music player&quot;
  }
}</pre></td></tr></table></div>

<p>Having the manifest line above will authorize your application to use the audio channel to play music, even when the screen is locked. Having said that, you probably realize that this code <strong>is specific to Firefox OS for now</strong>. I intentionally put the end of the last sentence in bold as it&#8217;s one thing you need to understand about Firefox OS: we had to create some APIs, features or elements to give the power HTML deserve for developers, but we are working with the W3C to make those standards. In the case that the standards won&#8217;t be the same as what we created, we&#8217;ll change it to reflect it.</p>
<h2>Firefox OS Web activities</h2>
<p>Finally, something very handy for Firefox OS developers: the <a href="https://developer.mozilla.org/en-US/docs/WebAPI/Web_Activities" title="Web Activities article on Mozilla Developer Network">Web Activities</a>. They define a way for applications to delegate an activity to another (usually user-chosen) application. They aren&#8217;t standardized, at the time of writing. In the case that will be interesting to us, we&#8217;ll use the Web Activity call <code>open</code>, to open music or video files. Note that for video, you can also use the <code>view</code> activity that basically does the same. Let&#8217;s say I want to open a remote video when someone clicks on a button with the id <code>open-video</code>: I&#8217;ll use the following code in my JavaScript to make it happen.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> openVideo <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#open-video&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>openVideo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    openVideo.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> openingVideo <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                type<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                  <span style="color: #3366CC;">&quot;video/webm&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/mp4&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/3gpp&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/youtube&quot;</span>
                <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://v2v.cc/~j/theora_testsuite/320x240.ogg&quot;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In that situation, the video player of Firefox OS will open, and play the video: it&#8217;s that easy!</p>
<h2>In the end&#8230;</h2>
<p>You may or may not need to use those tricks in your app, but adding videos or audio can enhance the quality of your application, and make it feel alive. At the end, you have to give a strong experience to your users, and it&#8217;s what will make the difference between a good and a great app!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://v2v.cc/~j/theora_testsuite/320x240.ogg" length="285310" type="audio/ogg" />
<enclosure url="http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg" length="191675" type="audio/ogg" />
		</item>
	</channel>
</rss>