-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
53 lines (47 loc) · 1.35 KB
/
background.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
// storing 'loaded' state of scripts per tab
var loadedScripts = {};
// storing a database of all available scripts
var scriptsDB = new CDNJSScripts();
scriptsDB.fetch();
function getLoadedScripts(context) {
if (context.tabId) {
if (typeof loadedScripts[context.tabId] == "undefined") {
loadedScripts[context.tabId] = {};
}
return loadedScripts[context.tabId];
}
throw "Unable to get scripts without a tab id";
}
var API = {
getLoaded: function(request, sendResponse) {
sendResponse(getLoadedScripts(request));
},
setLoaded: function(request) {
var loaded = getLoadedScripts(request);
if (request.script.loaded) {
loaded[request.script.id] = true;
} else {
delete loaded[request.script.id];
}
},
getScripts: function(request, sendResponse) {
sendResponse(scriptsDB.toJSON());
},
addScript: function(request) {
scriptsDB.add(new Script(request.script));
}
};
// reset tabs that get reloaded
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status && changeInfo.status == "loading") {
loadedScripts[tabId] = {};
}
});
// process API calls
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (typeof API[request.action] == 'function') {
API[request.action].call(API, request, sendResponse);
} else {
throw "Unable to process request " + request.action;
}
});