From 5ce8c50646f8c018863cdbaf396bea420586a9fe Mon Sep 17 00:00:00 2001 From: bblaisATcoveo Date: Tue, 1 Oct 2024 11:57:06 -0400 Subject: [PATCH] Various bug updates (#24) * Various bug updates - analytics fail when cookies are turned off - summary template not working correctly for some contextual searches - add mlParameters to filter ART/QS model * Added year adv parameter support * fix code failure checks * Changes following latest comments * do not return any results if year is outside range * remove cross-site scripting comment * adjusted did you mean button style Co-authored-by: Francis Gorman * Update src/connector.js Co-authored-by: Francis Gorman --------- Co-authored-by: Francis Gorman --- src/connector.js | 62 ++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/connector.js b/src/connector.js index 0358612..18a0dc7 100644 --- a/src/connector.js +++ b/src/connector.js @@ -228,33 +228,33 @@ function initTpl() { if ( !querySummaryTemplateHTML ) { if ( lang === "fr" ) { querySummaryTemplateHTML = - `%[numberOfResults] résultats de recherche pour "%[query]"`; + `

%[numberOfResults] résultats de recherche pour "%[query]"

`; } else { querySummaryTemplateHTML = - `%[numberOfResults] search results for "%[query]"`; + `

%[numberOfResults] search results for "%[query]"

`; } } if ( !didYouMeanTemplateHTML ) { if ( lang === "fr" ) { didYouMeanTemplateHTML = - `

Rechercher plutôt ?

`; + `

Rechercher plutôt ?

`; } else { didYouMeanTemplateHTML = - `

Did you mean ?

`; + `

Did you mean ?

`; } } if ( !noQuerySummaryTemplateHTML ) { if ( lang === "fr" ) { noQuerySummaryTemplateHTML = - `%[numberOfResults] résultats de recherche`; + `

%[numberOfResults] résultats de recherche

`; } else { noQuerySummaryTemplateHTML = - `%[numberOfResults] search results`; + `

%[numberOfResults] search results

`; } } @@ -414,7 +414,7 @@ function initEngine() { }, preprocessRequest: ( request, clientOrigin ) => { try { - if( clientOrigin === 'analyticsFetch' ) { + if ( clientOrigin === 'analyticsFetch' ) { let requestContent = JSON.parse( request.body ); // filter user sensitive content @@ -425,16 +425,20 @@ function initEngine() { const searchEvent = new CustomEvent( "searchEvent", { detail: requestContent } ); document.dispatchEvent( searchEvent ); } - if( clientOrigin === 'searchApiFetch' ) { + if ( clientOrigin === 'searchApiFetch' ) { let requestContent = JSON.parse( request.body ); // filter user sensitive content requestContent.enableQuerySyntax = params.isAdvancedSearch; - requestContent.analytics.originLevel3 = params.originLevel3; - request.body = JSON.stringify( requestContent ); + requestContent.mlParameters = { "filters": { "searchpageurl": params.originLevel3 } }; + + if ( requestContent.analytics ) { + requestContent.analytics.originLevel3 = params.originLevel3; + } + let q = requestContent.q; - requestContent.q = sanitizeQuery(q); - request.body = JSON.stringify(requestContent); + requestContent.q = sanitizeQuery( q ); + request.body = JSON.stringify( requestContent ); } } catch { console.warn( "No Headless Engine Loaded." ); @@ -476,7 +480,7 @@ function initEngine() { statusController = buildSearchStatus( headlessEngine ); if ( urlParams.allq || urlParams.exctq || urlParams.anyq || urlParams.noneq || urlParams.fqupdate || - urlParams.dmn || urlParams.fqocct || urlParams.elctn_cat || urlParams.filetype || urlParams.site ) { + urlParams.dmn || urlParams.fqocct || urlParams.elctn_cat || urlParams.filetype || urlParams.site || urlParams.year ) { let q = []; let qString = ""; if ( urlParams.allq ) { @@ -599,6 +603,16 @@ function initEngine() { } } + if ( urlParams.year ) { + const year = Number.parseInt( urlParams.year ); + if ( Number.isInteger( year ) && ( year >= 2000 ) && ( year <= ( new Date().getFullYear() + 1 ) ) ) { + aqString += ' @uri=".ca/' + urlParams.year + '"'; + } + else { + aqString += ' NOT @uri'; + } + } + if ( urlParams.site ) { let site = urlParams.site.toLowerCase().replace( '*', '' ); aqString += ' @canadagazettesite==' + site; @@ -863,7 +877,7 @@ function updateResultListState( newState ) { .replace( '%[result.title]', result.title ) .replace( '%[result.raw.author]', author ) .replace( '%[result.breadcrumb]', result.raw.displaynavlabel ? result.raw.displaynavlabel : result.printableUri ) - .replace( '%[result.printableUri]', result.printableUri ) + .replace( '%[result.printableUri]', result.printableUri.replaceAll( '&' , '&' ) ) .replace( '%[short-date-en]', getShortDateFormat( resultDate ) ) .replace( '%[short-date-fr]', getShortDateFormat( resultDate ) ) .replace( '%[long-date-en]', getLongDateFormat( resultDate, 'en' ) ) @@ -901,16 +915,18 @@ function updateQuerySummaryState( newState ) { querySummaryElement.textContent = ""; if ( querySummaryState.total > 0 ) { let numberOfResults = querySummaryState.total.toLocaleString( params.lang ); - // Create the

element - const hTwoAnchor = document.createElement("h2"); // Generate the text content - const querySummaryText = ((querySummaryState.query !== "" && !params.isAdvancedSearch) ? querySummaryTemplateHTML : noQuerySummaryTemplateHTML) - .replace('%[numberOfResults]', numberOfResults) - .replace('%[query]', querySummaryState.query) - .replace('%[queryDurationInSeconds]', querySummaryState.durationInSeconds.toLocaleString(params.lang)); - hTwoAnchor.textContent = querySummaryText; - querySummaryElement.innerHTML = ""; - querySummaryElement.appendChild(hTwoAnchor); + const querySummaryHTML = ( ( querySummaryState.query !== "" && !params.isAdvancedSearch ) ? querySummaryTemplateHTML : noQuerySummaryTemplateHTML ) + .replace( '%[numberOfResults]', numberOfResults ) + .replace( '%[query]', '' ) + .replace( '%[queryDurationInSeconds]', querySummaryState.durationInSeconds.toLocaleString( params.lang ) ); + + querySummaryElement.innerHTML = querySummaryHTML; + + const queryElement = querySummaryElement.querySelector( '.sr-query' ); + if ( queryElement ){ + queryElement.textContent = querySummaryState.query; + } } else { querySummaryElement.innerHTML = noResultTemplateHTML;