-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
199 lines (179 loc) · 5.46 KB
/
service-worker.js
File metadata and controls
199 lines (179 loc) · 5.46 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Graph } from "./graph.js";
// move to using idb promised for graph persistence, then remove anything that isn't directly related to responding to events
let graph = new Graph();
browser.storage.local.get("graph").then(
(data) => {
console.log("stored data", data.graph);
},
(err) => {
console.log(err);
}
);
let nids = {};
let rels = {};
let activeTab = "";
let openedBy = {};
let tabURL = {};
let tabData = {};
function addURL(url) {
if (url in nids) {
return nids[url];
} else {
return (nids[url] = graph.addNode("URL", { name: url, url: url }));
}
}
function addLink(source, target, type, props) {
return (rels[target] = graph.addRel(`${type}`, source, target, props));
}
function download(string, file, mime = "text/json") {
let data = `data:${mime};charset=utf-8,` + encodeURIComponent(string);
let anchor = document.createElement("a");
anchor.setAttribute("href", data);
anchor.setAttribute("download", file);
document.body.appendChild(anchor); // required for firefox
anchor.click();
anchor.remove();
/* Backup download method, requires download api permission
window.URL = window.webkitURL || window.URL;
file = new Blob([string],{type:'text/json'});
browser.downloads.download({
url : window.URL.createObjectURL(file),
filename : 'test.graph.json',
conflictAction : 'uniquify'
});*/
}
//Shows tab count badge on extension button
function updateCount(tabId, isOnRemoved) {
browser.tabs.query({}).then((tabs) => {
let length = tabs.length;
// onRemoved fires too early and the count is one too many.
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1396758
if (
isOnRemoved &&
tabId &&
tabs
.map((t) => {
return t.id;
})
.includes(tabId)
) {
length--;
}
browser.browserAction.setBadgeText({ text: length.toString() });
if (length > 2) {
browser.browserAction.setBadgeBackgroundColor({ color: "green" });
} else {
browser.browserAction.setBadgeBackgroundColor({ color: "red" });
}
});
}
updateCount();
//Receiving commands from other scripts
browser.runtime.onMessage.addListener((msg, sender) => {
switch (msg.cmd) {
case "download":
console.log("trying to DL", graph, graph.stringify(null, 2));
download(graph.stringify(null, 2), `tabs ${Date.now()}.graph.json`);
break;
case "clear":
graph = new Graph();
browser.storage.local.clear().then(
() => {
console.log("OK");
},
(e) => {
console.log(e);
}
);
nids = {};
tabData = {};
break;
case "click":
const source = addURL(msg.source);
const target = addURL(msg.target);
addLink(source, target, msg.type, { which: "click on page" });
tabURL[sender.tab.id] = target;
break;
case "change tab": //currently changes to tab#3, but in the future, use this to change to arbitrary tabID
browser.tabs.update(3, { active: true });
break;
case "log":
console.log("Logged event:", msg.event);
default:
console.log("Default case used for (msg) in background.js", msg);
}
});
browser.tabs.onRemoved.addListener((tabId) => {
updateCount(tabId, true);
});
browser.tabs.onCreated.addListener(async (e) => {
updateCount(e, false);
console.log("tabs.onCreated", e);
/*console.log("New tab created, (e)", e);
if ("openerTabId" in e) {
const openerTab = await browser.tabs.get(e.openerTabId);
const openerURL = openerTab.url;
openedBy[e.id] = { url: openerURL, id: e.openerTabId };
} else {
addURL(e.url);
}
tabURL[e.id] = e.url;
*/
});
browser.tabs.onActivated.addListener((activeInfo) => {
console.log("tabs.onActivated", activeInfo);
console.log({ activeInfo });
});
browser.tabs.onUpdated.addListener((tabId, changeInfo, tabInfo) => {
console.log("tabs.onUpdated (changeInfo, tabInfo)", changeInfo, tabInfo);
if ("url" in changeInfo) {
addURL(changeInfo.url);
browser.storage.local.set({
graph: graph,
});
}
//Tab is starting to load something else after last complete status
/*
if (
tabData[tabId].status === "complete" &&
"status" in changeInfo &&
changeInfo.status === "loading"
) {
const nid = addURL(tabData[tabId].url);
graph.nodes[nid].props["foo"] = "bar";
tabData[tabId] = tabInfo;
} else {
tabData[tabId] = tabInfo;
}
*/
/*
if ("status" in changeInfo && changeInfo.status === "complete") {
const target = addURL(tabInfo.url);
if (tabId in openedBy) {
//new tab opened by other tab
const source = addURL(openedBy[tabId].url);
addLink(source, target, "new tab");
delete openedBy[tabId];
} else {
//new tab opened without opener, or URL changed in existing tab
const source = addURL(tabURL[tabId]);
console.log("I think im adding source", source);
addLink(tabId, target, "same tab");
tabURL[tabId] = target;
}
}
*/
});
browser.webNavigation.onCommitted.addListener((event) => {
console.log(
"webNavigation.onCommitted:",
event /*.transitionType, event.transitionQualifiers*/
);
});
browser.webNavigation.onCompleted.addListener((event) => {
console.log(
"webNavigation.onCompleted:",
event /*.transitionType, event.transitionQualifiers*/
);
});
//TODO, make all event listeners here modify the graph in some way, assuming this will be non-persistent soon. Persistent : false will be added to the background in manaifest once this is done.