JavaScript APIs - The Web is the Platform - .toster conference, Moscow

73
JavaScript APIs - The Web is the Platform

description

 

Transcript of JavaScript APIs - The Web is the Platform - .toster conference, Moscow

Page 1: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

JavaScript APIs - The Web is the

Platform

Page 2: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 3: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 4: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 7: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

Mozilla is a global non-profit dedicated to putting you in control of your online experience and shaping the future of the Web for the public good

Page 9: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 10: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

File API

Page 11: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

<!-- The multiple attribute allows for uploading of multiple files--><input id="files-upload" type="file" multiple>

Page 12: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

var filesUpload = document.getElementById("files-upload");filesUpload.onchange = function () { // Access to data about all files var files = this.files, file; for (var i=0, l=files.length; i<l; i++) { file = file[i]; file.name; // Get the name of the file file.size; // Get the size of the file, in bytes file.type; // Get the type of the file };};

Page 13: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

for (var i=0, l=files.length, file, img; i<l; i++) { file = files[i]; if (typeof FileReader !== "undefined") { img = document.createElement("img"); reader = new FileReader(); reader.onload = (function (theImg) { return function (evt) { theImg.src = evt.target.result; }; }(img)); reader.readAsDataURL(file); }}

Page 14: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// For Firefox, Google Chrome and Safarivar xhr = new XMLHttpRequest();xhr.open("post", "upload/upload.php", true);xhr.onreadystatechange = function() { if (this.readyState === 4) { // File uploaded }};

// Upload file: Firefox, Google Chrome and Safarixhr.setRequestHeader("Content-Type", "multipart/form-data");xhr.setRequestHeader("X-File-Name", file.fileName);xhr.setRequestHeader("X-File-Size", file.fileSize);xhr.setRequestHeader("X-File-Type", file.type);

xhr.send(file);

Page 16: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

must die!!!Not rea

lly

Page 17: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 22: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 23: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 24: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

<button id="view-fullscreen">Fullscreen</button>

<script type="text/javascript">(function () { var viewFullScreen = document.getElementById("view-fullscreen"); if (viewFullScreen) { viewFullScreen.addEventListener("click", function () { var docElm = document.documentElement; if (docElm.mozRequestFullScreen) { docElm.mozRequestFullScreen(); } else if (docElm.webkitRequestFullScreen) { docElm.webkitRequestFullScreen(); } }, false); }})(); </script>

Page 25: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

mozRequestFullScreenWithKeys?

Page 26: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

html:-moz-full-screen { background: red;}

html:-webkit-full-screen { background: red;}

Page 28: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 29: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

<input type="file" id="take-picture" accept="image/*">

Page 30: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

takePicture.onchange = function (event) { // Get a reference to the taken picture or chosen file var files = event.target.files, file; if (files && files.length > 0) { file = files[0]; // Get window.URL object var URL = window.URL || window.webkitURL;

// Create ObjectURL var imgURL = URL.createObjectURL(file);

// Set img src to ObjectURL showPicture.src = imgURL;

// Revoke ObjectURL URL.revokeObjectURL(imgURL); }};

Page 32: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

var liveVideo = document.querySelector("#live-video");navigator.getUserMedia( {video: true}, function (stream) { liveVideo.src = stream; }, function (error) { console.log("An error occurred: " + error); });

Page 34: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

var docElm = document.documentElement; // Requesting Pointer LockdocElm.requestPointerLock = elem.requestPointerLock || elem.mozRequestPointerLock || elem.webkitRequestPointerLock; docElm.requestPointerLock();

Page 35: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

document.addEventListener("mousemove", function(e) { var movementX = e.movementX || e.mozMovementX || e.webkitMovementX || 0, movementY = e.movementY || e.mozMovementY || e.webkitMovementY || 0;

// Print the mouse movement delta values console.log("movementX=" + movementX, "movementY=" + movementY); }, false);

Page 39: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

sessionStorage.setItem("Wrestles", "Bears");console.log(sessionStorage.getItem("Wrestles"));

Page 40: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

localStorage.setItem("Occupation", "Politician");

Page 41: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

var vladimirPutin = { "sings" : "yes", "song" : "Blueberry Hill"};

localStorage.setItem("vladimirPutin", JSON.stringify(vladimirPutin));

console.log(typeof JSON.parse(localStorage.getItem("vladimirPutin")));

Page 43: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// IndexedDBvar indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB, IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction, dbVersion = 1;

// Create/open databasevar request = indexedDB.open("elephantFiles", dbVersion);

Page 44: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

createObjectStore = function (dataBase) { // Create an objectStore dataBase.createObjectStore("elephants");}

// Currently only in latest Firefox versionsrequest.onupgradeneeded = function (event) { createObjectStore(event.target.result);};

Page 45: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

request.onsuccess = function (event) { // Create XHR var xhr = new XMLHttpRequest(), blob;

xhr.open("GET", "elephant.png", true);

// Set the responseType to blob xhr.responseType = "blob";

xhr.addEventListener("load", function () { if (xhr.status === 200) { // Blob as response blob = xhr.response;

// Put the received blob into IndexedDB putElephantInDb(blob); } }, false); // Send XHR xhr.send();}

Page 46: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

putElephantInDb = function (blob) { // Open a transaction to the database var transaction = db.transaction(["elephants"], IDBTransaction.READ_WRITE);

// Put the blob into the dabase var put = transaction.objectStore("elephants").put(blob, "image");

// Retrieve the file that was just stored transaction.objectStore("elephants").get("image").onsuccess = function (event) { var imgFile = event.target.result;

// Get window.URL object var URL = window.URL || window.webkitURL;

// Create and revoke ObjectURL var imgURL = URL.createObjectURL(imgFile);

// Set img src to ObjectURL var imgElephant = document.getElementById("elephant"); imgElephant.setAttribute("src", imgURL);

// Revoking ObjectURL URL.revokeObjectURL(imgURL); };};

Page 48: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 49: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// Get battery level in percentagevar batteryLevel = battery.level * 100 + "%";

// Get whether device is charging or notvar chargingStatus = battery.charging;

// Time until the device is fully chargedvar batteryCharged = battery.chargingTime;

// Time until the device is dischargedvar batteryDischarged = battery.dischargingTime;

Page 50: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

battery.addEventLister("levelchange", function () { // Device's battery level changed}, false);

battery.addEventListener("chargingchange", function () { // Device got plugged in to power, or unplugged}, false);

battery.addEventListener("chargingtimechange", function () { // Device's charging time changed}, false);

battery.addEventListener("dischargingtimechange", function () { // Device's discharging time changed}, false);

Page 52: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 53: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 57: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// Telephony objectvar tel = navigator.mozTelephony;

// Check if the phone is muted (read/write property)console.log(tel.muted);

// Check if the speaker is enabled (read/write property)console.log(tel.speakerEnabled);

// Place a callvar call = tel.dial("123456789");

Page 58: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// Receiving a calltel.onincoming = function (event) { var incomingCall = event.call;

// Get the number of the incoming call console.log(incomingCall.number);

// Answer the call incomingCall.answer();};

// Disconnect a callcall.hangUp();

Page 59: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

// SMS objectvar sms = navigator.mozSMS;

// Send a messagesms.send("123456789", "Hello world!");

// Recieve a messagesms.onrecieved = function (event) { // Read message console.log(event.message);};

Page 61: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

(function () { document.querySelector("#vibrate-one-second").addEventListener("click", function () { navigator.mozVibrate(1000); }, false);

document.querySelector("#vibrate-twice").addEventListener("click", function () { navigator.mozVibrate([200, 100, 200, 100]); }, false);

document.querySelector("#vibrate-long-time").addEventListener("click", function () { navigator.mozVibrate(5000); }, false);

document.querySelector("#vibrate-off").addEventListener("click", function () { navigator.mozVibrate(0); }, false);})();

Page 67: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 70: JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Page 71: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

HTML5 -

The beauty of the Open Web

Page 72: JavaScript APIs - The Web is the Platform - .toster conference, Moscow

"So we saved the world together for a while, and that was lovely."

- Lost