cat.js
4.47 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
function TabManager() {
this.htmlTypeRegex = new RegExp("^text/html.*");
this.tabId2Txid = {};
this.serverMapping = {};
this.serverMappingUpdated = false;
this.updateServerMapping = function(server) {
if(!this.serverMappingUpdated) {
this.serverMappingUpdated = true;
var xhr = new XMLHttpRequest();
var mappingUrl = "http://" + server + "/cat/s/plugin/chrome?mapping=true"
xhr.open("GET", mappingUrl, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { // 4 = DONE
this.serverMapping = JSON.parse(xhr.responseText);
console.debug("got server mappinag " + this.serverMapping);
}
}
xhr.send();
}
};
this.iconClicked = function(tabId) {
var rootId = this.tabId2Txid["" + tabId].rootId;
var server = this.tabId2Txid["" + tabId].server;
server = server.split(",")[0];
var newServer = this.serverMapping[server];
if(newServer) {
server = newServer;
}
chrome.tabs.create({url: "http://" + server + "/cat/r/m/" + rootId});
};
this.inspectAndRecordCatRootId = function(tabId, headers, url) {
for (var i = 0; i < headers.length; i++) {
var name = headers[i]["name"];
var value = headers[i]["value"];
if(name === "X-CAT-ROOT-ID") {
var rootId = value;
} else if(name ==="X-CAT-SERVER") {
var server = value;
}
}
if(rootId && server) {
this.tabId2Txid["" + tabId] = {rootId: rootId, server: server, url: url};
console.debug(tabId + " recognized as cat enabled with rootId " + rootId);
this.updateServerMapping(server);
}
};
this.isHtmlResponse = function(headers) {
for (var i = 0; i < headers.length; i++) {
var name = headers[i]["name"];
var value = headers[i]["value"];
if(name == "Content-Type" && this.htmlTypeRegex.test(value)) {
return true;
}
}
return false;
};
this.headersReceived = function(tabId, headers, url) {
console.debug("header for " + tabId + " with url " + url);
for (var i = 0; i < headers.length; i++) {
var name = headers[i]["name"];
var value = headers[i]["value"];
console.debug("\t" + name + " : " + value);
}
if(tabId <= 0) {
return;
}
this.inspectAndRecordCatRootId(tabId, headers, url);
};
this.headersWillBeSent = function(tabId, headers) {
headers.push({name: "X-CAT-TRACE-MODE", value: "true"});
return {requestHeaders: headers};
};
this.tabRemoved = function(tabId) {
delete this.tabId2Txid["" + tabId];
};
this.tabUrlUpdated = function(tabId, url) {
var tabCatInfo = this.tabId2Txid["" + tabId];
if(tabCatInfo && tabCatInfo["url"] != url) {
console.debug(tabId + " url updated, reset cat info");
delete this.tabId2Txid["" + tabId];
}
};
this.tabCompleted = function(tabId) {
if(this.tabId2Txid["" + tabId]) {
this.showIcon(tabId);
}
};
this.tabReplaced = function(addedTabId, removedTabId) {
console.debug("replacing " + removedTabId + " with " + addedTabId);
if(this.tabId2Txid["" + addedTabId]) {
this.showIcon(addedTabId);
}
};
this.showIcon = function(tabId) {
console.debug("show pageAction for " + tabId);
chrome.pageAction.show(tabId);
};
}
var tabMgr = new TabManager();
var responseCallback = function(details) {
if("main_frame" == details.type){
var headers = details.responseHeaders;
var tabId = details.tabId;
var url = details.url;
tabMgr.headersReceived(tabId, headers, url);
}
};
var requestCallback = function(details) {
var headers = details.requestHeaders;
var tabId = details.tabId;
return tabMgr.headersWillBeSent(tabId, headers);
};
var filter = {urls: ["*://*/*"]};
chrome.webRequest.onHeadersReceived.addListener(responseCallback, filter, ["responseHeaders"]);
chrome.webRequest.onBeforeSendHeaders.addListener(requestCallback, filter, ["blocking", "requestHeaders"]);
chrome.pageAction.onClicked.addListener(function(tab) {
//alert(localStorage["favorite_color"]);
tabMgr.iconClicked(tab.id);
});
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
console.debug(tabId + " removed");
tabMgr.tabRemoved(tabId);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
console.debug(tabId + " updated");
for(var x in changeInfo) {
console.debug("\t" + x + " : " + changeInfo[x]);
}
var url = changeInfo.url;
if(url) {
console.debug(tabId + "'s url has changed");
tabMgr.tabUrlUpdated(tabId, url);
} else if(changeInfo["status"] === "complete") {
tabMgr.tabCompleted(tabId);
}
});
chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) {
tabMgr.tabReplaced(addedTabId, removedTabId);
});