-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start adding the beaconlrc class * finalize the beacon manager class * temporarily fix tests * Return only hash as payload * Remove duplicate payload * Only add child element below the fold threshold and parent elements only below the fold * use config threshold instead of static value * Revert "use config threshold instead of static value" This reverts commit b4f633b. * use logger class and comment the xpath for now * Replace static values with config * start doing as grooming * get only elements that have hash * Added test for lrc * Best practices * Additional best practices * start considering viewport on element distance calculation * adjust imports and the log message * change the structure of test to handle more cases and use sinon for mocking * test * test 2 * fix tests attempt 1 * add more tests * add tests for run method * add more tests * remove not used argument --------- Co-authored-by: Michael Lee <[email protected]> Co-authored-by: Mathieu Lamiot <[email protected]>
- Loading branch information
1 parent
ba590ed
commit 8e25bbf
Showing
6 changed files
with
412 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
'use strict'; | ||
|
||
import BeaconUtils from "./Utils.js"; | ||
|
||
class BeaconLrc { | ||
constructor(config, logger) { | ||
this.config = config; | ||
this.logger = logger; | ||
this.lazyRenderElements = []; | ||
} | ||
|
||
async run() { | ||
try { | ||
const elementsInView = this._getLazyRenderElements(); | ||
if (elementsInView) { | ||
this._processElements(elementsInView); | ||
} | ||
} catch (err) { | ||
this.errorCode = 'script_error'; | ||
this.logger.logMessage('Script Error: ' + err); | ||
} | ||
} | ||
|
||
_getLazyRenderElements() { | ||
const elements = document.querySelectorAll('[data-rocket-location-hash]'); | ||
|
||
if (elements.length <= 0) { | ||
return []; | ||
} | ||
|
||
const validElements = Array.from(elements).filter(element => !this._skipElement(element)); | ||
|
||
return validElements.map(element => ({ | ||
element: element, | ||
depth: this._getElementDepth(element), | ||
distance: this._getElementDistance(element), | ||
hash: this._getLocationHash(element) | ||
})); | ||
} | ||
|
||
_getElementDepth(element) { | ||
let depth = 0; | ||
let parent = element.parentElement; | ||
while (parent) { | ||
depth++; | ||
parent = parent.parentElement; | ||
} | ||
return depth; | ||
} | ||
|
||
_getElementDistance(element) { | ||
const rect = element.getBoundingClientRect(); | ||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop; | ||
return Math.max(0, rect.top + scrollTop - BeaconUtils.getScreenHeight()); | ||
} | ||
|
||
_skipElement(element) { | ||
const skipStrings = this.config.skipStrings || ['memex']; | ||
if (!element || !element.id) return false; | ||
return skipStrings.some(str => element.id.toLowerCase().includes(str.toLowerCase())); | ||
} | ||
|
||
_shouldSkipElement(element, exclusions) { | ||
if (!element) return false; | ||
for (let i = 0; i < exclusions.length; i++) { | ||
const [attribute, pattern] = exclusions[i]; | ||
const attributeValue = element.getAttribute(attribute); | ||
if (attributeValue && new RegExp(pattern, 'i').test(attributeValue)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
_processElements(elements) { | ||
elements.forEach(({ element, depth, distance, hash }) => { | ||
if (this._shouldSkipElement(element, this.config.exclusions || [])) { | ||
return; | ||
} | ||
|
||
if ( 'No hash detected' === hash ) { | ||
return; | ||
} | ||
|
||
const can_push_hash = element.parentElement && this._getElementDistance(element.parentElement) < this.config.lrc_threshold && distance >= this.config.lrc_threshold; | ||
|
||
const color = can_push_hash ? "green" : distance === 0 ? "red" : ""; | ||
this.logger.logColoredMessage( `${'\t'.repeat(depth)}${element.tagName} (Depth: ${depth}, Distance from viewport bottom: ${distance}px)`, color ); | ||
|
||
//const xpath = this._getXPath(element); | ||
//console.log(`%c${'\t'.repeat(depth)}Xpath: ${xpath}`, style); | ||
|
||
this.logger.logColoredMessage(`${'\t'.repeat(depth)}Location hash: ${hash}`, color); | ||
|
||
this.logger.logColoredMessage(`${'\t'.repeat(depth)}Dimensions Client Height: ${element.clientHeight}`, color); | ||
|
||
if (can_push_hash) { | ||
this.lazyRenderElements.push(hash); // Push the hash | ||
this.logger.logMessage(`Element pushed with hash: ${hash}`); | ||
} | ||
}); | ||
} | ||
|
||
_getXPath(element) { | ||
if (element && element.id !== "") { | ||
return `//*[@id="${element.id}"]`; | ||
} | ||
|
||
return this._getElementXPath(element); | ||
} | ||
|
||
_getElementXPath(element) { | ||
if (element === document.body) { | ||
return '/html/body'; | ||
} | ||
const position = this._getElementPosition(element); | ||
return `${this._getElementXPath(element.parentNode)}/${element.nodeName.toLowerCase()}[${position}]`; | ||
} | ||
|
||
_getElementPosition(element) { | ||
let pos = 1; | ||
let sibling = element.previousElementSibling; | ||
while (sibling) { | ||
if (sibling.nodeName === element.nodeName) { | ||
pos++; | ||
} | ||
sibling = sibling.previousElementSibling; | ||
} | ||
return pos; | ||
} | ||
|
||
_getLocationHash(element) { | ||
return element.hasAttribute('data-rocket-location-hash') | ||
? element.getAttribute('data-rocket-location-hash') | ||
: 'No hash detected'; | ||
} | ||
|
||
getResults() { | ||
return this.lazyRenderElements; | ||
} | ||
} | ||
|
||
export default BeaconLrc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.