1/** 2 * @file 3 * Injects CollectionPage schema from /api/cbis-product-list responses. 4 * 5 * Attaches globally. Acts as a no-op on pages where no Ajax request to 6 * /api/cbis-product-list is ever made (i.e. pages without a product list 7 * widget), so no page-type guard is needed. 8 */ 9 10(function ($, Drupal, once) { 11 'use strict'; 12 13 /** 14 * Intercepts cbis-product-list API responses and injects schema into <head>. 15 * 16 * app.js uses jQuery $.getJSON for the product list API call, so we 17 * intercept via $(document).ajaxSuccess() rather than wrapping window.fetch. 18 * 19 * NOTE: camping_schema module defines an identically-named behavior 20 * (cbisProductListSchema) and uses the same once() key. On camping.se 21 * (where both modules are enabled and app.js uses window.fetch instead of 22 * $.getJSON) only camping_schema's fetch-based behavior runs; once() ensures 23 * this ajaxSuccess handler is not also registered there, preventing double 24 * injection. 25 */ 26 Drupal.behaviors.cbisProductListSchema = { 27 attach: function (context, settings) { 28 once('cbis-product-list-schema', 'body', context).forEach(function () { 29 $(document).ajaxSuccess(function (event, xhr, ajaxSettings, data) { 30 if ( 31 ajaxSettings.url && 32 ajaxSettings.url.includes('/api/cbis-product-list') && 33 data && 34 data.schema 35 ) { 36 injectSchema(data.schema); 37 } 38 }); 39 }); 40 } 41 }; 42 43 /** 44 * Injects or updates the JSON-LD schema tag in <head>. 45 * 46 * @param {Object} schema - The schema.org object from the API response. 47 */ 48 function injectSchema(schema) { 49 const id = 'cbis-product-list-collection-page'; 50 51 // Overwrite url with the live page value. Keep schema.name from the API 52 // response ($node->label()) rather than document.title, which appends the 53 // site name suffix ("| Höga Kusten") and corrupts the schema name. 54 schema.url = window.location.href; 55 56 const json = JSON.stringify(schema, undefined, 2); 57 const existing = document.getElementById(id); 58 59 if (existing) { 60 existing.textContent = json; 61 } 62 else { 63 const script = document.createElement('script'); 64 script.type = 'application/ld+json'; 65 script.id = id; 66 script.textContent = json; 67 document.head.appendChild(script); 68 } 69 } 70 71})(jQuery, Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.