Upgrade to Pro — share decks privately, control downloads, hide ads and more …

New Web Platform APIs

New Web Platform APIs

A look at new (stable + experimental) Web platform APIs that we can already use nowadays.

Arnelle Balane

November 18, 2017
Tweet

More Decks by Arnelle Balane

Other Decks in Programming

Transcript

  1. // Create a broadcast channel const channel = new BroadcastChannel('channel-name');

    // Broadcast data channel.postMessage({ title: 'New Web Platform APIs' });
  2. // Create a broadcast channel const channel = new BroadcastChannel('channel-name');

    // Broadcast data channel.postMessage({ title: 'New Web Platform APIs' }); // Receive data (in another context) channel.onmessage = (e) => { // e.data == { title: 'New Web Platform APIs' } };
  3. // Listen for network connection changes navigator.connection.onchange = (e) =>

    { console.log('Network type changed to ' + e.target.type); };
  4. navigator.share({ title: 'New Web Platform APIs', text: 'Check out this

    Web Share API demo!', url: 'https://arnellebalane.com/web-share-api' }) .then(() => console.log('Share successful.')) .catch((error) => console.error('Share failed.'));
  5. // Google Signin const profile = user.getBasicProfile(); const credential =

    new FederatedCredential({ id: profile.getEmail(), name: profile.getName(), iconURL: profile.getImageUrl(), provider: 'https://accounts.google.com' });
  6. // Google Signin const profile = user.getBasicProfile(); const credential =

    new FederatedCredential({ id: profile.getEmail(), name: profile.getName(), iconURL: profile.getImageUrl(), provider: 'https://accounts.google.com' }); navigator.credentials.store(credential);
  7. navigator.credentials.get({ password: true, federated: { providers: ['https://accounts.google.com'] } }) .then(credential

    => { if (credential.type === 'password') { signInWithPassword(credential); } });
  8. navigator.credentials.get({ password: true, federated: { providers: ['https://accounts.google.com'] } }) .then(credential

    => { if (credential.type === 'password') { signInWithPassword(credential); } else if (credential.type === 'federated') { signInWithGoogle(credential); } });
  9. const methodData = { ... }; const details = {

    displayItems: [ { label: 'GDG DevFest Cebu 2017 Ticket', amount: { currency: 'USD', value: '10.00' } } ], total: { label: 'Total', amount: { currency: 'USD', value: '10.00' } } };
  10. const methodData = { ... }; const details = {

    ... }; const options = { requestPayerName: true, requestPayerEmail: true };
  11. const methodData = { ... }; const details = {

    ... }; const options = { ... }; const request = new PaymentRequest( methodData, details, options );
  12. const request = new PaymentRequest(...); request.show().then(response => { // {

    // details: { // cardNumber: '0000 0000 0000 0000', // cardSecurityCode: '000', // ... // }, // methodName: 'mastercard', // payerName: 'Arnelle Balane', // payerEmail: '[email protected]' // } });
  13. const elementOne = document.querySelector('#element-one'); const observer = new ResizeObserver(entries =>

    { // Run when observed elements' size changes }); // Observe changes in element’s size observer.observe(elementOne);
  14. const elementOne = document.querySelector('#element-one'); const observer = new ResizeObserver(entries =>

    { // Run when observed elements' size changes }); // Stop observing an element observer.unobserve(elementOne);
  15. const elementOne = document.querySelector('#element-one'); const observer = new ResizeObserver(entries =>

    { // Run when observed elements' size changes }); // Stop observing all elements observer.disconnect();
  16. const metadata = new MediaMetadata({ title: 'Song Title', artist: 'Artist

    Name', album: 'The Ultimate Album', artwork: [ { src: 'images/artwork-192.png', sizes: '192x192', type: 'image/png' } ] });
  17. const metadata = new MediaMetadata({ title: 'Song Title', artist: 'Artist

    Name', album: 'The Ultimate Album', artwork: [ { src: 'images/artwork-192.png', sizes: '192x192', type: 'image/png' } ] }); navigator.mediaSession.metadata = metadata;
  18. const mediaSession = navigator.mediaSession; mediaSession.setActionHandler('play', () => { ... });

    mediaSession.setActionHandler('pause', () => { ... }); mediaSession.setActionHandler('seekbackward', () => { ... }); mediaSession.setActionHandler('seekforward', () => { ... }); mediaSession.setActionHandler('previoustrack', () => { ... }); mediaSession.setActionHandler('nexttrack', () => { ... });
  19. const sensor = new LinearAccelerationSensor({ frequency: 50 // Hz, or

    readings per second }); sensor.onreading = (e) => { // e.target.x // e.target.y // e.target.z };
  20. const sensor = new LinearAccelerationSensor({ frequency: 50 // Hz, or

    readings per second }); sensor.onreading = (e) => { // e.target.x // e.target.y // e.target.z }; sensor.onerror = (e) => handleError(e);
  21. const sensor = new LinearAccelerationSensor({ ... }); sensor.onreading = (e)

    => { ... }; sensor.onerror = (e) => { ... }; // Start reporting sensor readings sensor.start();
  22. const sensor = new LinearAccelerationSensor({ ... }); sensor.onreading = (e)

    => { ... }; sensor.onerror = (e) => { ... }; // Stop reporting sensor readings sensor.stop();
  23. const detector = new FaceDetector(); detector.detect(imageSource).then(faces => { // [

    { // boundingBox: { // top: 100, // left: 100, // width: 100, // height: 100, // ... // }, // landmarks: [ ... ] // } ] });
  24. const detector = new TextDetector(); detector.detect(imageSource).then(texts => { // [

    { // boundingBox: { ... }, // rawValue: 'hello world' // } ] });
  25. const detector = new BarcodeDetector(); detector.detect(imageSource).then(barcodes => { // [

    { // boundingBox: { ... }, // rawValue: 'https://arnellebalane.com/', // cornerPoints: [ // { x: 0, y: 0 }, // { x: 100, y: 0 }, // { x: 100, y: 100 }, // { x: 0, y: 100 }, // ] // } ] });
  26. navigator.bluetooth.requestDevice({ filters: [ { services: ['battery_service'] } ] }) .then(device

    => device.gatt.connect()) .then(server => server.getPrimaryService('battery_service'))
  27. navigator.bluetooth.requestDevice({ filters: [ { services: ['battery_service'] } ] }) .then(device

    => device.gatt.connect()) .then(server => server.getPrimaryService('battery_service')) .then(service => service.getCharacteristic('battery_level'))
  28. navigator.bluetooth.requestDevice({ filters: [ { services: ['battery_service'] } ] }) .then(device

    => device.gatt.connect()) .then(server => server.getPrimaryService('battery_service')) .then(service => service.getCharacteristic('battery_level')) .then(characteristic => characteristic.readValue()) .then(value => console.log(value));