1class HtmlLoader { 2 3 load(url, selector) { 4 return fetch(url, { 5 headers: { 6 'x-selector': selector 7 } 8 }).then(function (response) { 9 try{ 10 if(window.AnalyticsManager){ 11 window.AnalyticsManager.recordFromAjax(response); 12 } 13 }catch(e){ 14 console.error(e); 15 } 16 17 if (!response.ok) { 18 throw Error(); 19 } 20 return response.text(); 21 }).then(function (html) { 22 return new Promise((resolve, reject) => { 23 const parser = new DOMParser(); 24 const loadedDocument = parser.parseFromString(html, 'text/html'); 25 const styles = loadedDocument.querySelectorAll('link[rel="stylesheet"]'); 26 27 styles.forEach(function (style) { 28 const src = style.getAttribute('href'); 29 if (!src) { 30 console.debug('Skip inline style') 31 return; 32 } 33 if (document.querySelector('link[href="' + src + '"]')) { 34 console.debug('Already loaded: ' + src); 35 return; 36 } 37 console.info('Load stylesheet: ' + src); 38 document.head.append(style); 39 }); 40 41 let scriptPendings = 0; 42 const scriptLoaded = function () { 43 scriptPendings--; 44 if (scriptPendings === 0) { 45 const titleNode = loadedDocument.querySelector('title'); 46 const title = titleNode ? titleNode.innerText : undefined; 47 48 try{ 49 if(window.AnalyticsManager){ 50 window.AnalyticsManager.pageView(url, title); 51 } 52 }catch(e){ 53 console.error(e); 54 } 55 56 resolve({ 57 title: title, 58 node: loadedDocument.querySelector(selector), 59 doc: loadedDocument 60 }); 61 } 62 } 63 const scripts = loadedDocument.querySelectorAll('script'); 64 scripts.forEach(function (script) { 65 const src = script.getAttribute('src'); 66 if (!src) { 67 console.debug('Skip inline script') 68 return; 69 } 70 if (document.querySelector('script[src="' + src + '"]')) { 71 console.debug('Already loaded: ' + src); 72 return; 73 } 74 scriptPendings++; 75 console.info('Load script: ' + src); 76 77 const newScript = document.createElement("script"); 78 newScript.async = false; 79 newScript.onload = function(){ 80 scriptLoaded(); 81 }; 82 newScript.onerror = function(){ 83 console.error('Error while loading ' + url); 84 }; 85 document.head.appendChild(newScript); 86 newScript.src = src; 87 }); 88 89 if (scriptPendings === 0) { 90 scriptPendings = 1; 91 scriptLoaded(); 92 } 93 }); 94 }); 95 96 } 97 98} 99 100window.HtmlLoader = HtmlLoader;
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.