-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorder.js
More file actions
89 lines (78 loc) · 2.62 KB
/
recorder.js
File metadata and controls
89 lines (78 loc) · 2.62 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
const cleanupMessage = 'cleanup'
function registerEventListeners() {
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request['message'] === cleanupMessage) {
console.log('Got an unregister event , clearing up ....')
// reload the window to remove the content script
window.location.reload()
}
})
}
registerEventListeners()
function buildAndSendMessage(actionContext) {
let locationStrategyList = buildElementLocatorPayload(event)
let payload = {}
payload.actionContext = actionContext
payload.locationStrategyList = locationStrategyList
payload.pageUrl = window.location.href
console.log('The payload is ' + JSON.stringify(payload))
chrome.runtime.sendMessage({
type: "notification", options: {
message: payload
}
});
}
window.addEventListener('click', function (event) {
let actionContext = { action: 'CLICK' }
buildAndSendMessage(actionContext)
})
window.addEventListener('keypress', function (event) {
let actionContext = { action: 'TYPE', value: event.key }
buildAndSendMessage(actionContext)
})
window.addEventListener('mouse', function (event) {
// TODO this needs to be updated
broadcastEvent(JSON.stringify(buildElementLocatorPayload(event)))
})
function buildElementLocatorPayload(event) {
return [
{ method: 'id', locator: getElementAttribute(event.target, 'ID') },
{ method: 'name', locator: getElementAttribute(event.target, 'NAME') },
{ method: 'xpath', locator: getElementAttribute(event.target, 'XPATH') }
]
}
function getElementXpath(target) {
var elementPath
if (target === document.body)
return '//' + target.tagName.toLowerCase() + ''
var ix = 0
var siblings = target.parentNode.childNodes
for (var i = 0; i < siblings.length; i++) {
var sibling = siblings[i]
if (sibling === target) {
elementPath = getElementXpath(target.parentNode) + '/' + target.tagName + '[' + (ix + 1) + ']'
return elementPath.toLowerCase()
}
if (sibling.nodeType === 1 && sibling.tagName === target.tagName)
ix++
}
}
function getElementAttribute(target, attributeName) {
var attr
switch (attributeName) {
case 'XPATH':
attr = getElementXpath(target)
break
case 'ID':
attr = target.id
break
case 'NAME':
attr = target.name
break
}
if (attr == null || attr === undefined) {
attr = ''
}
return attr
}