-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.html
79 lines (62 loc) · 1.96 KB
/
background.html
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
<html><head></head>
<body>
<script src="lib/underscore-1.3.1.min.js"></script>
<script>
delete localStorage['GLOBAL_CURRENT_CONTEXT']
</script>
<script src="lib/omni.js"></script>
<script>
//map of tabid => context id
var tabmap = {};
var mostRecentTabId;
chrome.tabs.onActiveChanged.addListener(function(tabid, selectinfo){
mostRecentTabId = tabid;
});
var mostRecentLoadData;
chrome.extension.onConnect.addListener(function(port){
console.log('connection made!!!');
port.onMessage.addListener(function(msg){
if(msg.innerText && msg.title) {
console.log('setting most recent load data to', msg);
mostRecentLoadData = msg;
} else {
console.log('msg from client: ', msg);
//tabmap[mostRecentTabId] = msg;
//if(!omni.Contexts.has(msg)) throw "context doesn't exist";
localStorage['GLOBAL_CURRENT_CONTEXT'] = msg.context;
}
});
});
var oldurl = '';
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var ctxTitle = tabmap[tabId],
newurl = tab.url;
/*
console.log('---');
console.log('tabid', tabId);
console.log('active', mostRecentTabId);
console.log('ctxTitle', ctxTitle);
console.log('url', newurl);
console.log('---');
*/
if(newurl == oldurl /* || !ctxTitle */) return;
oldurl = newurl;
//var context = omni.Contexts.get(ctxTitle);
var contextName = localStorage['GLOBAL_CURRENT_CONTEXT'];
if(!contextName) return;
var context = omni.Contexts.get(contextName);
if(_.find(context.urls, function(url){ return url.url == newurl })) {
console.log('url is already tracked. returning');
return;
}
if(!mostRecentLoadData) {
console.log('most recent load data hasnt been written, dawg');
return;
}
console.log('adding url', newurl, 'to context', context.title)
context.urls.push(mostRecentLoadData)
omni.Contexts.save();
});
</script>
</body>
</html>