Skip to content

Instantly share code, notes, and snippets.

@esfand
Forked from chrahunt/background.js
Last active July 12, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esfand/dc7291d56e64a66427f11bec6a28a3f8 to your computer and use it in GitHub Desktop.
Save esfand/dc7291d56e64a66427f11bec6a28a3f8 to your computer and use it in GitHub Desktop.
Chrome extension for testing chrome.storage.local.
(function(window, document, undefined) {
var Storage = {};
window.Storage = Storage;
Storage.tracking = true;
Storage.track = function() {
Storage.tracking = !Storage.tracking;
if (Storage.tracking) {
console.log("Tracking storage performance.");
} else {
console.log("Not tracking storage performance.");
}
}
Storage.KB = 1024;
Storage.MB = 1024 * 1024;
Storage.GB = 1024 * 1024 * 1024;
Storage.showTotalBytes = function() {
function callback(bytes) {
console.log("Total bytes in use: " + bytes);
}
if (arguments.length == 0) {
return chrome.storage.local.getBytesInUse(null, callback);
} else {
var ary = arguments.slice();
ary.push(callback);
return chrome.storage.local.getBytesInUse.apply(null, ary);
}
};
// Print entry list to console.
Storage.getEntryList = function(callback) {
if (typeof callback == 'undefined') {
callback = function(ids) {
console.log(ids);
}
}
chrome.storage.local.get("index:ids", function(items) {
if (chrome.runtime.lastError) {
console.log("Error retrieving index: " + chrome.runtime.lastError);
return;
}
var ids = items["index:ids"];
callback(ids);
});
};
Storage.getEntry = function(id) {
var start = performance.now();
chrome.storage.local.get(id, function(items) {
if (chrome.runtime.lastError) {
console.log("Error retrieving entry: " + chrome.runtime.lastError);
return;
}
if (!items[id]) {
console.log("Entry not found.");
}
var end = performance.now();
console.log("Entry retrieved in " + (end - start) + " ms.");
});
}
// Store a value with the specified key, save the key to the index.
Storage.store = function(key, value) {
var storeKey = "data:" + key;
chrome.storage.local.get("index:ids", function(items) {
if (chrome.runtime.lastError) {
console.log("Error retrieving index: " + chrome.runtime.lastError);
return;
}
var ids = items["index:ids"];
ids.push(storeKey);
var store = {};
var start = performance.now();
store["index:ids"] = ids;
store[storeKey] = value;
chrome.storage.local.set(store, function() {
if (chrome.runtime.lastError) {
console.log("Error Storing: " + chrome.runtime.lastError);
}
var end = performance.now();
console.log("Value stored in " + (end - start) + " ms.");
});
});
};
// Get a string with the specified number of bytes.
Storage.getData = function(bytes) {
var chars = [];
for (var i = 0; i < bytes; i++) {
chars.push('A');
}
return chars.join('');
};
// Create and add an arbitrary entry to the local storage.
Storage.addEntry = function(bytes) {
var id = Storage._guid();
var data = Storage.getData(bytes);
Storage.store(id, data);
Storage.updateSizes();
};
Storage.getSizes = function(callback) {
if (typeof callback == 'undefined') {
callback = function(info) {
console.log(info);
}
}
chrome.storage.local.get("index:size", function(items) {
var sizes = items["index:size"];
callback(sizes);
})
};
Storage.updateSizes = function() {
function waitUntilCondition(cond, fn) {
if (cond()) {
fn();
} else {
setTimeout(function() {
waitUntilCondition(cond, fn);
}, 20);
}
}
Storage.getEntryList(function(ids) {
var error = false;
var checked = {};
ids.forEach(function(id) {
checked[id] = false;
});
var sizes;
chrome.storage.local.get("index:size", function(items) {
if (chrome.runtime.lastError) {
console.log("Error: " + chrome.runtime.lastError);
error = true;
return;
}
sizes = items["index:size"];
ids.forEach(function(id) {
if (!sizes.hasOwnProperty(id)) {
chrome.storage.local.getBytesInUse(id, function(size) {
sizes[id] = size;
checked[id] = true;
});
} else {
checked[id] = true;
}
});
});
waitUntilCondition(function() {
return ids.every(function(id) {return checked[id];});
}, function() {
chrome.storage.local.set({
"index:size": sizes
});
});
});
};
Storage._guid = function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
chrome.runtime.onInstalled.addListener(updateFunction);
function updateFunction(details) {
var previousVersion = details.previousVersion;
var version = chrome.runtime.getManifest().version;
// Get/set settings.
chrome.storage.local.get(["index:ids", "index:size"], function(result) {
if (!result["index:ids"]) {
chrome.storage.local.set({
"index:ids": []
});
}
if (!result["index:size"]) {
chrome.storage.local.set({
"index:size": {}
});
}
});
}
})(window, document);
{
// Required
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0.1",
// Recommended
"description": "Extension for testing limits of chrome storage.",
// Optional
"author": "Chris Hunt",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"storage",
"unlimitedStorage"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment