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 -->
|
||||
<script src="utils.js"></script>
|
||||
<script src="db.js"></script>
|
||||
<script src="analytics.js"></script>
|
||||
<script src="deferred.js"></script>
|
||||
<script src="loader.js"></script>
|
||||
|
@@ -302,7 +302,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
const d = deferred();
|
||||
var obj = {};
|
||||
obj[setting] = value;
|
||||
chrome.storage.local.set(obj, d.resolve);
|
||||
db.local.set(obj, d.resolve);
|
||||
return d.promise;
|
||||
}
|
||||
|
||||
@@ -322,13 +322,13 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
});
|
||||
// Push into the items hash if its a new item being saved
|
||||
if (isNewItem) {
|
||||
chrome.storage.local.get(
|
||||
db.local.get(
|
||||
{
|
||||
items: {}
|
||||
},
|
||||
function(result) {
|
||||
result.items[currentItem.id] = true;
|
||||
chrome.storage.local.set({
|
||||
db.local.set({
|
||||
items: result.items
|
||||
});
|
||||
}
|
||||
@@ -1008,13 +1008,11 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
if (js) {
|
||||
contents += '<script>\n' + js + '\n//# sourceURL=userscript.js';
|
||||
} else {
|
||||
var origin = chrome.i18n.getMessage()
|
||||
? `chrome-extension://${chrome.i18n.getMessage('@@extension_id')}`
|
||||
: `${location.origin}`;
|
||||
contents +=
|
||||
'<script src="' +
|
||||
'filesystem:chrome-extension://' +
|
||||
chrome.i18n.getMessage('@@extension_id') +
|
||||
'/temporary/' +
|
||||
'script.js' +
|
||||
'">';
|
||||
'<script src="' + `filesystem:${origin}/temporary/script.js` + '">';
|
||||
}
|
||||
contents += '\n</script>\n</body>\n</html>';
|
||||
|
||||
@@ -1089,15 +1087,14 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
// CSP from affecting it.
|
||||
writeFile('script.js', blobjs, function() {
|
||||
writeFile('preview.html', blob, function() {
|
||||
const frameSrc =
|
||||
'filesystem:chrome-extension://' +
|
||||
chrome.i18n.getMessage('@@extension_id') +
|
||||
'/temporary/' +
|
||||
'preview.html';
|
||||
var origin = chrome.i18n.getMessage()
|
||||
? `chrome-extension://${chrome.i18n.getMessage('@@extension_id')}`
|
||||
: `${location.origin}`;
|
||||
var src = `filesystem:${origin}/temporary/preview.html`;
|
||||
if (scope.detachedWindow) {
|
||||
scope.detachedWindow.postMessage(frame.src, '*');
|
||||
scope.detachedWindow.postMessage(src, '*');
|
||||
} else {
|
||||
frame.src = frameSrc;
|
||||
frame.src = src;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2014,7 +2011,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
) {
|
||||
hasSeenNotifications = true;
|
||||
notificationsBtn.classList.remove('has-new');
|
||||
chrome.storage.sync.set(
|
||||
db.sync.set(
|
||||
{
|
||||
lastSeenVersion: version
|
||||
},
|
||||
@@ -2307,7 +2304,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
$('#demo-frame').classList.remove('pointer-none');
|
||||
});
|
||||
|
||||
chrome.storage.local.get(
|
||||
db.local.get(
|
||||
{
|
||||
layoutMode: 1,
|
||||
code: ''
|
||||
@@ -2322,7 +2319,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
);
|
||||
|
||||
// Get synced `preserveLastCode` setting to get back last code (or not).
|
||||
chrome.storage.sync.get(
|
||||
db.sync.get(
|
||||
{
|
||||
preserveLastCode: true,
|
||||
replaceNewTab: false,
|
||||
@@ -2349,7 +2346,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
if (result.preserveLastCode && lastCode) {
|
||||
unsavedEditCount = 0;
|
||||
if (lastCode.id) {
|
||||
chrome.storage.local.get(lastCode.id, function(itemResult) {
|
||||
db.local.get(lastCode.id, function(itemResult) {
|
||||
utils.log('Load item ', lastCode.id);
|
||||
currentItem = itemResult[lastCode.id];
|
||||
refreshEditor();
|
||||
@@ -2389,7 +2386,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
);
|
||||
|
||||
// Check for new version notifications
|
||||
chrome.storage.sync.get(
|
||||
db.sync.get(
|
||||
{
|
||||
lastSeenVersion: ''
|
||||
},
|
||||
@@ -2401,7 +2398,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
trackEvent('ui', 'onboardModalSeen', version);
|
||||
document.cookie = 'onboarded=1';
|
||||
}
|
||||
chrome.storage.sync.set(
|
||||
db.sync.set(
|
||||
{
|
||||
lastSeenVersion: version
|
||||
},
|
||||
@@ -2409,7 +2406,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
||||
);
|
||||
// set some initial preferences on closing the onboard modal
|
||||
utils.once(document, 'overlaysClosed', function() {
|
||||
chrome.storage.sync.set(
|
||||
db.sync.set(
|
||||
{
|
||||
replaceNewTab: onboardShowInTabOptionBtn.classList.contains(
|
||||
'selected'
|
||||
|
@@ -180,4 +180,6 @@
|
||||
log: log,
|
||||
once: once
|
||||
};
|
||||
|
||||
window.chrome.i18n = { getMessage: () => {} };
|
||||
})();
|
||||
|
Reference in New Issue
Block a user