forked from esamattis/TextareaConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
94 lines (94 loc) · 2.46 KB
/
content.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
(function() {
var port, textAreas;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
(function() {
var siteId, timeStamp;
timeStamp = new Date().getTime();
siteId = function() {
return location.href.replace(/[^a-zA-Z0-9_\-]/g, "") + "-" + timeStamp;
};
$.fn.uuid = function() {
var e, uuid;
e = $(this.get(0));
uuid = e.data("uuid");
if (uuid) {
return uuid;
} else {
$.fn.uuid.counter += 1;
uuid = siteId() + "-" + $.fn.uuid.counter;
e.data("uuid", uuid);
return uuid;
}
};
$.fn.uuid.counter = 0;
$.fn.editInExternalEditor = function(port) {
var sendToEditor, that;
that = $(this);
if (that.data("server")) {
return;
}
that.data("server", true);
sendToEditor = function(spawn) {
if (spawn == null) {
spawn = false;
}
return port.postMessage({
textarea: that.val(),
uuid: that.uuid(),
spawn: spawn,
action: "open"
});
};
return sendToEditor(true);
};
return $.fn.flashBg = function() {
this.addClass("textareaconnect-updated");
return setTimeout(__bind(function() {
return this.removeClass("textareaconnect-updated");
}, this), 100);
};
})();
textAreas = {};
port = chrome.extension.connect({
name: "textareapipe"
});
port.onMessage.addListener(function(obj) {
var textarea;
textarea = textAreas[obj.uuid];
if (obj.textarea === textarea.val()) {
return;
}
textarea.val(obj.textarea);
return textarea.flashBg();
});
chrome.extension.onRequest.addListener(function(req, sender) {
var realUrl, textarea;
if (req.action === "edittextarea") {
realUrl = req.onClickData.frameUrl || req.onClickData.pageUrl;
if (realUrl !== window.location.href) {
return;
}
textarea = $(document.activeElement);
textAreas[textarea.uuid()] = textarea;
return textarea.editInExternalEditor(port);
}
});
$(window).unload(function() {
var key, ta, uuids;
uuids = (function() {
var _results;
_results = [];
for (key in textAreas) {
ta = textAreas[key];
_results.push(ta.uuid());
}
return _results;
})();
if (uuids.length > 0) {
return port.postMessage({
action: "delete",
uuids: uuids
});
}
});
}).call(this);