From 742f96f644f95066ef1bcef07ff8560b270bf99e Mon Sep 17 00:00:00 2001 From: Thomas Wilburn Date: Sun, 12 Jan 2014 21:09:44 -0800 Subject: [PATCH] Load configs and other text via getPackageDirectoryEntry() Fixes #127 Turns out this isn't actually any faster, but it is cleaner and doesn't clutter the console. We'll keep it for now. --- js/util/text.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/js/util/text.js b/js/util/text.js index 5869fa2..73bcf1c 100644 --- a/js/util/text.js +++ b/js/util/text.js @@ -1,6 +1,7 @@ define(function() { var cache = {}; + var directory = null; return { load: function(name, parentRequire, onLoad, config) { @@ -8,12 +9,24 @@ define(function() { return onLoad(cache[name]); } - var xhr = new XMLHttpRequest(); - xhr.open("GET", name); - xhr.onload = xhr.onerror = function() { - onLoad(xhr.responseText); - } - xhr.send(); + var getFile = function() { + directory.getFile(name, {create: false}, function(entry) { + entry.file(function(file) { + var reader = new FileReader(); + reader.onloadend = function() { + cache[name] = reader.result; + onLoad(reader.result); + }; + reader.readAsText(file); + }); + }); + }; + + if (directory) return getFile(); + chrome.runtime.getPackageDirectoryEntry(function(dir) { + directory = dir; + getFile(); + }); } };