1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 13:56:22 -04:00

clean up scripts

This commit is contained in:
Jake Jarvis 2020-05-04 21:25:07 -04:00
parent 759bae4f14
commit 9e1a053d39
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
5 changed files with 154 additions and 116 deletions

View File

@ -2,32 +2,35 @@
/*! Dark mode switcheroo | MIT License | jrvs.io/darkmode */
(function() {
(function () {
// improve variable mangling when minifying
var win = window;
var doc = win.document;
var bod = doc.body;
var cls = bod.classList;
var sto = localStorage;
var body = doc.body;
var classes = body.classList;
var storage = localStorage;
// check for preset `dark_mode_pref` preference in localStorage
// check for preset `dark_mode_pref` preference in local storage
var pref_key = 'dark_mode_pref';
var pref = sto.getItem(pref_key);
var pref = storage.getItem(pref_key);
// change CSS via these <body> classes:
var dark = 'dark';
var light = 'light';
// which class is <body> set to initially?
var default_theme = light;
// use an element with class `dark-mode-toggle` to trigger swap when clicked
var toggle = doc.querySelector('.dark-mode-toggle');
// keep track of current state (light by default) no matter how we got there
var active = false;
// keep track of current state no matter how we got there
var active = (default_theme === dark);
// receives a class name and switches <body> to it
var activateTheme = function(theme) {
cls.remove(dark, light);
cls.add(theme);
var activateTheme = function (theme) {
classes.remove(dark, light);
classes.add(theme);
active = (theme === dark);
};
@ -37,18 +40,23 @@
// user has never clicked the button, so go by their OS preference until/if they do so
if (!pref) {
// check for OS dark mode setting and switch accordingly
var prefers_dark = '(prefers-color-scheme: dark)';
var prefers_light = '(prefers-color-scheme: light)';
// returns media query selector syntax
var prefers = function (theme) {
return '(prefers-color-scheme: ' + theme + ')';
};
if (win.matchMedia(prefers_dark).matches)
// check for OS dark/light mode preference and switch accordingly
// default to `default_theme` set above if unsupported
if (win.matchMedia(prefers(dark)).matches)
activateTheme(dark);
else
else if (win.matchMedia(prefers(light)).matches)
activateTheme(light);
else
activateTheme(default_theme);
// real-time switching if supported by OS/browser
win.matchMedia(prefers_dark).addListener(function(e) { if (e.matches) activateTheme(dark); });
win.matchMedia(prefers_light).addListener(function(e) { if (e.matches) activateTheme(light); });
win.matchMedia(prefers(dark)).addListener(function (e) { if (e.matches) activateTheme(dark); });
win.matchMedia(prefers(light)).addListener(function (e) { if (e.matches) activateTheme(light); });
}
// don't freak out if page happens not to have a toggle
@ -57,18 +65,18 @@
toggle.style.visibility = 'visible';
// handle toggle click
toggle.addEventListener('click', function() {
toggle.addEventListener('click', function () {
// switch to the opposite theme & save preference in local storage
if (!active) {
activateTheme(dark);
sto.setItem(pref_key, dark);
// send click event to analytics
sa_event('triggered_dark_mode');
} else {
if (active) {
activateTheme(light);
sto.setItem(pref_key, light);
// send click event to analytics
storage.setItem(pref_key, light);
// send event to SA
sa_event('triggered_light_mode');
} else {
activateTheme(dark);
storage.setItem(pref_key, dark);
// send event to SA
sa_event('triggered_dark_mode');
}
}, true);
}

View File

@ -1,9 +1,9 @@
/* jshint browser: true, laxbreak:true, -W080 */
/*! Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2020-03-02; 21e2) */
// https://github.com/simpleanalytics/scripts/blob/3918189e9137b55522a2dbeb3d7cb4bc2c26c52a/src/default.js
/*! Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2020-05-03; d98d) */
// https://github.com/simpleanalytics/scripts/blob/915d98d39868cbb578619f64b5e2374a5af60c2b/src/default.js
(function(window, baseUrl, apiUrlPrefix, version, saGlobal) {
(function (window, baseUrl) {
if (!window) return;
// Generate the needed variables, this seems like a lot of repetition, but it
@ -17,6 +17,7 @@
var slash = "/";
var nav = window.navigator;
var loc = window.location;
var hostname = loc.hostname;
var doc = window.document;
var notSending = "Not sending requests ";
var encodeURIComponentFunc = encodeURIComponent;
@ -26,42 +27,46 @@
var addEventListenerFunc = window.addEventListener;
var fullApiUrl = protocol + baseUrl;
var undefinedVar = undefined;
var hostname = loc.hostname;
var functionName = "sa_event";
var payload = {
version: 2
version: 2,
};
payload.hostname = hostname;
var options = {
hostname: hostname,
functionName: functionName,
};
payload.hostname = options.hostname;
// A simple log function so the user knows why a request is not being send
var warn = function(message) {
var warn = function (message) {
if (con && con.warn) con.warn("Simple Analytics:", message);
};
var now = Date.now;
var uuid = function() {
var uuid = function () {
var cryptoObject = window.crypto || window.msCrypto;
var emptyUUID = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
if (!cryptoObject)
return emptyUUID.replace(/[018]/g, function(c) {
var r = (Math.random() * 16) | 0,
v = c < 2 ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
return emptyUUID.replace(/[018]/g, function(c) {
if (cryptoObject && cryptoObject.getRandomValues)
return emptyUUID.replace(/[018]/g, function (c) {
return (
c ^
(cryptoObject.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16);
});
return emptyUUID.replace(/[018]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c < 2 ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
var assign = function() {
var assign = function () {
var to = {};
for (var index = 0; index < arguments.length; index++) {
var nextSource = arguments[index];
@ -84,17 +89,22 @@
/* Do nothing */
}
// Send data via image of XHR request
function sendData(data) {
// Send data via image
function sendData(data, callback) {
data = assign(payload, data);
new Image().src =
var image = new Image();
if (callback) {
image.onerror = callback;
image.onload = callback;
}
image.src =
fullApiUrl +
"/send.gif?" +
Object.keys(data)
.filter(function(key) {
.filter(function (key) {
return data[key] != undefinedVar;
})
.map(function(key) {
.map(function (key) {
return (
encodeURIComponentFunc(key) +
"=" +
@ -111,7 +121,7 @@
sendData({
type: errorText,
error: errorOrMessage,
url: hostname + loc.pathname
url: options.hostname + loc.pathname,
});
}
@ -119,7 +129,7 @@
// from our script (checked by filename) to our server.
addEventListenerFunc(
errorText,
function(event) {
function (event) {
if (event.filename && event.filename.indexOf(baseUrl) > -1) {
sendError(event.message);
}
@ -136,20 +146,23 @@
var scrolled = 0;
/** endif **/
// When a customer overwrites the hostname, we need to know what the original
// hostname was to hide that domain from referrer traffic
if (options.hostname !== hostname) payload.hostname_original = hostname;
// Don't track when localhost
/** unless testing **/
if (loc.hostname.indexOf(".") == -1)
return warn(notSending + "from localhost");
if (hostname.indexOf(".") == -1) return warn(notSending + "from " + hostname);
/** endunless **/
try {
var getParams = function(regex) {
var getParams = function (regex) {
// From the search we grab the utm_source and ref and save only that
var matches = loc.search.match(
new RegExp("[?&](" + regex + ")=([^?&]+)", "gi")
);
var match = matches
? matches.map(function(m) {
? matches.map(function (m) {
return m.split("=")[1];
})
: [];
@ -166,10 +179,12 @@
source: getParams(utmRegexPrefix + "source|source|ref"),
medium: getParams(utmRegexPrefix + "medium"),
campaign: getParams(utmRegexPrefix + "campaign"),
term: getParams(utmRegexPrefix + "term"),
content: getParams(utmRegexPrefix + "content"),
referrer:
(doc.referrer || "")
.replace(/^https?:\/\/((m|l|w{2,3}([0-9]+)?)\.)?([^?#]+)(.*)$/, "$4")
.replace(/^([^/]+)\/$/, "$1") || undefinedVar
.replace(/^([^/]+)\/$/, "$1") || undefinedVar,
};
// We don't put msHidden in if duration block, because it's used outside of that functionality
@ -179,7 +194,7 @@
var hiddenStart;
window.addEventListener(
"visibilitychange",
function() {
function () {
if (doc.hidden) hiddenStart = now();
else msHidden += now() - hiddenStart;
},
@ -189,7 +204,7 @@
var sendBeaconText = "sendBeacon";
var sendOnLeave = function(id, push) {
var sendOnLeave = function (id, push) {
var append = { type: "append", original_id: push ? id : lastPageId };
/** if duration **/
@ -218,7 +233,7 @@
var scroll = "scroll";
var body = doc.body || {};
var documentElement = doc.documentElement || {};
var position = function() {
var position = function () {
try {
var Height = "Height";
var scrollHeight = scroll + Height;
@ -245,11 +260,11 @@
}
};
addEventListenerFunc("load", function() {
addEventListenerFunc("load", function () {
scrolled = position();
addEventListenerFunc(
scroll,
function() {
function () {
if (scrolled < position()) scrolled = position();
},
false
@ -257,7 +272,7 @@
});
/** endif **/
var sendPageView = function(isPushState, deleteSourceInfo) {
var sendPageView = function (isPushState, deleteSourceInfo) {
if (isPushState) sendOnLeave("" + lastPageId, true);
lastPageId = uuid();
page.id = lastPageId;
@ -267,12 +282,12 @@
https: loc.protocol == https,
timezone: timezone,
width: window.innerWidth,
type: pageviewsText
type: pageviewsText,
})
);
};
var pageview = function(isPushState) {
var pageview = function (isPushState) {
// Obfuscate personal data in URL by dropping the search and hash
var path = decodeURIComponentFunc(loc.pathname);
@ -282,7 +297,7 @@
lastSendPath = path;
var data = {
path: path
path: path,
};
// If a user does refresh we need to delete the referrer because otherwise it count double
@ -310,7 +325,7 @@
isPushState || userNavigated
? false
: doc.referrer
? doc.referrer.split(slash)[2] != loc.hostname
? doc.referrer.split(slash)[2] != hostname
: true;
/** endif **/
@ -323,46 +338,60 @@
/** if events **/
var sessionId = uuid();
var validTypes = ["string", "number"];
var sendEvent = function(event) {
var endEvent = function () {};
var sendEvent = function (event, callbackRaw) {
var isFunction = event instanceof Function;
if (["string", "number"].indexOf(typeof event) < 0 && !isFunction)
return warn("event is not a string: " + event);
var callback =
callbackRaw instanceof Function ? callbackRaw : function () {};
if (validTypes.indexOf(typeof event) < 0 && !isFunction) {
warn("event is not a string: " + event);
return callback();
}
try {
if (isFunction) {
event = event();
if (["string", "number"].indexOf(typeof event) < 0)
return warn("event function output is not a string: " + event);
if (validTypes.indexOf(typeof event) < 0) {
warn("event function output is not a string: " + event);
return callback();
}
}
} catch (error) {
return warn("in your event function: " + error.message);
warn("in your event function: " + error.message);
return callback();
}
event = ("" + event).replace(/[^a-z0-9]+/gi, "_").replace(/(^_|_$)/g, "");
if (event)
sendData(
assign(source, {
type: "event",
event: event,
session_id: sessionId
})
session_id: sessionId,
}),
callback
);
};
var defaultEventFunc = function(event) {
sendEvent(event);
var defaultEventFunc = function (event, callback) {
sendEvent(event, callback);
};
// Set default function if user didn't define a function
if (!window[functionName]) window[functionName] = defaultEventFunc;
if (!window[options.functionName])
window[options.functionName] = defaultEventFunc;
var eventFunc = window[functionName];
var eventFunc = window[options.functionName];
// Read queue of the user defined function
var queue = eventFunc && eventFunc.q ? eventFunc.q : [];
// Overwrite user defined function
window[functionName] = defaultEventFunc;
window[options.functionName] = defaultEventFunc;
// Post events from the queue of the user defined function
for (var event in queue) sendEvent(queue[event]);

View File

@ -4,14 +4,7 @@
https://github.com/twitter/twemoji/blob/gh-pages/LICENSE
*/
(function (
// WARNING: this file is generated automatically via
// `node scripts/build.js`
// please update its `createTwemoji` function
// at the bottom of the same file instead.
) {
(function () {
'use strict';
/* jshint maxparams:4 */

View File

@ -44,11 +44,11 @@ A _very_ barebones example is embedded above ([view the source here](https://git
---
### Minified JS: (392 bytes gzipped 📦)
### Minified JS: (410 bytes gzipped 📦)
```js
/*! Dark mode switcheroo | MIT License | jrvs.io/darkmode */
(function(){var e=window,t=e.document,i=t.body.classList,a=localStorage,c="dark_mode_pref",d=a.getItem(c),r="dark",o="light",s=t.querySelector(".dark-mode-toggle"),n=!1,m=function(e){i.remove(r,o);i.add(e);n=e===r};d===r&&m(r);d===o&&m(o);if(!d){var l="(prefers-color-scheme: dark)",f="(prefers-color-scheme: light)";e.matchMedia(l).matches?m(r):m(o);e.matchMedia(l).addListener((function(e){e.matches&&m(r)}));e.matchMedia(f).addListener((function(e){e.matches&&m(o)}))}if(s){s.style.visibility="visible";s.addEventListener("click",(function(){if(n){m(o);a.setItem(c,o)}else{m(r);a.setItem(c,r)}}),!0)}})();
(function(){var e=window,t=e.document,i=t.body.classList,a=localStorage,c="dark_mode_pref",d=a.getItem(c),n="dark",o="light",r=o,s=t.querySelector(".dark-mode-toggle"),m=r===n,l=function(e){i.remove(n,o);i.add(e);m=e===n};d===n&&l(n);d===o&&l(o);if(!d){var f=function(e){return"(prefers-color-scheme: "+e+")"};e.matchMedia(f(n)).matches?l(n):e.matchMedia(f(o)).matches?l(o):l(r);e.matchMedia(f(n)).addListener((function(e){e.matches&&l(n)}));e.matchMedia(f(o)).addListener((function(e){e.matches&&l(o)}))}if(s){s.style.visibility="visible";s.addEventListener("click",(function(){if(m){l(o);a.setItem(c,o)}else{l(n);a.setItem(c,n)}}),!0)}})();
```
### Full JS:
@ -56,32 +56,35 @@ A _very_ barebones example is embedded above ([view the source here](https://git
```js
/*! Dark mode switcheroo | MIT License | jrvs.io/darkmode */
(function() {
(function () {
// improve variable mangling when minifying
var win = window;
var doc = win.document;
var bod = doc.body;
var cls = bod.classList;
var sto = localStorage;
var body = doc.body;
var classes = body.classList;
var storage = localStorage;
// check for preset `dark_mode_pref` preference in localStorage
// check for preset `dark_mode_pref` preference in local storage
var pref_key = 'dark_mode_pref';
var pref = sto.getItem(pref_key);
var pref = storage.getItem(pref_key);
// change CSS via these <body> classes:
var dark = 'dark';
var light = 'light';
// which class is <body> set to initially?
var default_theme = light;
// use an element with class `dark-mode-toggle` to trigger swap when clicked
var toggle = doc.querySelector('.dark-mode-toggle');
// keep track of current state (light by default) no matter how we got there
var active = false;
// keep track of current state no matter how we got there
var active = (default_theme === dark);
// receives a class name and switches <body> to it
var activateTheme = function(theme) {
cls.remove(dark, light);
cls.add(theme);
var activateTheme = function (theme) {
classes.remove(dark, light);
classes.add(theme);
active = (theme === dark);
};
@ -91,18 +94,23 @@ A _very_ barebones example is embedded above ([view the source here](https://git
// user has never clicked the button, so go by their OS preference until/if they do so
if (!pref) {
// check for OS dark mode setting and switch accordingly
var prefers_dark = '(prefers-color-scheme: dark)';
var prefers_light = '(prefers-color-scheme: light)';
// returns media query selector syntax
var prefers = function (theme) {
return '(prefers-color-scheme: ' + theme + ')';
};
if (win.matchMedia(prefers_dark).matches)
// check for OS dark/light mode preference and switch accordingly
// default to `default_theme` set above if unsupported
if (win.matchMedia(prefers(dark)).matches)
activateTheme(dark);
else
else if (win.matchMedia(prefers(light)).matches)
activateTheme(light);
else
activateTheme(default_theme);
// real-time switching if supported by OS/browser
win.matchMedia(prefers_dark).addListener(function(e) { if (e.matches) activateTheme(dark); });
win.matchMedia(prefers_light).addListener(function(e) { if (e.matches) activateTheme(light); });
win.matchMedia(prefers(dark)).addListener(function (e) { if (e.matches) activateTheme(dark); });
win.matchMedia(prefers(light)).addListener(function (e) { if (e.matches) activateTheme(light); });
}
// don't freak out if page happens not to have a toggle
@ -111,14 +119,14 @@ A _very_ barebones example is embedded above ([view the source here](https://git
toggle.style.visibility = 'visible';
// handle toggle click
toggle.addEventListener('click', function() {
toggle.addEventListener('click', function () {
// switch to the opposite theme & save preference in local storage
if (!active) {
activateTheme(dark);
sto.setItem(pref_key, dark);
} else {
if (active) {
activateTheme(light);
sto.setItem(pref_key, light);
storage.setItem(pref_key, light);
} else {
activateTheme(dark);
storage.setItem(pref_key, dark);
}
}, true);
}

View File

@ -19,4 +19,4 @@ This website uses [**Simple Analytics**](https://simpleanalytics.com/?ref=jarv.i
**📈 Stats for this site are public! [View them here.](/stats/)**
Pages and first-party assets on this website are served by [**Netlify**](https://www.netlify.com/). Refer to their [privacy policy](https://www.netlify.com/privacy/) and [GDPR statement](https://www.netlify.com/privacy/) for more information.
Pages and first-party assets on this website are served by [**Netlify**](https://www.netlify.com/). Refer to their [privacy policy](https://www.netlify.com/privacy/) and [GDPR statement](https://www.netlify.com/gdpr/) for more information.