|
1 | 1 | const SESSION_COOKIE_NAME = 'sessionId'; |
2 | 2 | const SESSION_COOKIE_EXPIRATION_DAYS = 7; |
3 | 3 |
|
| 4 | +/** |
| 5 | + * Escape a selector. |
| 6 | + * @param selector |
| 7 | + * @returns {string} |
| 8 | + */ |
| 9 | +function escapeSelector(selector) { |
| 10 | + return selector.replaceAll(/#(\d)/g, '#\\3$1 '); |
| 11 | +} |
| 12 | + |
4 | 13 | /** |
5 | 14 | * Generate a UUID. |
6 | 15 | * @returns {string} |
@@ -178,7 +187,7 @@ function getLoadedSections(main) { |
178 | 187 | function displayOffers(section, offers) { |
179 | 188 | offers.forEach((offer) => { |
180 | 189 | const { type, cssSelector, content } = offer; |
181 | | - const targetElement = section.querySelector(cssSelector); |
| 190 | + const targetElement = section.querySelector(escapeSelector(cssSelector)); |
182 | 191 | if (targetElement) { |
183 | 192 | switch (type) { |
184 | 193 | case 'insertAfter': |
@@ -214,7 +223,7 @@ function displayOffers(section, offers) { |
214 | 223 | * @param selector The element selector. |
215 | 224 | */ |
216 | 225 | function getSectionByElementSelector(selector) { |
217 | | - let section = document.querySelector(selector); |
| 226 | + let section = document.querySelector(escapeSelector(selector)); |
218 | 227 | while (section && !section.classList.contains('section')) { |
219 | 228 | section = section.parentNode; |
220 | 229 | } |
@@ -249,11 +258,12 @@ export default function loadOffers(client, useProxy) { |
249 | 258 | window?.measurePerformance('targeting:loading-offers'); |
250 | 259 |
|
251 | 260 | offers.forEach((offer) => { |
| 261 | + const { cssSelector } = offer; |
252 | 262 | console.debug('processing offer', offer); // eslint-disable-line no-console |
253 | | - const section = getSectionByElementSelector(offer.selector); |
| 263 | + const section = getSectionByElementSelector(cssSelector); |
254 | 264 | if (section) { |
255 | 265 | // eslint-disable-next-line no-console |
256 | | - console.debug(`hiding section for selector ${offer.selector}`, section); |
| 266 | + console.debug(`hiding section for selector ${cssSelector}`, section); |
257 | 267 | section.style.visibility = 'hidden'; |
258 | 268 | window?.createPerformanceMark( |
259 | 269 | `targeting:rendering-section:${Array.from(section.classList).join('_')}`, |
|
0 commit comments