Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent search pages from showing up in the results of searches. #15081

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion pages/search.njk
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<!-- Page Title-->
<h1 class="page-title">{{ 'Search Results' | i18n(locale) }} "<span class="query-display"></span>"</h1>
<div class="entry-content">
<div class="gcse-searchresults-only">
<div class="gcse-searchresults-only" data-webSearchSafesearch="active">
</div>
</article>
</div>
Expand All @@ -51,6 +51,44 @@
{# https://cse.google.com/cse.js?cx=001779225245372747843:o_16poin-0q #}

<script>
function myWebResultsReadyCallback() {
console.log("WEB RESULTS READY");
}
function myWebResultsRenderedCallback(gname, query, promoElts, resultElts) {
console.log("WEB RESULTS RENDERED");
// const colors = ['Gainsboro', 'FloralWhite'];
// let colorSelector = 0;
for (const result of resultElts) {
// result.style.backgroundColor = colors[colorSelector];
// colorSelector = (colorSelector + 1) % colors.length;
var titles = result.getElementsByClassName('gs-title');
console.log("elements found",titles.length);
var gotOne = false;
for (title of titles) {
// get data-cturl contents if present
if ('cturl' in title.dataset) {
var cturl = title.dataset.cturl;
if (cturl.includes('/search')) {
console.log("SEARCH FOUND");
result.style.display = 'none';
gotOne = true;
}
}
}
// catches the occasional listing which does not have a cturl link
if (gotOne == false && result.innerHTML.includes('/search')) {
result.style.backgroundColor = 'red';
result.style.display = 'none';
gotOne = true;
}
}

// var searchresults= document.getElementsByClassName("gsc-webResult");
// console.log("Search results length: " + searchResults.length);
// inside look for a.gs-title that contains data-cturl that contains "/search/"
// if found, hide the parent.
}

function sanitize(string) {
console.log("sanitize");
const map = {
Expand All @@ -69,6 +107,27 @@
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?language=en-US&cx=' + cx;

// add resultsreadycallback - jbum
// gcse.searchCallbacks.web.ready = myWebResultsReadyCallback;
window.__gcse = {
// parsetags: 'explicit', // Defaults to 'onload'
// initializationCallback: myInitializationCallback,
searchCallbacks: {
// image: {
// starting: myImageSearchStartingCallback,
// ready: myImageResultsReadyCallback,
// rendered: myImageResultsRenderedCallback,
// },
web: {
// starting: myWebSearchStartingCallback,
ready: myWebResultsReadyCallback,
rendered: myWebResultsRenderedCallback,
},
},
};


const s = document.getElementsByTagName('script');
s[s.length - 1]
.parentNode
Expand Down Expand Up @@ -105,6 +164,10 @@
history.pushState(null, null, `${currentHost}${window.location.pathname}?q=${query}`);
}
myWebSearchStartingCallback(query)




</script>

{% endblock %}
Loading