Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 714c41f

Browse files
authored
Merge pull request #33 from petrogad/data-id
Addition of Data-id selection
2 parents 7274814 + b0107ca commit 714c41f

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project is pretty fresh, but does the following already:
1515
- Shows which events are being recorded.
1616
- Copy to clipboard.
1717
- Offers configuration options.
18+
- Allows data-id configuration for element selection.
1819

1920
> Note: we only record clicks etc. on a handful of elements, see the `elements-to-bind-to.js` and `dom-events-to-record.js` files in the code-generator folder for which events. This collection will be expanded in future releases.
2021

src/code-generator/CodeGenerator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export const defaults = {
2121
headless: true,
2222
waitForNavigation: true,
2323
waitForSelectorOnClick: true,
24-
blankLinesBetweenBlocks: true
24+
blankLinesBetweenBlocks: true,
25+
dataAttribute: ''
2526
}
2627

2728
export default class CodeGenerator {

src/content-scripts/index.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@ class EventRecorder {
99
}
1010

1111
start () {
12-
const events = Object.values(eventsToRecord)
13-
if (!window.pptRecorderAddedControlListeners) {
14-
this.addAllListeners(elementsToBindTo, events)
15-
window.pptRecorderAddedControlListeners = true
16-
}
12+
chrome.storage.local.get(['options'], ({ options }) => {
13+
const {dataAttribute} = options ? options.code : {}
14+
if (dataAttribute) {
15+
this.dataAttribute = dataAttribute
16+
}
1717

18-
if (!window.document.pptRecorderAddedControlListeners && chrome.runtime && chrome.runtime.onMessage) {
19-
const boundedGetCurrentUrl = this.getCurrentUrl.bind(this)
20-
const boundedGetViewPortSize = this.getViewPortSize.bind(this)
21-
chrome.runtime.onMessage.addListener(boundedGetCurrentUrl)
22-
chrome.runtime.onMessage.addListener(boundedGetViewPortSize)
23-
window.document.pptRecorderAddedControlListeners = true
24-
}
18+
const events = Object.values(eventsToRecord)
19+
if (!window.pptRecorderAddedControlListeners) {
20+
this.addAllListeners(elementsToBindTo, events)
21+
window.pptRecorderAddedControlListeners = true
22+
}
2523

26-
const msg = { control: 'event-recorder-started' }
27-
this.sendMessage(msg)
28-
console.debug('Puppeteer Recorder in-page EventRecorder started')
24+
if (!window.document.pptRecorderAddedControlListeners && chrome.runtime && chrome.runtime.onMessage) {
25+
const boundedGetCurrentUrl = this.getCurrentUrl.bind(this)
26+
const boundedGetViewPortSize = this.getViewPortSize.bind(this)
27+
chrome.runtime.onMessage.addListener(boundedGetCurrentUrl)
28+
chrome.runtime.onMessage.addListener(boundedGetViewPortSize)
29+
window.document.pptRecorderAddedControlListeners = true
30+
}
31+
32+
const msg = { control: 'event-recorder-started' }
33+
this.sendMessage(msg)
34+
console.debug('Puppeteer Recorder in-page EventRecorder started')
35+
})
2936
}
3037

3138
addAllListeners (elements, events) {
@@ -68,8 +75,12 @@ class EventRecorder {
6875
if (this.previousEvent && this.previousEvent.timeStamp === e.timeStamp) return
6976
this.previousEvent = e
7077

78+
const selector = e.target.hasAttribute && e.target.hasAttribute(this.dataAttribute)
79+
? formatDataSelector(e.target, this.dataAttribute)
80+
: finder(e.target, { seedMinLength: 5, optimizedMinLength: 10 })
81+
7182
const msg = {
72-
selector: finder(e.target, { seedMinLength: 5, optimizedMinLength: 10 }),
83+
selector: selector,
7384
value: e.target.value,
7485
tagName: e.target.tagName,
7586
action: e.type,
@@ -99,5 +110,9 @@ function getCoordinates (evt) {
99110
return eventsWithCoordinates[evt.type] ? { x: evt.clientX, y: evt.clientY } : null
100111
}
101112

113+
function formatDataSelector (element, attribute) {
114+
return `[${attribute}=${element.getAttribute(attribute)}]`
115+
}
116+
102117
window.eventRecorder = new EventRecorder()
103118
window.eventRecorder.start()

src/options/components/App.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
</small>
99
</div>
1010
<div class="content" v-if="!loading">
11+
<div class="settings-block">
12+
<h4 class="settings-block-title">
13+
Code Recorder settings
14+
</h4>
15+
<div class="settings-block-main">
16+
<div class="settings-group">
17+
<label>
18+
<input id="options-code-dataAttribute" type="text" v-model="options.code.dataAttribute" @change="save" placeholder="Custom data-id selector">
19+
Define a data-id attribute that we'll attempt to use when selecting the elements.
20+
</label>
21+
</div>
22+
</div>
23+
</div>
1124
<div class="settings-block">
1225
<h4 class="settings-block-title">
1326
Code Generator settings
@@ -160,6 +173,16 @@
160173
display: block;
161174
}
162175
}
176+
input[type="text"] {
177+
margin-bottom: 10px;
178+
width: 100%;
179+
border: 1px solid $gray-light;
180+
padding-left: 15px;
181+
height: 38px;
182+
font-size: 14px;
183+
border-radius: 10px;
184+
-webkit-box-sizing: border-box;
185+
}
163186
}
164187
}
165188
}

0 commit comments

Comments
 (0)