From 650935a6d3fa4bb0174a9fb8038ad01327323476 Mon Sep 17 00:00:00 2001 From: Filipe Oliveira Date: Wed, 4 Sep 2019 10:13:00 -0300 Subject: [PATCH] Search data attribute in parent elements. --- src/content-scripts/index.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/content-scripts/index.js b/src/content-scripts/index.js index 609c2ff..b136997 100644 --- a/src/content-scripts/index.js +++ b/src/content-scripts/index.js @@ -84,12 +84,28 @@ class EventRecorder { } } + getDataAttributeContainer (element) { + if (!element) return + + if (element.attributes[this.dataAttribute]) { + return element + } + + if (element.parentElement) { + return this.getDataAttributeContainer(element.parentElement) + } + + return null + } + recordEvent (e) { if (this.previousEvent && this.previousEvent.timeStamp === e.timeStamp) return this.previousEvent = e - const selector = e.target.hasAttribute && e.target.hasAttribute(this.dataAttribute) - ? formatDataSelector(e.target, this.dataAttribute) + const dataAttributeContainer = this.getDataAttributeContainer(e.target) + + const selector = dataAttributeContainer + ? formatDataSelector(dataAttributeContainer, this.dataAttribute) : finder(e.target, { seedMinLength: 5, optimizedMinLength: 10 }) const msg = {