-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent-script.coffee
More file actions
211 lines (189 loc) · 5.95 KB
/
content-script.coffee
File metadata and controls
211 lines (189 loc) · 5.95 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
200
201
202
203
204
205
206
207
208
209
210
class EventRecorder
constructor: (options) ->
@frames = []
@recording = false
start: ->
@recording = true
console.log 'Starting to record events...'
$(document).click @recordEvent.bind @
$(':input').change @recordEvent.bind @
stop: ->
@recording = false
console.log 'Stopping...'
recordEvent: (e) ->
return if not @recording
self = @
record =
url: document.location.href
type: e.type
target: self.getXPath e.target
value: if e.type is 'change' then $(e.target).val() else null
@frames.push record
console.log record
chrome.extension.sendMessage
type: 'event'
event: record
getElementByXPath: (path) ->
result = document.evaluate path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
return result.singleNodeValue
getXPath: (el) ->
segs = []
while el
if el.hasAttribute 'id'
segs.unshift "id(\"#{el.getAttribute 'id'}\")"
return segs.join '/'
else if el.hasAttribute 'class'
segs.unshift "#{el.localName.toLowerCase()}[@class=\"#{el.getAttribute 'class'}\"]"
else
sib = el.previousSibling
i = 1
while sib = sib.previousSibling
i++ if sib.localName is el.localName
segs.unshift "#{el.localName.toLowerCase()}[#{i}]"
if segs.length
return "/#{segs.join '/'}"
else
return null
el = el.parentNode
class EventPlayback
constructor: (record) ->
@events = record.events
@events.length = record.size
processURL: (url) ->
url.split(/[?#]/)[0]
compareLocation: (url1, url2) ->
if url1 is url2
true
else if (@processURL url1) is (@processURL url2)
true
else
false
getElementByXPath: (path) ->
result = document.evaluate path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
return result.singleNodeValue
playback: ->
for event in @events
continue if not @compareLocation event.url, document.location.href
console.log event
switch event.type
when 'click'
el = @getElementByXPath event.target
console.log 'clicking'
console.log el
$(el).click()
when 'change'
el = @getElementByXPath event.target
console.log 'changing'
console.log el
$(el).val event.value
null
class PageControl
constructor: ->
place: (records) ->
for record in records
do (record) =>
console.log record
$('body').append '
<div id=' + record.id + ' class="AA-record">
<div>
<button class="handle">◆</button>
<button class="playback">' + record.title + '</button>
<button class="dropdown">▾</button>
</div>
<ul>
<li><a class="rename">Rename</a></li>
<li><a class="edit">Edit</a></li>
<li><a class="delete">Delete</a></li>
</ul>
<input class="input" type="text" value="' + record.title + '" />
</div>'
console.log record.offset
record.offset ?= top: 0, left: 0
$("##{record.id}").offset record.offset
$("##{record.id} .playback").button().click ->
chrome.extension.sendMessage
type: 'action'
action: 'start-playback'
id: record.id
.next().button().click ->
menu = $(@).parent().next()
if menu.is ':visible'
menu.hide()
else
menu.show().position
my: 'left top',
at: 'left bottom',
of: @
.parent().buttonset()
.next().hide().menu()
$("##{record.id} .edit").click ->
window.open "#{chrome.extension.getURL 'editor.html'}##{record.id}", '_newtab'
$("##{record.id} .delete").click ->
$("##{record.id}").html ''
chrome.extension.sendMessage
type: 'action'
action: 'delete'
id: record.id
$("##{record.id} .rename").click ->
input = $("##{record.id} .input")
input.addClass 'editing'
input.position
my: 'left'
at: 'left'
of: @
setTimeout (-> input.focus()), 10
close = =>
input = $("##{record.id} .input")
@updateRecord record.id, title: input.val()
input.removeClass 'editing'
$("##{record.id} .playback .ui-button-text").html input.val()
$("##{record.id} .input").blur close
$("##{record.id} .input").keypress (e) ->
close() if e.keyCode is 13
$("##{record.id}").draggable
cancel: false
handle: '.handle'
stop: (event, ui) =>
console.log 'drag stopped!'
console.log event
console.log ui
@updateRecord record.id,
offset: ui.offset
updateRecord: (id, attr) ->
chrome.extension.sendMessage
type: 'data'
id : id
attr: attr
display: (msg) ->
$('.AA-msg').html "<h3>#{msg}</h3>"
clearDisplay: ->
$('.AA-msg').html ''
$('body').append '<div class="AA-msg"></div>'
er = null
ep = null
control = new PageControl
chrome.extension.onMessage.addListener (request, sender, sendResponse) ->
console.log 'Got message from extension.'
console.log request
switch request.action
when 'start'
console.log 'Action is "start"'
er = new EventRecorder
er.start()
control.display 'Recording...'
when 'stop'
console.log 'Action is "stop"'
er.stop()
control.clearDisplay()
when 'playback'
console.log 'Action is "playback"'
ep = new EventPlayback request.record
ep.playback()
control.display 'Replaying...'
when 'place'
console.log 'Action is "place"'
console.log 'content-script loaded'
chrome.extension.sendMessage type: 'ping', (response) ->
console.log 'got response from control'
console.log response.records
control.place response.records