Skip to content

Commit 5308560

Browse files
committed
Directory indexing for creating search database
Fixes #70 This will ease the process of updating search entries because now we don't have to maintain a hard-coded database and a simple update in the directory will be enough.
1 parent 46f30aa commit 5308560

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

code/js/metadata.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2018 The OpenGenus Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
var obj = {};
6+
7+
function readDirectory(dirEntry) {
8+
var dirReader = dirEntry.createReader();
9+
dirReader.readEntries(function(entries) {
10+
for(var i = 0; i < entries.length; i++) {
11+
var entry = entries[i];
12+
if (entry.isDirectory){
13+
readDirectory(entry);
14+
}
15+
else if (entry.isFile) {
16+
var filename = entry.name;
17+
var filepath = entry.fullPath.split("/").slice(2,-1).join("/");
18+
if (!obj[filepath]) {
19+
obj[filepath] = [];
20+
}
21+
obj[filepath].push(filename);
22+
}
23+
}
24+
localStorage["metadata"] = JSON.stringify(obj);
25+
});
26+
}
27+
28+
function populate () {
29+
chrome.runtime.getPackageDirectoryEntry(function(storageRootEntry) {
30+
if(storageRootEntry.isDirectory) {
31+
var dirReader = storageRootEntry.createReader();
32+
33+
var readEntries = function() {
34+
dirReader.readEntries(function(entries) {
35+
for(var i = 0; i < entries.length; i++) {
36+
var entry = entries[i];
37+
if (entry.name === "code") {
38+
readDirectory(entry);
39+
break;
40+
}
41+
}
42+
43+
if (!entries.length)
44+
readEntries();
45+
});
46+
};
47+
48+
readEntries();
49+
}
50+
});
51+
}
52+
53+
if (!localStorage["metadata"]) {
54+
populate();
55+
}

code/js/popup.js

Lines changed: 3 additions & 8 deletions
Large diffs are not rendered by default.

code/popup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
3+
<script src="js/metadata.js"></script>
44
<script src="js/jquery.js"></script>
55
<script src="js/popper.min.js"></script>
66
<script src="js/bootstrap.min.js"></script>

0 commit comments

Comments
 (0)