Skip to content

Commit

Permalink
Various bug updates (#24)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update src/connector.js

Co-authored-by: Francis Gorman <[email protected]>

---------

Co-authored-by: Francis Gorman <[email protected]>
  • Loading branch information
bblaisATcoveo and GormFrank authored Oct 1, 2024
1 parent f9f76e1 commit 5ce8c50
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,33 +228,33 @@ function initTpl() {
if ( !querySummaryTemplateHTML ) {
if ( lang === "fr" ) {
querySummaryTemplateHTML =
`%[numberOfResults] résultats de recherche pour "%[query]"`;
`<h2>%[numberOfResults] résultats de recherche pour "%[query]"</h2>`;
}
else {
querySummaryTemplateHTML =
`%[numberOfResults] search results for "%[query]"`;
`<h2>%[numberOfResults] search results for "%[query]"</h2>`;
}
}

if ( !didYouMeanTemplateHTML ) {
if ( lang === "fr" ) {
didYouMeanTemplateHTML =
`<p class="h5 mrgn-lft-md">Rechercher plutôt <button class="btn btn-lg btn-link p-0" type="button">%[correctedQuery]</button> ?</p>`;
`<p class="h5">Rechercher plutôt <button class="btn btn-lg btn-link p-0 mrgn-bttm-sm" type="button">%[correctedQuery]</button> ?</p>`;
}
else {
didYouMeanTemplateHTML =
`<p class="h5 mrgn-lft-md">Did you mean <button class="btn btn-lg btn-link p-0" type="button">%[correctedQuery]</button> ?</p>`;
`<p class="h5">Did you mean <button class="btn btn-lg btn-link p-0 mrgn-bttm-sm" type="button">%[correctedQuery]</button> ?</p>`;
}
}

if ( !noQuerySummaryTemplateHTML ) {
if ( lang === "fr" ) {
noQuerySummaryTemplateHTML =
`%[numberOfResults] résultats de recherche`;
`<h2>%[numberOfResults] résultats de recherche</h2>`;
}
else {
noQuerySummaryTemplateHTML =
`%[numberOfResults] search results`;
`<h2>%[numberOfResults] search results</h2>`;
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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." );
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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( '&' , '&amp;' ) )
.replace( '%[short-date-en]', getShortDateFormat( resultDate ) )
.replace( '%[short-date-fr]', getShortDateFormat( resultDate ) )
.replace( '%[long-date-en]', getLongDateFormat( resultDate, 'en' ) )
Expand Down Expand Up @@ -901,16 +915,18 @@ function updateQuerySummaryState( newState ) {
querySummaryElement.textContent = "";
if ( querySummaryState.total > 0 ) {
let numberOfResults = querySummaryState.total.toLocaleString( params.lang );
// Create the <h2> 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]', '<span class="sr-query"></span>' )
.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;
Expand Down

0 comments on commit 5ce8c50

Please sign in to comment.