-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
86 lines (78 loc) · 1.77 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
window.onload = function(){
test_db_functionality = false;
if(test_db_functionality){
readSites(function(){
getEulaSectionForTag("http://testSite.com", "testTag", function(sectionText){
console.log("Database test, test section is: " + sectionText);
});
});
} else {
readSites();
}
}
function getEulaSectionForTag(siteDomain, tag, callback){
console.log("Sanity check.");
siteDomain = siteDomain.toLowerCase();
tag = tag.toLowerCase();
getEulaByDomain(siteDomain, function(eula){
if(eula == null){
callback(null);
} else {
sectionFound = null;
eula.sections.forEach(function(section){
if(section.tags.includes(tag)){
sectionFound = section;
}
});
if(sectionFound == null){
callback(null);
} else {
callback(sectionFound.text);
}
}
});
}
sites = null;
function readSites(callback){
var path = 'data/sites.json'
getJson(path, function(sitesJson){
sites = sitesJson.sites;
if(callback){
callback();
}
});
}
function getJson(path, callback){
var xhr = new XMLHttpRequest;
var fullPath = chrome.runtime.getURL(path);
xhr.open("GET", chrome.runtime.getURL(path));
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
if(xhr.responseText != null && xhr.responseText != ""){
var parsed_json = JSON.parse(xhr.responseText);
callback(parsed_json);
} else {
callback(null);
}
}
};
xhr.send();
}
function getEulaByDomain(domain, callback){
foundSite = null;
sites.forEach(function(site){
if(site.domains.includes(domain)){
foundSite = site;
}
});
if(foundSite == null){
callback(null);
} else {
eulaName = foundSite.name;
readSavedEULA(eulaName, callback);
}
}
function readSavedEULA(eulaName, callback){
var path = 'data/eulas/' + eulaName + '.json'
getJson(path, callback);
}