From 9719692512325b5f88de666281ba286343408010 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 12 Apr 2020 20:50:03 +0200 Subject: Introduce serviceWorker, disabled by default --- assets/search.js | 17 ++++++++++++++--- assets/sw-register.js | 6 ++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 assets/sw-register.js (limited to 'assets') diff --git a/assets/search.js b/assets/search.js index d6ae18b..0393d2c 100644 --- a/assets/search.js +++ b/assets/search.js @@ -12,20 +12,27 @@ document.addEventListener('keypress', focusSearchFieldOnKeyPress); - function focusSearchFieldOnKeyPress(e) { + /** + * @param {Event} event + */ + function focusSearchFieldOnKeyPress(event) { if (input === document.activeElement) { return; } - const characterPressed = String.fromCharCode(e.charCode); + const characterPressed = String.fromCharCode(event.charCode); if (!isHotkey(characterPressed)) { return; } input.focus(); - e.preventDefault(); + event.preventDefault(); } + /** + * @param {String} character + * @returns {Boolean} + */ function isHotkey(character) { const dataHotkeys = input.getAttribute('data-hotkeys') || ''; return dataHotkeys.indexOf(character) >= 0; @@ -63,6 +70,10 @@ }); } + /** + * @param {String} src + * @param {Function} callback + */ function loadScript(src, callback) { const script = document.createElement('script'); script.defer = true; diff --git a/assets/sw-register.js b/assets/sw-register.js new file mode 100644 index 0000000..583532c --- /dev/null +++ b/assets/sw-register.js @@ -0,0 +1,6 @@ +if (navigator.serviceWorker) { + navigator.serviceWorker.register( + "{{ "/sw.js" | relURL }}", + { scope: "{{ "/" | relURL }}" } + ); +} -- cgit v1.2.3