-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
016acb1
commit 2b37104
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
|
||
<link rel="canonical" href="https://jonathanzuniga.github.io/infinite-fart/" /> | ||
|
||
<title>Infinite fart</title> | ||
</head> | ||
<body> | ||
<div id="status"></div> | ||
|
||
<div id="infinite" style="height: 2000px"></div> | ||
|
||
<script type="text/javascript" src="http://code.onion.com/fartscroll.js"></script> | ||
<script type="text/javascript" src="scripts.js"></script> | ||
<!-- <script type="text/javascript" src="service-worker.js"></script> --> | ||
|
||
<script type="text/javascript"> | ||
// register ServiceWorker, remember to use absolute path! | ||
if (navigator.serviceWorker) { | ||
navigator.serviceWorker.register('/infinite-fart/sw.js', { scope: '/infinite-fart/' }) | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Fart every 400 pixels scrolled | ||
fartscroll(); | ||
|
||
var scroll = function test() { | ||
console.log(document.getElementById('infinite').style.height) | ||
var el = document.getElementById('infinite'); | ||
var newHeight = document.getElementById('infinite').offsetHeight + 10; | ||
el.style.height = newHeight + 'px'; | ||
} | ||
window.addEventListener('scroll', scroll, false); | ||
|
||
|
||
|
||
window.addEventListener('load', function() { | ||
var status = document.getElementById('status'); | ||
|
||
function updateOnlineStatus(event) { | ||
var condition = navigator.onLine ? 'online' : 'offline'; | ||
|
||
status.className = condition; | ||
status.innerHTML = condition.toUpperCase(); | ||
} | ||
|
||
window.addEventListener('online', updateOnlineStatus); | ||
window.addEventListener('offline', updateOnlineStatus); | ||
}); | ||
|
||
if (navigator.onLine === false) { | ||
alert('You seem to be offline.'); | ||
console.log('Is offline.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const CACHE_NAME = 'infinite-fart' | ||
const urlsToCache = [ | ||
'/', | ||
'/fartscroll.js', | ||
'/scripts.js' | ||
] | ||
|
||
function respondFromNetworkThenCache (event) { | ||
// Check network first, then cache | ||
const request = event.request | ||
event.respondWith( | ||
fetch(request) | ||
.then(response => addToCache(request, response)) | ||
.catch(() => fetchFromCache(event)) | ||
.catch(() => offlineResponse()) | ||
) | ||
} | ||
|
||
function respondFromCacheThenNetwork (event) { | ||
// Check cache first, then network | ||
const request = event.request | ||
event.respondWith( | ||
fetchFromCache(event) | ||
.catch(() => fetch(request)) | ||
.then(response => addToCache(request, response)) | ||
.catch(() => offlineResponse()) | ||
) | ||
} | ||
|
||
self.addEventListener('install', (event) => { | ||
// Perform install steps | ||
event.waitUntil( | ||
caches.open(CACHE_NAME) | ||
.then((cache) => { | ||
return cache.addAll(urlsToCache); | ||
}) | ||
) | ||
}) | ||
|
||
self.addEventListener('fetch', (event) => { | ||
// Handle requests | ||
if (shouldHandleFetch(event)) { | ||
if (event.request.headers.get('Accept').indexOf('text/html') >= 0) { | ||
respondFromNetworkThenCache(event) | ||
} else { | ||
respondFromCacheThenNetwork(event) | ||
} | ||
} | ||
}) | ||
|
||
self.addEventListener('activate', (event) => { | ||
var cacheWhitelist = [CACHE_NAME] | ||
// Clean up old cache versions | ||
event.waitUntil( | ||
caches.keys().then(function(cacheNames) { | ||
return Promise.all( | ||
cacheNames.map(function(cacheName) { | ||
if (cacheWhitelist.indexOf(cacheName) === -1) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
) | ||
}) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var APP_PREFIX = 'infinite-fart_' // Identifier for this app (this needs to be consistent across every cache update) | ||
var VERSION = 'version_01' // Version of the off-line cache (change this value everytime you want to update cache) | ||
var CACHE_NAME = APP_PREFIX + VERSION | ||
var URLS = [ // Add URL you want to cache in this list. | ||
'/infinite-fart/', // If you have separate JS/CSS files, | ||
'/infinite-fart/index.html', // add path to those files here | ||
'/infinite-fart/fartscroll.js', | ||
'/infinite-fart/scripts.js' | ||
] | ||
|
||
// Respond with cached resources | ||
self.addEventListener('fetch', function (e) { | ||
console.log('fetch request : ' + e.request.url) | ||
e.respondWith( | ||
caches.match(e.request).then(function (request) { | ||
if (request) { // if cache is available, respond with cache | ||
console.log('responding with cache : ' + e.request.url) | ||
return request | ||
} else { // if there are no cache, try fetching request | ||
console.log('file is not cached, fetching : ' + e.request.url) | ||
return fetch(e.request) | ||
} | ||
|
||
// You can omit if/else for console.log & put one line below like this too. | ||
// return request || fetch(e.request) | ||
}) | ||
) | ||
}) | ||
|
||
// Cache resources | ||
self.addEventListener('install', function (e) { | ||
e.waitUntil( | ||
caches.open(CACHE_NAME).then(function (cache) { | ||
console.log('installing cache : ' + CACHE_NAME) | ||
return cache.addAll(URLS) | ||
}) | ||
) | ||
}) | ||
|
||
// Delete outdated caches | ||
self.addEventListener('activate', function (e) { | ||
e.waitUntil( | ||
caches.keys().then(function (keyList) { | ||
// `keyList` contains all cache names under your username.github.io | ||
// filter out ones that has this app prefix to create white list | ||
var cacheWhitelist = keyList.filter(function (key) { | ||
return key.indexOf(APP_PREFIX) | ||
}) | ||
// add current cache name to white list | ||
cacheWhitelist.push(CACHE_NAME) | ||
|
||
return Promise.all(keyList.map(function (key, i) { | ||
if (cacheWhitelist.indexOf(key) === -1) { | ||
console.log('deleting cache : ' + keyList[i] ) | ||
return caches.delete(keyList[i]) | ||
} | ||
})) | ||
}) | ||
) | ||
}) |