Skip to content

Commit

Permalink
Add slice start/end to query generation parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed May 9, 2024
1 parent 14191e6 commit 86ff5ca
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { consentShadowRoot, getCmpBoxConsent, getConsentCdnSetup, getContentPass
import { PartialSite, Sites } from './types.js'

const QUOTES = /["„].*["„]/
const START_SLICE = 2
const END_SLICE = 15

const extractQuery = (node: HTMLElement, quoted = true) => createQuery(node.innerText, quoted)
const createQuery = (text: string, quoted = true) => {
let query = text.split(' ').slice(4, 17).join(' ').replace('"', '')
const extractQuery = (node: HTMLElement, quoted = true, startSlice = START_SLICE, endSlice = END_SLICE) => createQuery(node.innerText, quoted, startSlice, endSlice)
const createQuery = (text: string, quoted = true, startSlice = START_SLICE, endSlice = END_SLICE) => {
let query = text.split(' ').slice(startSlice, endSlice).join(' ').replace('"', '')
// remove some special chars
query = query.replace(/[!:?;'/()]/g, ' ').replace(/(((?<!\d)[,.])|([,.](?!\d)))/g, ' ').replace(/ {1,}/g, ' ')
// remove non-leading/trailing quotes
Expand All @@ -18,15 +20,15 @@ const createQuery = (text: string, quoted = true) => {
}
return queryParts.join(' ')
}
const makeQueryFunc = (selector: string|string[], quoted = true) => {
const makeQueryFunc = (selector: string|string[], quoted = true, startSlice = START_SLICE, endSlice = END_SLICE) => {
if (!Array.isArray(selector)) {
selector = [selector]
}
return (node) => {
for (const sel of selector) {
const el = node.querySelector(sel)
if (el) {
return extractQuery(el, quoted)
return extractQuery(el, quoted, startSlice, endSlice)
}
}
}
Expand Down

0 comments on commit 86ff5ca

Please sign in to comment.