forked from stsquad/emacs_chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlcomms.js
174 lines (149 loc) · 5 KB
/
xmlcomms.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
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
/*
* xmlcomms.js
*
* This handles making XMLHttp calls to the "Edit Server"
*
* As no other parts of the extension can make xhr requests this
* is essentially the gatekeeper to all contact with the Edit
* Server.
*
* This file is part of emacs_chrome (http://github.com/stsquad/emacs_chrome)
* and licensed under the GPLv3. See the COPYING file for details
*/
// Get the base URL from which we make all requests to the server..
function getEditUrl()
{
var port = localStorage["edit_server_port"];
if (!port) {
port = 9292;
}
return "http://127.0.0.1:" + port + "/";
}
/*
* Give some feedback to the user via the icon/hover text.
*/
function updateUserFeedback(string, redIcon)
{
console.log("updateUserFeedback: "+string);
chrome.browserAction.setTitle({title:string});
if (redIcon) {
chrome.browserAction.setIcon({path:"emacs23-16x16-red.png"});
} else {
chrome.browserAction.setIcon({path:"emacs23-16x16.png"});
}
}
// Initial message
updateUserFeedback("Awaiting edit request", false);
// Called when the user clicks on the browser action.
//
// When clicked we send a message to the current active tab's
// content script. It will then use heuristics to decide which text
// area to spawn an edit request for.
chrome.browserAction.onClicked.addListener(function(tab) {
var find_msg = {
msg: "find_edit"
};
var tab_port = chrome.tabs.connect(tab.id);
tab_port.postMessage(find_msg);
updateUserFeedback("sent request to content script", false);
});
// Handle and edit request coming from the content page script
//
// Package up the text to be edited and send it to the edit server
function handleContentMessages(msg, tab_port)
{
console.log("handleContentMessages called:"+JSON.stringify(msg));
var cmd = msg.msg;
var id = msg.id;
var text = msg.text;
var xhr = new XMLHttpRequest();
var url = getEditUrl() + cmd;
console.log(" page URL:"+tab_port.tab.url);
console.log(" tab_port:"+tab_port.portId_);
console.log(" request URL:"+url);
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
console.log("State change:"+ xhr.readyState + " status:"+xhr.status);
// readyState 4=HTTP response complete
if(xhr.readyState == 4) {
if (xhr.status == 200) {
var update_msg = {
msg: "update",
text: xhr.responseText,
id: id
};
updateUserFeedback("Last Edit request a success", false);
tab_port.postMessage(update_msg);
} else if (xhr.status == 0) {
// Is the edit server actually running?
updateUserFeedback("Error: is edit server running?", true);
} else {
updateUserFeedback("Un-handled response: "+xhr.status, true);
}
}
}
// reset the display before sending request..
updateUserFeedback("Edit request sent", false);
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("x-url", tab_port.tab.url);
xhr.setRequestHeader("x-id", id);
xhr.send(text);
}
// Handle and edit request coming from the content page script
//
// Package up the text to be edited and send it to the edit server
function handleTestMessages(msg, tab_port)
{
var url = getEditUrl() + "status";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
console.log("State change:"+ xhr.readyState + " status:"+xhr.status);
// readyState 4=HTTP response complete
if(xhr.readyState == 4) {
if (xhr.status == 200) {
tab_port.postMessage({msg: "test_result", text: xhr.responseText});
} else if (xhr.status == 0) {
tab_port.postMessage({msg: "test_result", text: "Edit Server Test failed: is it running?"});
} else {
tab_port.postMessage({msg: "test_result", text: "Un-handled response: "+xhr.status});
}
}
}
xhr.send();
}
// Handle config request messages, the textarea.js content script being in it's own
// isolated sandbox has to be fed all this via the IPC mechanisms
function getBooleanConfig(config) {
return typeof localStorage[config] == "undefined" ? false : localStorage[config] == "true";
}
function handleConfigMessages(msg, tab_port)
{
var config_msg = {
msg: "config",
enable_dblclick: getBooleanConfig("enable_dblclick"),
enable_keys: getBooleanConfig("enable_keys")
};
tab_port.postMessage(config_msg);
}
/*
Handle all in-coming messages to the extension.
As other parts of the extension cannot trigger XHR requests they all
send message to the main part of the extension to service these requests.
*/
function localMessageHandler(port)
{
port.onMessage.addListener(function(msg, port) {
if (msg.msg == "config") {
handleConfigMessages(msg, port);
} else if (msg.msg == "edit") {
handleContentMessages(msg, port);
} else if (msg.msg == "test") {
handleTestMessages(msg, port);
} else if (msg.msg == "error") {
updateUserFeedback(msg.text, true);
}
});
}
// Hook up whenever someone connects to the extension comms port
chrome.extension.onConnect.addListener(localMessageHandler);