As a temporary solution if someone needs to enable the toggle button by default, you can add this script to your theme (I’ve put it under <body>) which basically looks for a change in the AI search results and then toggles the AI results to enabled. This isn’t the cleanest code, maybe @awesomerobot has a more elegant way to do this.
<script type="text/javascript">
// THIS SCRIPT IS TO ENABLE THE TOGGLE TO INCLUDE SEARCH RESULTS FROM AI AFTER THE SEARCH IS COMPLETE - DELETE ONCE THERE IS A NATIVE OPTION TO ENABLE THIS BY DEFAULT
console.log('Script loaded. Continually looking for .semantic-search__searching and managing observer.');
let searchObserver = null; // Variable to hold the observer instance
function observeSearchContainer() {
const searchStatusContainer = document.querySelector('.semantic-search__searching');
if (searchStatusContainer) {
// If the container is found
if (!searchObserver) {
// If an observer is not already running, start one
console.log('.semantic-search__searching found. Starting observation.');
searchObserver = new MutationObserver(function(mutations) {
//console.log('Mutation detected in .semantic-search__searching:', mutations);
mutations.forEach(function(mutation) {
// Check for relevant mutations within the observed container
if (mutation.type === 'characterData') {
//console.log('Relevant mutation type detected. Attempting to trigger toggle button functionality. ' + mutation.type);
const currentToggleButton = document.querySelector('button.d-toggle-switch__checkbox.semantic-search__results-toggle');
if (currentToggleButton) {
const isCurrentlyEnabled = currentToggleButton.getAttribute('aria-checked') === 'true';
if (!isCurrentlyEnabled) { // If the toggle is NOT currently enabled
// Trigger a click event to activate associated functionality
currentToggleButton.click();
console.log('Toggle button click event triggered.');
} else {
console.log('Toggle button already enabled.');
}
} else {
// This case might happen if the toggle button is removed while the container is still present
console.log('Toggle button not found when trying to trigger functionality.');
}
}
});
});
const config = { childList: true, subtree: true, characterData: true, attributes: true };
searchObserver.observe(searchStatusContainer, config);
console.log('MutationObserver started on .semantic-search__searching.');
} else {
// Container found, and observer is already running for it
//console.log('.semantic-search__searching found, observer already active.');
}
} else {
// If the container is NOT found
if (searchObserver) {
// If an observer was previously running, it means the container was removed
console.log('.semantic-search__searching removed. Disconnecting observer.');
searchObserver.disconnect(); // Stop observing
searchObserver = null; // Reset the observer variable
} else {
// Container not found, and no observer is active (correct state)
//console.log('.semantic-search__searching not found yet.');
}
}
}
// Use an interval to periodically check for the existence of the container
const containerCheckInterval = setInterval(observeSearchContainer, 500); // Check every 500 milliseconds
</script>