-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
356 lines (323 loc) · 14.6 KB
/
index.html
File metadata and controls
356 lines (323 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title data-l10n-id="slides-title">Developing addons for Firefox OS</title>
<meta name="locales" content="en-US, es"/>
<meta name="default_locale" content="en-US">
<meta data-l10n-id="slides-description" name="description" content=""/>
<meta name="author" content="Adrian Crespo"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link rel="prefetch" type="application/l10n" href="data/locales.ini" />
<script type="text/javascript" src="js/l10n.js" defer></script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1 id="firstSlide" data-l10n-id="slides-title">Firefox OS - Spark</h1>
<h3 id="firstSlide" data-l10n-id="slides-subtitle">Developing addons for Firefox OS</h3>
<p data-l10n-id="by-author">
<small>Created by <a target="_blank" href="http://mozilla.com/">Mozilla</a></small>
</p>
</section>
<section>
<h2 data-l10n-id="title-spark">What is Spark?</h2>
<p data-l10n-id="p1-spark">
Spark is a set of tools, customizations, and features built on top of the next generation of the Firefox OS platform
</p>
<p data-l10n-id="p2-spark">
Spark is intended to empower users to customize their experience.
</p>
<p data-l10n-id="p3-spark" class="fragment">
That is why addons comes into play.
</p>
</section>
<!-- Example of nested vertical slides -->
<section>
<section>
<h2 data-l10n-id="title-addons">Addons</h2>
<p data-l10n-id="p1-addons">Add-ons are a well-known concept in the world of web browsers.</p>
<p data-l10n-id="p2-addons">This concept has been added to Firefox OS too!</p>
</section>
<section>
<h2 data-l10n-id="title-addons-2">But what are addons?</h2>
<p data-l10n-id="p1-addons-2">Add-ons are app packages composed of JavaScript, CSS, and other assets.</p>
<p data-l10n-id="p2-addons-2">A single Firefox OS add-on can extend just one app, several, or all of them.</p>
<p data-l10n-id="p3-addons-2" class="fragment">Lets build our first addon!</p>
</section>
</section>
<section>
<section>
<h2 data-l10n-id="title-imgur">Our first addon</h2>
<p data-l10n-id="p2-imgur">This addon will take any screnshot taken with out device and will upload it to IMGUR.</p>
<p data-l10n-id="p1-imgur">It will be attached to the system app: <b>system.gaiamobile.org</b></p>
<p data-l10n-id="p3-imgur" class="fragment">That means that it will inherit the permissions of the target app: since system.gaiamobile.org is <b>certified</b>, the addon will inherit the same permissions.</p>
</section>
<section>
<h2 data-l10n-id="title-imgur-2">IMGUR Addon</h2>
<p data-l10n-id="p1-imgur">It must have a manifest file.</p>
<p data-l10n-id="p1-imgur">It will be a Chrome manifest, quite different from the manifests used for Firefox OS Webapps.</p>
<p class="fragment">Manifest File Format: <a target="_blank" href="https://developer.chrome.com/extensions/manifest">Field summary</a>.</p>
</section>
<section>
<h2 data-l10n-id="title-imgur-2">IMGUR Addon</h2>
<pre><code data-trim contenteditable>
{
"manifest_version": 2,
"version": "2.0",
"name": "IMGUR Screenshots",
"description": "Send a screenshot to IMGUR.",
"author": "Adrian Crespo, Giovanny Gongora",
"role": "addon",
"type": "certified",
"content_scripts": [{
"matches": ["app://system.gaiamobile.org/index.html"],
"css": [],
"js": ["imgur.js"]
}],
"icons": {
"512": "/style/icons/512.png",
"256": "/style/icons/256.png",
"128": "/style/icons/128.png",
"90": "/style/icons/90.png",
"64": "/style/icons/64.png",
"48": "/style/icons/48.png",
"32": "/style/icons/32.png",
"16": "/style/icons/16.png"
}
}
</code></pre>
<small>
<p data-l10n-id="p1-imgur-2">The "matches" field is what is used to attach the addon to any app.</p>
<p data-l10n-id="p2-imgur-2">It uses origin + entry point (index.html)</p>
<p data-l10n-id="p3-imgur-2">The js and css fields are the assets attached to the target.</p>
</small>
</section>
<section>
<p>IMGUR Addon</p>
<img src="img/zipFile.png">
<p data-l10n-id="p1-imgur-3">This manifest.json will be inside the package.</p>
</section>
</section>
<section>
<section>
<h2 data-l10n-id="title-update">But we want to publish it!</h2>
<p data-l10n-id="p1-update">If we want any user to be able to install it we need another manifest.</p>
<p data-l10n-id="p2-update">It will be "update.webapp" and it will be hosted in our Web Hosting. That will let the Hackerplace to find it.</p>
<small data-l10n-id="p3-update" class="fragment">Pro tip: It has its own Content-Type, so check <a href="https://developer.mozilla.org/en-US/Apps/Build/Manifest#Serving_manifests">these for steps your hosting.</a></small>
</section>
<section>
<h2 data-l10n-id="title-update-2">So it will look like...</h2>
<img src="img/ftp.png">
</section>
</section>
<section>
<section>
<h2 data-l10n-id="title-code">Injecting the code</h2>
<p data-l10n-id="p1-code">In the content_script field, we specified our script:</p>
<p data-l10n-id="p2-code">"js": ["imgur.js"]</p>
<p data-l10n-id="p3-code">We are attaching that script to a Web App that whose url matches a regular expresion.</p>
<p data-l10n-id="p3-code">We are matching a local app, app://system.gaiamobile.org/index.html, but it could be even a Website, like ["http://www.mozilla.com/*"].</p>
<p><small data-l10n-id="p4-code" class="fragment">Pro tip: <a href="https://developer.chrome.com/extensions/content_scripts#match-patterns-globs">match patterns.</a></small></p>
</section>
<section>
<h2 data-l10n-id="title-code-2">The window object</h2>
<p data-l10n-id="p1-code-2">Add-ons only share a <b>proxied version</b> of the content window.</p>
<p data-l10n-id="p2-code-2">As a result, anything that is written to the window object from an add-on is unavailable to the app code.</p>
<p data-l10n-id="p3-code-2">However, anything on the window object that is set by app code is available to add-ons. Similarly, the DOM is accessible as usual.</p>
</section>
<section>
<h2 data-l10n-id="title-code-2">Considerations about the window object</h2>
<p data-l10n-id="p1-code-2">When an app is already running and an add-on that targets it is enabled. in such a case, a window.onload handler won't work.</p>
<p data-l10n-id="p2-code-2">As a result, anything that is written to the window object from an add-on is unavailable to the app code.</p>
<p data-l10n-id="p3-code-2">However, anything on the window object that is set by app code is available to add-ons. Similarly, the DOM is accessible as usual.</p>
</section>
<section>
<h2 data-l10n-id="title-code-3">Preventing multiple injections</h2>
<p data-l10n-id="p1-code-3">To prevent an add-on from being injected into a single app instance multiple times, you should check whether your add-on is already present.</p>
<p data-l10n-id="p2-code-3">I recommend to set a private propery or variable to check whenever it has been loaded.</p>
<p data-l10n-id="p3-code-3">I usually do it in a constructor.</p>
</section>
<section>
<h2 data-l10n-id="title-code-4">Example</h2>
<pre><code data-trim contenteditable>
(function () {
function uploader() {
console.log("Object created!");
this._started = false;
uploader.prototype.start();
}
uploader.prototype = {
/**
* Start to handle screenshot events.
* @memberof Screenshot.prototype
*/
start: function () {
console.log("IMGUR Running");
if (this._started) {
throw 'Instance should not be start()\'ed twice.';
}
this._started = true;
window.addEventListener('mozChromeEvent', this);
},
/**
* Stop handling screenshot events.
* @memberof Screenshot.prototype
*/
stop: function () {
if (!this._started) {
throw 'Instance was never start()\'ed but stop() is called.';
}
this._started = false;
window.removeEventListener('mozChromeEvent', this.handleEvent);
},
/**
* Handle screenshot events.
* @param {DOMEvent} evt DOM Event to handle.
* @memberof Screenshot.prototype
*/
handleEvent: function (evt) {
switch (evt.type) {
case 'mozChromeEvent':
if (evt.detail.type === 'take-screenshot-success') {
console.log("There is an screenshot available.");
if(navigator.onLine){
this.handleTakeScreenshotSuccess(evt.detail.file);
}
} else if (evt.detail.type === 'take-screenshot-error') {
this.notify('screenshotFailed', evt.detail.error);
}
break;
default:
console.debug('Unhandled event: ' + evt.type);
break;
}
},
/**
* Handle the take-screenshot-success mozChromeEvent.
* @param {Blob} file Blob object received from the event.
* @memberof Screenshot.prototype
*/
handleTakeScreenshotSuccess: function (file) {
try {
var self = this;
var fd = new FormData();
fd.append("image", file);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://api.imgur.com/3/image');
xhr.setRequestHeader('Authorization', 'Client-ID 440d44a2a741339');
xhr.onload = function() {
var data = JSON.parse(xhr.responseText).data;
var imgurURL = data.link;
console.log(imgurURL);
self.notify('Screenshot uploaded: ', imgurURL, null, true);
//const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
//gClipboardHelper.copyString(imgurURL);
};
xhr.send(fd);
} catch (e) {
console.log('exception in screenshot handler', e);
this.notify('screenshotFailed', e.toString());
}
},
/**
* Display a screenshot success or failure notification.
* Localize the first argument, and localize the third if the second is null
* @param {String} titleid l10n ID of the string to show.
* @param {String} body Label to show as body, or null.
* @param {String} bodyid l10n ID of the label to show as body.
* @param {String} onClick Optional handler if the notification is clicked
* @memberof Screenshot.prototype
*/
//TODO: l10n
notify: function (titleid, body, bodyid, onClick) {
console.log("A notification would be send: " + titleid);
var notification = new window.Notification(titleid, {
body: body,
icon: '/style/icons/Gallery.png'
});
notification.onclick = function () {
notification.close();
if (onClick) {
new MozActivity({
name: "view",
data: {
type: "url",
url: body
}
});
}
};
}
};
console.log("Lets start!");
var uploader = new uploader();
}());
</code></pre>
</section>
</section>
<section>
<h2 data-l10n-id="title-workers">Web Workers</h2>
<p>TODO</p>
</section>
<section>
<section>
<h2 data-l10n-id="title-practices">Best practices</h2>
<p>
<ul>
<li>Never use synchronous XMLHttpRequests.</li>
<li>Performing sequential, asynchronous operations can often be greatly simplified using Promises.</li>
<li>Avoid writing slow CSS.</li>
<li>Avoid DOM mutation event listeners.</li>
<li>Using window.setInterval() or window.setTimeout() can be problematic, too.</li>
<li>Clean up event listeners.</li>
<li>Don't store references to window objects and DOM nodes for too long.</li>
</ul>
</p>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>