-
Notifications
You must be signed in to change notification settings - Fork 9
/
tosdr.js
67 lines (60 loc) · 1.75 KB
/
tosdr.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
"use strict";
window.Tosdr = (function () {
var services = [];
function ajax (url, options) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
options.success(JSON.parse(xhr.responseText));
}
}
};
xhr.send();
}
function loadService (serviceName, serviceIndexData) {
ajax('http://tosdr.org/services/' + serviceName + '.json', {
success: function (service) {
if (!service.url) {
console.log(serviceName+' has no service url');
return;
}
service.urlRegExp = createRegExpForServiceUrl(service.url);
service.points = serviceIndexData.points;
service.links = serviceIndexData.links;
if (!service.tosdr) {
service.tosdr = { rated: false };
}
services.push(service);
localStorage.setItem(serviceName, JSON.stringify(service));
}
});
}
function createRegExpForServiceUrl (serviceUrl) {
if (/^http/.exec(serviceUrl)) {
return new RegExp(serviceUrl + '.*');
} else {
return new RegExp('https?://[^:/]*\\b' + serviceUrl + '.*');
}
}
ajax('http://tosdr.org/index/services.json', {
success: function (servicesIndex) {
for (var serviceName in servicesIndex) {
loadService(serviceName, servicesIndex[serviceName]);
}
}
});
function getService (url) {
var matchingServices = services.filter(function (service) {
return service.urlRegExp.exec(url);
});
return matchingServices.length > 0 ? matchingServices[0] : null;
}
return {
getServices: function () {
return services;
},
getService: getService
};
})();