1;(function () { 2 let baf_container = "ac-container" // parent container class. 3 4 if (document.getElementsByClassName(baf_container).length > 0) { 5 let logOutput = false // Output schema to console. For Dev. 6 7 let questionClass = "baf_schema" // Question Class. 8 let answerClass = "baf_content" // Answer Class. 9 10 // Build Data 11 let questions = Array.from(document.getElementsByClassName(questionClass)).map(function (e) { 12 return e.textContent 13 }) 14 15 let answers = Array.from(document.getElementsByClassName(answerClass)).map(function (e) { 16 return e.textContent 17 }) 18 19 if (questions.length && answers.length) { 20 let data = { 21 "@context": "https://schema.org", 22 "@type": "FAQPage", 23 mainEntity: [], 24 } 25 26 buildItem = (q, a) => { 27 // Ref: https://developers.google.com/search/docs/advanced/structured-data/faqpage 28 let item = { 29 "@type": "Question", 30 name: null, 31 acceptedAnswer: { 32 "@type": "Answer", 33 text: null, 34 }, 35 } 36 37 item["name"] = q
38 item["acceptedAnswer"]["text"] = a 39 40 return item 41 } 42 43 data["mainEntity"] = questions.map(function (q, i) { 44 // Here we go for the extra checking. 45 // if any 46 if (q.trim().length != 0 && answers[i].trim().length != 0) { 47 return buildItem(q, answers[i]) 48 } 49 }) 50 51 let script = document.createElement("script") // create a brand new elements. 52 script.type = "application/ld+json" 53 script.innerHTML = JSON.stringify(data) // convert string data in to json format. 54 document.getElementsByTagName("head")[0].appendChild(script) 55 56 if (logOutput) { 57 console.assert(questions.length == answers.length, { 58 questions: questions.length, 59 answers: answers.length, 60 errorMsg: "Questions and Answers are not the same lengths", 61 }) 62 console.log(script.outerHTML) 63 } 64 } 65 } 66})(document)
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.