From 136727afc459f2395767f08aadd73034794f6553 Mon Sep 17 00:00:00 2001 From: James Champ Date: Thu, 21 Mar 2024 14:13:59 -0700 Subject: [PATCH] Avoid `JSON.parse` errors --- .../openlibrary/js/carousel/Carousel.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/openlibrary/plugins/openlibrary/js/carousel/Carousel.js b/openlibrary/plugins/openlibrary/js/carousel/Carousel.js index 8955b1f66e3..6cd34a8843f 100644 --- a/openlibrary/plugins/openlibrary/js/carousel/Carousel.js +++ b/openlibrary/plugins/openlibrary/js/carousel/Carousel.js @@ -42,14 +42,18 @@ export class Carousel { this.$container = $container; //This loads in i18n strings from a hidden input element, generated in the books/custom_carousel.html template. - this.i18n = JSON.parse($('input[name="carousel-i18n-strings"]').attr('value')); - this.availabilityStatuses = { - open: {cls: 'cta-btn--available', cta: this.i18n['open']}, - borrow_available: {cls: 'cta-btn--available', cta: this.i18n['borrow_available']}, - borrow_unavailable: {cls: 'cta-btn--unavailable', cta: this.i18n['borrow_unavailable']}, - error: {cls: 'cta-btn--missing', cta: this.i18n['error']}, - // private: {cls: 'cta-btn--available', cta: 'Preview'} - }; + const i18nInput = document.querySelector('input[name="carousel-i18n-strings"]') + if (i18nInput) { + this.i18n = JSON.parse(i18nInput.value); + + this.availabilityStatuses = { + open: {cls: 'cta-btn--available', cta: this.i18n['open']}, + borrow_available: {cls: 'cta-btn--available', cta: this.i18n['borrow_available']}, + borrow_unavailable: {cls: 'cta-btn--unavailable', cta: this.i18n['borrow_unavailable']}, + error: {cls: 'cta-btn--missing', cta: this.i18n['error']}, + // private: {cls: 'cta-btn--available', cta: 'Preview'} + }; + } } get slick() {