Skip to content

Instantly share code, notes, and snippets.

View deanhume's full-sized avatar
🎮
Gaming

Dean deanhume

🎮
Gaming
View GitHub Profile
@deanhume
deanhume / clear-cache.js
Created September 20, 2017 08:22
Clear Service Worker Cache
if ('serviceWorker' in navigator) {
caches.keys().then(function(cacheNames) {
cacheNames.forEach(function(cacheName) {
caches.delete(cacheName);
});
});
}
@deanhume
deanhume / fetch-api-post.js
Created August 19, 2015 10:48
A simple POST request using the fetch API
fetch(url, {
method: 'POST',
headers: {
'auth': '1234'
},
body: JSON.stringify({
name: 'dean',
login: 'dean',
})
})
@deanhume
deanhume / XHR-request.js
Created August 19, 2015 10:46
A simple XHR request
function successListener() {
var data = JSON.parse(this.responseText);
console.log(data);
}
function failureListener(err) {
console.log('Request failed', err);
}
var request = new XMLHttpRequest();
@deanhume
deanhume / web-share.js
Created February 22, 2017 21:34
Web Share API example
var shareButton = document.getElementById('shareThis');
var supported = document.getElementById('support');
// Listen for any clicks
shareButton.addEventListener('click', function (ev) {
// Check if the current browser supports the Web Share API
if (navigator.share !== undefined) {
// Get the canonical URL from the link tag
var shareUrl = document.querySelector('link[rel=canonical]') ? document.querySelector('link[rel=canonical]').href : window.location.href;
@deanhume
deanhume / persistent-storage.js
Last active July 31, 2023 00:37
Persistent Storage API - Progressive Web Apps
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then(granted => {
if (granted) {
alert("Storage will persist and not be cleared");
} else {
alert("Storage won’t persist and may be cleared");
}
});
};
@deanhume
deanhume / example-html.html
Created December 15, 2015 17:46
Example HTML file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Service Worker Toolbox</title>
</head>
<body>
<!-- Images -->
<img src="/images/contact.svg" height="80" width="80" />
<img src="/images/info.svg" height="80" width="80" />
@deanhume
deanhume / face-detection.js
Last active July 24, 2022 00:48
Face Detection - Shape Detection API
var image = document.getElementById('image');
// Does the current browser support the API?
if (window.FaceDetector) {
var faceDetector = new FaceDetector();
faceDetector.detect(image)
.then(faces => {
console.log(‘Faces found:’, faces.length);
@deanhume
deanhume / intersection-observer.js
Last active May 20, 2021 17:55
Intersection Observer - Image lazy load
// Get all of the images that are marked up to lazy load
const images = document.querySelectorAll('.js-lazy-image');
const config = {
// If the image gets within 50px in the Y axis, start the download.
rootMargin: '50px 0px',
threshold: 0.01
};
// The observer for the images on the page
let observer = new IntersectionObserver(onIntersection, config);
@deanhume
deanhume / service-worker-ghost-cms.js
Created March 23, 2018 16:46
Service Worker for Ghost CMS
const cacheName = 'blogCache';
const offlineUrl = '/offline/';
self.addEventListener('install', event => {
event.waitUntil(
caches.open(cacheName)
.then(cache => cache.addAll([
'./assets/font/icons.woff2',
'./assets/js/script.js',
'https://fonts.googleapis.com/css?family=Open+Sans:400,700',
@deanhume
deanhume / workbox-sw.js
Created July 27, 2017 06:31
Workbox Caching Example
importScripts('workbox-sw.prod.v1.1.0.js');
const workboxSW = new self.WorkboxSW();
workboxSW.precache(fileManifest);
// The route for any requests from the googleapis origin
workboxSW.router.registerRoute('https://fonts.googleapis.com/(.*)',
workboxSW.strategies.cacheFirst({
cacheName: 'googleapis',
cacheableResponse: {