Skip to content

Instantly share code, notes, and snippets.

View JustinDFuller's full-sized avatar
🧐

Justin Fuller JustinDFuller

🧐
View GitHub Profile
@JustinDFuller
JustinDFuller / video.js
Last active January 6, 2017 13:05
Autoplay Video
const videoContainer = document.getElementById('myVideoID');
const suppressAutoplayForUser = /* Use an external service like a database to fill this in */
const video = document.createElement('video');
video.autoplay = suppressAutoplayForUser ? false : true;
video.src = 'myAwesomeVideo.mp4';
video.textContent = 'Your browser does not support this media type, or the Video tag.';
videoContainer.appendChild(video);
/* Creates:
<div id='myVideoID'>
@JustinDFuller
JustinDFuller / video.js
Last active January 6, 2017 12:23
Reload A Video On Error
const videoElement = document.getElementById('myVideo');
videoElement.onerror = onerror;
const errorButton = document.createElement('button');
errorButton.setAttribute('type', 'button');
errorButton.setAttribute('id', 'reload');
errorButton.setAttribute('onclick', 'reloadVideo()');
errorButton.textContent = 'Reload';
errorButton.addEventListener("click", reloadVideo);
@JustinDFuller
JustinDFuller / video.js
Created January 6, 2017 12:31
Change video src based on loading times.
let loadingTimes = 0;
const maxLoadingTimesBeforeChange = 4;
const videoOptions = {
hd: 'myAwesomeVideoHD.mp4',
sd: 'myAwesomeVideoSD.mp4',
};
const video = `<video id='myVideo' src=${videoOptions.hd}></video>`;
const videoContainer = document.getElementById('myVideoContainerID');
videoContainer.innerHTML = video;
@JustinDFuller
JustinDFuller / console.js
Created January 7, 2017 17:22
Console.Assert example
const test = getFalseValue();
function getFalseValue () {
return false;
}
console.assert(test, 'Test value was false', test);
// Outputs: "Assertion failed: Test value was false false"
@JustinDFuller
JustinDFuller / console-count.js
Last active January 7, 2017 17:32
Console.cont
function myFunction (userID) {
console.count(`MyFunction called with user: ${userID}`);
}
let users = 5;
while (users--) {
myFunction(users);
}
@JustinDFuller
JustinDFuller / console-trace.js
Last active January 7, 2017 18:18
Console.trace
function ends () {
console.trace();
}
function middle () {
ends();
}
function begins () {
middle();
@JustinDFuller
JustinDFuller / console-formatting.js
Last active January 7, 2017 18:01
Console Formatting
console.log('This will format a %s then the number %i, then the object %o, then the floating point number %.2f',
"String",
5,
{ name: "Justin"},
5.5855
);
// Outputs:
// "This will format a String then the number 5, then the object Objectname: "Justin"__proto__: Object, then the floating point number 5.5855"
@JustinDFuller
JustinDFuller / battery.js
Last active January 9, 2017 13:04
Battery status API
if ('getBattery' in navigator) {
navigator.getBattery().then(battery => {
console.log('Initial', battery);
battery.onchargingchange = onchange;
battery.onchargingtimechange = onchange;
battery.ondischargingtimechange = onchange;
battery.onlevelchange = onchange;
function onchange (event) {
console.log('On change, type: %s', event.type, battery);
@JustinDFuller
JustinDFuller / low-battery.js
Last active January 11, 2017 01:25
Watching for the battery getting low
let warningShowed = false;
if ('getBattery' in navigator) {
navigator.getBattery().then(battery => {
checkBatteryLevel();
battery.onchargingchange = checkBatteryLevel;
battery.onchargingtimechange = checkBatteryLevel;
battery.ondischargingtimechange = checkBatteryLevel;
battery.onlevelchange = checkBatteryLevel;
@JustinDFuller
JustinDFuller / on-copy.js
Last active January 16, 2017 02:16
Stopping copying
const modifyCopy = e => {
e.clipboardData.setData('text/plain', 'Please don\'t copy our work!');
e.preventDefault();
};
document.addEventListener('copy', modifyCopy);