mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-25 07:51:12 +02:00
add offline enabled build process
This commit is contained in:
11
src/db.js
11
src/db.js
@@ -1,8 +1,11 @@
|
||||
(() => {
|
||||
const FAUX_DELAY = 1;
|
||||
var local = {
|
||||
get: (obj, cb) => {
|
||||
if (typeof obj === 'string') {
|
||||
setTimeout(() => cb(window.localStorage.getItem(obj)), 100);
|
||||
const retVal = {};
|
||||
retVal[obj] = JSON.parse(window.localStorage.getItem(obj));
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
} else {
|
||||
const retVal = {};
|
||||
Object.keys(obj).forEach(key => {
|
||||
@@ -10,14 +13,16 @@
|
||||
retVal[key] =
|
||||
val === undefined || val === null ? obj[key] : JSON.parse(val);
|
||||
});
|
||||
setTimeout(() => cb(retVal), 100);
|
||||
setTimeout(() => cb(retVal), FAUX_DELAY);
|
||||
}
|
||||
},
|
||||
set: (obj, cb) => {
|
||||
Object.keys(obj).forEach(key => {
|
||||
window.localStorage.setItem(key, JSON.stringify(obj[key]));
|
||||
});
|
||||
setTimeout(() => cb(), 100);
|
||||
setTimeout(() => {
|
||||
if (cb) cb();
|
||||
}, FAUX_DELAY);
|
||||
}
|
||||
};
|
||||
window.db = {
|
||||
|
@@ -583,6 +583,7 @@
|
||||
<!-- endbuild -->
|
||||
|
||||
<!-- build:js script.js -->
|
||||
<script src="service-worker-registration.js"></script>
|
||||
<script src="utils.js"></script>
|
||||
<script src="db.js"></script>
|
||||
<script src="analytics.js"></script>
|
||||
|
@@ -473,7 +473,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
*/
|
||||
function fetchItems(shouldSaveGlobally) {
|
||||
var d = deferred();
|
||||
chrome.storage.local.get('items', function(result) {
|
||||
db.local.get('items', function(result) {
|
||||
var itemIds = Object.getOwnPropertyNames(result.items || {}),
|
||||
items = [];
|
||||
if (!itemIds.length) {
|
||||
@@ -484,7 +484,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
trackEvent('fn', 'fetchItems', itemIds.length);
|
||||
for (let i = 0; i < itemIds.length; i++) {
|
||||
/* eslint-disable no-loop-func */
|
||||
chrome.storage.local.get(itemIds[i], function(itemResult) {
|
||||
db.local.get(itemIds[i], function(itemResult) {
|
||||
if (shouldSaveGlobally) {
|
||||
savedItems[itemIds[i]] = itemResult[itemIds[i]];
|
||||
}
|
||||
@@ -572,20 +572,20 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
|
||||
itemTile.remove();
|
||||
// Remove from items list
|
||||
chrome.storage.local.get(
|
||||
db.local.get(
|
||||
{
|
||||
items: {}
|
||||
},
|
||||
function(result) {
|
||||
delete result.items[itemId];
|
||||
chrome.storage.local.set({
|
||||
db.local.set({
|
||||
items: result.items
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
// Remove individual item too.
|
||||
chrome.storage.local.remove(itemId, function() {
|
||||
db.local.remove(itemId, function() {
|
||||
alertsService.add('Item removed.');
|
||||
// This item is open in the editor. Lets open a new one.
|
||||
if (currentItem.id === itemId) {
|
||||
@@ -1428,13 +1428,13 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
}
|
||||
if (mergedItemCount) {
|
||||
// save new items
|
||||
chrome.storage.local.set(toMergeItems, function() {
|
||||
db.local.set(toMergeItems, function() {
|
||||
alertsService.add(
|
||||
mergedItemCount + ' creations imported successfully.'
|
||||
);
|
||||
});
|
||||
// Push in new item IDs
|
||||
chrome.storage.local.get(
|
||||
db.local.get(
|
||||
{
|
||||
items: {}
|
||||
},
|
||||
@@ -1443,7 +1443,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
for (var id in toMergeItems) {
|
||||
result.items[id] = true;
|
||||
}
|
||||
chrome.storage.local.set({
|
||||
db.local.set({
|
||||
items: result.items
|
||||
});
|
||||
trackEvent('fn', 'itemsImported', mergedItemCount);
|
||||
@@ -1691,7 +1691,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
utils.log(settingName, el.type === 'checkbox' ? el.checked : el.value);
|
||||
prefs[settingName] = el.type === 'checkbox' ? el.checked : el.value;
|
||||
obj[settingName] = prefs[settingName];
|
||||
chrome.storage.sync.set(obj, function() {
|
||||
db.sync.set(obj, function() {
|
||||
alertsService.add('Setting saved');
|
||||
});
|
||||
trackEvent('ui', 'updatePref-' + settingName, prefs[settingName]);
|
||||
|
62
src/service-worker-registration.js
Normal file
62
src/service-worker-registration.js
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright 2015 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* eslint-env browser */
|
||||
'use strict';
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
// Delay registration until after the page has loaded, to ensure that our
|
||||
// precaching requests don't degrade the first visit experience.
|
||||
// See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration
|
||||
window.addEventListener('load', function() {
|
||||
// Your service-worker.js *must* be located at the top-level directory relative to your site.
|
||||
// It won't be able to control pages unless it's located at the same level or higher than them.
|
||||
// *Don't* register service worker file in, e.g., a scripts/ sub-directory!
|
||||
// See https://github.com/slightlyoff/ServiceWorker/issues/468
|
||||
navigator.serviceWorker.register('service-worker.js').then(function(reg) {
|
||||
// updatefound is fired if service-worker.js changes.
|
||||
reg.onupdatefound = function() {
|
||||
// The updatefound event implies that reg.installing is set; see
|
||||
// https://w3c.github.io/ServiceWorker/#service-worker-registration-updatefound-event
|
||||
var installingWorker = reg.installing;
|
||||
|
||||
installingWorker.onstatechange = function() {
|
||||
switch (installingWorker.state) {
|
||||
case 'installed':
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the old content will have been purged and the fresh content will
|
||||
// have been added to the cache.
|
||||
// It's the perfect time to display a "New content is available; please refresh."
|
||||
// message in the page's interface.
|
||||
console.log('New or updated content is available.');
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a "Content is cached for offline use." message.
|
||||
console.log('Content is now available offline!');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'redundant':
|
||||
console.error('The installing service worker became redundant.');
|
||||
break;
|
||||
}
|
||||
};
|
||||
};
|
||||
}).catch(function(e) {
|
||||
console.error('Error during service worker registration:', e);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user