mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-17 12:01:13 +02:00
Add a database proxy
This commit is contained in:
27
src/db.js
Normal file
27
src/db.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
(() => {
|
||||||
|
var local = {
|
||||||
|
get: (obj, cb) => {
|
||||||
|
if (typeof obj === 'string') {
|
||||||
|
setTimeout(() => cb(window.localStorage.getItem(obj)), 100);
|
||||||
|
} else {
|
||||||
|
const retVal = {};
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
|
let val = window.localStorage.getItem(key);
|
||||||
|
retVal[key] =
|
||||||
|
val === undefined || val === null ? obj[key] : JSON.parse(val);
|
||||||
|
});
|
||||||
|
setTimeout(() => cb(retVal), 100);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
set: (obj, cb) => {
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
|
window.localStorage.setItem(key, JSON.stringify(obj[key]));
|
||||||
|
});
|
||||||
|
setTimeout(() => cb(), 100);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.db = {
|
||||||
|
local: chrome && chrome.storage ? chrome.storage.local : local,
|
||||||
|
sync: chrome && chrome.storage ? chrome.storage.sync : local
|
||||||
|
};
|
||||||
|
})();
|
@@ -584,6 +584,7 @@
|
|||||||
|
|
||||||
<!-- build:js script.js -->
|
<!-- build:js script.js -->
|
||||||
<script src="utils.js"></script>
|
<script src="utils.js"></script>
|
||||||
|
<script src="db.js"></script>
|
||||||
<script src="analytics.js"></script>
|
<script src="analytics.js"></script>
|
||||||
<script src="deferred.js"></script>
|
<script src="deferred.js"></script>
|
||||||
<script src="loader.js"></script>
|
<script src="loader.js"></script>
|
||||||
|
@@ -302,7 +302,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
const d = deferred();
|
const d = deferred();
|
||||||
var obj = {};
|
var obj = {};
|
||||||
obj[setting] = value;
|
obj[setting] = value;
|
||||||
chrome.storage.local.set(obj, d.resolve);
|
db.local.set(obj, d.resolve);
|
||||||
return d.promise;
|
return d.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,13 +322,13 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
});
|
});
|
||||||
// Push into the items hash if its a new item being saved
|
// Push into the items hash if its a new item being saved
|
||||||
if (isNewItem) {
|
if (isNewItem) {
|
||||||
chrome.storage.local.get(
|
db.local.get(
|
||||||
{
|
{
|
||||||
items: {}
|
items: {}
|
||||||
},
|
},
|
||||||
function(result) {
|
function(result) {
|
||||||
result.items[currentItem.id] = true;
|
result.items[currentItem.id] = true;
|
||||||
chrome.storage.local.set({
|
db.local.set({
|
||||||
items: result.items
|
items: result.items
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1008,13 +1008,11 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
if (js) {
|
if (js) {
|
||||||
contents += '<script>\n' + js + '\n//# sourceURL=userscript.js';
|
contents += '<script>\n' + js + '\n//# sourceURL=userscript.js';
|
||||||
} else {
|
} else {
|
||||||
|
var origin = chrome.i18n.getMessage()
|
||||||
|
? `chrome-extension://${chrome.i18n.getMessage('@@extension_id')}`
|
||||||
|
: `${location.origin}`;
|
||||||
contents +=
|
contents +=
|
||||||
'<script src="' +
|
'<script src="' + `filesystem:${origin}/temporary/script.js` + '">';
|
||||||
'filesystem:chrome-extension://' +
|
|
||||||
chrome.i18n.getMessage('@@extension_id') +
|
|
||||||
'/temporary/' +
|
|
||||||
'script.js' +
|
|
||||||
'">';
|
|
||||||
}
|
}
|
||||||
contents += '\n</script>\n</body>\n</html>';
|
contents += '\n</script>\n</body>\n</html>';
|
||||||
|
|
||||||
@@ -1089,15 +1087,14 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
// CSP from affecting it.
|
// CSP from affecting it.
|
||||||
writeFile('script.js', blobjs, function() {
|
writeFile('script.js', blobjs, function() {
|
||||||
writeFile('preview.html', blob, function() {
|
writeFile('preview.html', blob, function() {
|
||||||
const frameSrc =
|
var origin = chrome.i18n.getMessage()
|
||||||
'filesystem:chrome-extension://' +
|
? `chrome-extension://${chrome.i18n.getMessage('@@extension_id')}`
|
||||||
chrome.i18n.getMessage('@@extension_id') +
|
: `${location.origin}`;
|
||||||
'/temporary/' +
|
var src = `filesystem:${origin}/temporary/preview.html`;
|
||||||
'preview.html';
|
|
||||||
if (scope.detachedWindow) {
|
if (scope.detachedWindow) {
|
||||||
scope.detachedWindow.postMessage(frame.src, '*');
|
scope.detachedWindow.postMessage(src, '*');
|
||||||
} else {
|
} else {
|
||||||
frame.src = frameSrc;
|
frame.src = src;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -2014,7 +2011,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
) {
|
) {
|
||||||
hasSeenNotifications = true;
|
hasSeenNotifications = true;
|
||||||
notificationsBtn.classList.remove('has-new');
|
notificationsBtn.classList.remove('has-new');
|
||||||
chrome.storage.sync.set(
|
db.sync.set(
|
||||||
{
|
{
|
||||||
lastSeenVersion: version
|
lastSeenVersion: version
|
||||||
},
|
},
|
||||||
@@ -2307,7 +2304,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
$('#demo-frame').classList.remove('pointer-none');
|
$('#demo-frame').classList.remove('pointer-none');
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.storage.local.get(
|
db.local.get(
|
||||||
{
|
{
|
||||||
layoutMode: 1,
|
layoutMode: 1,
|
||||||
code: ''
|
code: ''
|
||||||
@@ -2322,7 +2319,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Get synced `preserveLastCode` setting to get back last code (or not).
|
// Get synced `preserveLastCode` setting to get back last code (or not).
|
||||||
chrome.storage.sync.get(
|
db.sync.get(
|
||||||
{
|
{
|
||||||
preserveLastCode: true,
|
preserveLastCode: true,
|
||||||
replaceNewTab: false,
|
replaceNewTab: false,
|
||||||
@@ -2349,7 +2346,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
if (result.preserveLastCode && lastCode) {
|
if (result.preserveLastCode && lastCode) {
|
||||||
unsavedEditCount = 0;
|
unsavedEditCount = 0;
|
||||||
if (lastCode.id) {
|
if (lastCode.id) {
|
||||||
chrome.storage.local.get(lastCode.id, function(itemResult) {
|
db.local.get(lastCode.id, function(itemResult) {
|
||||||
utils.log('Load item ', lastCode.id);
|
utils.log('Load item ', lastCode.id);
|
||||||
currentItem = itemResult[lastCode.id];
|
currentItem = itemResult[lastCode.id];
|
||||||
refreshEditor();
|
refreshEditor();
|
||||||
@@ -2389,7 +2386,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Check for new version notifications
|
// Check for new version notifications
|
||||||
chrome.storage.sync.get(
|
db.sync.get(
|
||||||
{
|
{
|
||||||
lastSeenVersion: ''
|
lastSeenVersion: ''
|
||||||
},
|
},
|
||||||
@@ -2401,7 +2398,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
trackEvent('ui', 'onboardModalSeen', version);
|
trackEvent('ui', 'onboardModalSeen', version);
|
||||||
document.cookie = 'onboarded=1';
|
document.cookie = 'onboarded=1';
|
||||||
}
|
}
|
||||||
chrome.storage.sync.set(
|
db.sync.set(
|
||||||
{
|
{
|
||||||
lastSeenVersion: version
|
lastSeenVersion: version
|
||||||
},
|
},
|
||||||
@@ -2409,7 +2406,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
);
|
);
|
||||||
// set some initial preferences on closing the onboard modal
|
// set some initial preferences on closing the onboard modal
|
||||||
utils.once(document, 'overlaysClosed', function() {
|
utils.once(document, 'overlaysClosed', function() {
|
||||||
chrome.storage.sync.set(
|
db.sync.set(
|
||||||
{
|
{
|
||||||
replaceNewTab: onboardShowInTabOptionBtn.classList.contains(
|
replaceNewTab: onboardShowInTabOptionBtn.classList.contains(
|
||||||
'selected'
|
'selected'
|
||||||
|
@@ -180,4 +180,6 @@
|
|||||||
log: log,
|
log: log,
|
||||||
once: once
|
once: once
|
||||||
};
|
};
|
||||||
|
|
||||||
|
window.chrome.i18n = { getMessage: () => {} };
|
||||||
})();
|
})();
|
||||||
|
Reference in New Issue
Block a user