APIS de JS
Índice
APIS DE HTML5
var original = document.getElementById("original").value;
if (window.localStorage) localStorage.original = original;
Mecanismos de almacenamiento
1. locallocaStorage
2. sessionStorage
function getPos(position) {
var lat = position.coords.latitude,
lon = position.coords.longitude;
var img = "http://maps.googleapis.com/maps/api/staticmap?center="
+lat+","+lon+"&zoom=15&size=500x300&sensor=false";
document.getElementById('mapa').innerHTML = "<img src='"+img+"'>";
}
var speaker = new SpeechSynthesisUtterance();
speaker.voice = 'Google Español';
speaker.lang = 'es-ES';
speaker.text = 'Texto de prueba';
speechSynthesis.speak(speaker);
speechSynthesis.getVoices();
<button onClick="audio.play()">Play/Continue</button>
<button onClick="audio.pause()">Pause</button>
<script>
var audio = new Audio(url);
</script>
<script>
title = '¡Ha recibido un correo!';
options = {
body: 'Correo de la ULL',
tag: 'idmsg1',
icon: 'imagen'
};
Notification.requestPermission(function(status) {
console.log('User Notification Permission: ' + status);
new Notification(title, options);
});
</script>
/*Acelerómetro*/
window.addEventListener('devicemotion', function(e) {
document.getElementById('accelx').value = e.acceleration.x;
document.getElementById('accely').value = e.acceleration.y;
document.getElementById('accelz').value = e.acceleration.z;
});
/*Vibración*/
navigator.vibrate(500);
/*Batería*/
var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;
function updateBatteryStatus() {
console.log("Battery status: " + battery.level * 100 + " %");
if (battery.charging) {
console.log("Battery is charging");
}
}
Console
console.log('hello world');
/*Prints: hello world, to stdout*/
console.error(new Error('Whoops, something bad happened'));
/*Prints: [Error: Whoops, something bad happened], to stderr*/
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
/*Prints: Danger Will Robinson! Danger!, to stderr*/
Errores
const fs = require('fs');
fs.readFile('a file that does not exist', (err, data) => {
if (err) {
console.error('There was an error reading the file!', err);
return;
}
});
try {
const m = 1;
const n = 0;
const x = m / n;
} catch (err) {
console.error('Error, n is 0');
}
Assert
const assert = require('assert');
assert(true);
/*OK*/
assert(1);
/*OK*/
assert(false);
/*throws "AssertionError: false == true"*/
assert(0);
/*throws "AssertionError: 0 == true"*/
assert(false, 'it\'s false');
/*throws "AssertionError: it's false"*/
Crypto
const crypto = require('crypto');
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('hex');
console.log(hash);
Child Process
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Process
process.argv;
process.cwd();
process.env.VAR;
process.chdir(directory);
process.getuid();
process.getegid();
process.cpuUsage();
process.kill(pid, signal);
Readline
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
/*TODO: Log the answer in a database*/
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
Google Analytics API
Blogger API
CustomSearch API
Drive API
Glass Mirror API
URL Shortener API
YouTube API
Gmail API