зеркало из https://github.com/electron/electron.git
Merge pull request #5974 from electron/chrome-storage-sync
Add chrome.storage.sync and chrome.storage.get APIs
This commit is contained in:
Коммит
c68ca16148
|
@ -64,6 +64,7 @@
|
|||
'lib/renderer/api/screen.js',
|
||||
'lib/renderer/api/web-frame.js',
|
||||
'lib/renderer/extensions/i18n.js',
|
||||
'lib/renderer/extensions/storage.js',
|
||||
],
|
||||
'js2c_sources': [
|
||||
'lib/common/asar.js',
|
||||
|
|
|
@ -183,12 +183,7 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
|
|||
onMessage: chrome.runtime.onMessage
|
||||
}
|
||||
|
||||
chrome.storage = {
|
||||
sync: {
|
||||
get () {},
|
||||
set () {}
|
||||
}
|
||||
}
|
||||
chrome.storage = require('./extensions/storage.js')
|
||||
|
||||
chrome.pageAction = {
|
||||
show () {},
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
const getStorage = () => {
|
||||
const data = window.localStorage.getItem('__chrome.storage.sync__')
|
||||
if (data != null) {
|
||||
return JSON.parse(data)
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
const setStorage = (storage) => {
|
||||
const json = JSON.stringify(storage)
|
||||
window.localStorage.setItem('__chrome.storage.sync__', json)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sync: {
|
||||
get (keys, callback) {
|
||||
const storage = getStorage()
|
||||
if (keys == null) return storage
|
||||
|
||||
let defaults = {}
|
||||
switch (typeof keys) {
|
||||
case 'string':
|
||||
keys = [keys]
|
||||
break
|
||||
case 'object':
|
||||
if (!Array.isArray(keys)) {
|
||||
defaults = keys
|
||||
keys = Object.keys(keys)
|
||||
}
|
||||
break
|
||||
}
|
||||
if (keys.length === 0) return {}
|
||||
|
||||
let items = {}
|
||||
keys.forEach(function (key) {
|
||||
var value = storage[key]
|
||||
if (value == null) value = defaults[key]
|
||||
items[key] = value
|
||||
})
|
||||
|
||||
setTimeout(function () {
|
||||
callback(items)
|
||||
})
|
||||
},
|
||||
|
||||
set (items, callback) {
|
||||
const storage = getStorage()
|
||||
|
||||
Object.keys(items).forEach(function (name) {
|
||||
storage[name] = items[name]
|
||||
})
|
||||
|
||||
setStorage(storage)
|
||||
|
||||
setTimeout(callback)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -878,6 +878,7 @@ describe('browser-window module', function () {
|
|||
assert.equal(message.runtimeId, 'foo')
|
||||
assert.equal(message.tabId, w.webContents.id)
|
||||
assert.equal(message.i18nString, 'foo - bar (baz)')
|
||||
assert.deepEqual(message.storageItems, {foo: 'bar'})
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
|
|
@ -4,13 +4,22 @@
|
|||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
<script>
|
||||
var message = JSON.stringify({
|
||||
runtimeId: chrome.runtime.id,
|
||||
tabId: chrome.devtools.inspectedWindow.tabId,
|
||||
i18nString: chrome.i18n.getMessage('foo', ['bar', 'baz'])
|
||||
function testStorage (callback) {
|
||||
chrome.storage.sync.set({foo: 'bar'}, function () {
|
||||
chrome.storage.sync.get({foo: 'baz'}, callback)
|
||||
})
|
||||
}
|
||||
|
||||
testStorage(function (items) {
|
||||
var message = JSON.stringify({
|
||||
runtimeId: chrome.runtime.id,
|
||||
tabId: chrome.devtools.inspectedWindow.tabId,
|
||||
i18nString: chrome.i18n.getMessage('foo', ['bar', 'baz']),
|
||||
storageItems: items
|
||||
})
|
||||
var sendMessage = `require('electron').ipcRenderer.send('answer', ${message})`
|
||||
window.chrome.devtools.inspectedWindow.eval(sendMessage, function () {})
|
||||
})
|
||||
var sendMessage = `require('electron').ipcRenderer.send('answer', ${message})`
|
||||
window.chrome.devtools.inspectedWindow.eval(sendMessage, function () {})
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
Загрузка…
Ссылка в новой задаче