removed logging + improved dynamic result fetching
This commit is contained in:
parent
21c8f549f0
commit
a138928d63
6 changed files with 28 additions and 29 deletions
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
var (
|
||||
debugMode bool = true
|
||||
funcs = template.FuncMap{
|
||||
funcs = template.FuncMap{
|
||||
"sub": func(a, b int) int {
|
||||
return a - b
|
||||
},
|
||||
|
@ -15,10 +15,3 @@ var (
|
|||
},
|
||||
}
|
||||
)
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
</form>
|
||||
<div class="results_settings">
|
||||
<form>
|
||||
<h1>Settings</h1>
|
||||
<h1>SETTINGS ARE NOT IMPLEMENTED YET</h1>
|
||||
<h2>Theme</h2>
|
||||
<label for="theme-dark">Dark Theme:</label>
|
||||
<input type="checkbox" class="results-settings" id="theme-dark" name="theme" value="dark"><br>
|
||||
|
|
|
@ -67,8 +67,10 @@
|
|||
</div>
|
||||
<br>
|
||||
{{end}}
|
||||
{{else}}
|
||||
{{else if .NoResults}}
|
||||
<div class="no-results">No results found for '{{ .Query }}'. Try different keywords.</div>
|
||||
{{else}}
|
||||
<div class="no-more-results">Looks like this is the end of results.</div>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="prev-next prev-img" id="prev-next">
|
||||
|
@ -84,26 +86,39 @@
|
|||
</form>
|
||||
</div>
|
||||
<script>
|
||||
document.getElementById('content').classList.remove('js-enabled');
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
if (document.getElementById('prev-next')) {
|
||||
document.getElementById('prev-next').style.display = 'none';
|
||||
|
||||
let page = {{ .Page }};
|
||||
const query = "{{ .Query }}";
|
||||
let loading = false;
|
||||
let hasMoreResults = true;
|
||||
|
||||
function loadResults(newPage) {
|
||||
if (loading || !hasMoreResults) return;
|
||||
loading = true;
|
||||
fetch(`/search?q=${encodeURIComponent(query)}&t=text&p=${newPage}`)
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(data, 'text/html');
|
||||
const newResults = doc.getElementById('results').innerHTML;
|
||||
document.getElementById('results').innerHTML += newResults;
|
||||
page = newPage;
|
||||
const noResultsMessage = "No results found for '{{ .Query }}'. Try different keywords.";
|
||||
|
||||
if (newResults.includes(noResultsMessage)) {
|
||||
document.getElementById('results').innerHTML += "<div class='no-more-results'>Looks like this is the end of results.</div>";
|
||||
hasMoreResults = false;
|
||||
} else {
|
||||
document.getElementById('results').innerHTML += newResults;
|
||||
page = newPage;
|
||||
}
|
||||
loading = false;
|
||||
})
|
||||
.catch(error => console.error('Error loading results:', error));
|
||||
.catch(error => {
|
||||
console.error('Error loading results:', error);
|
||||
loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('scroll', () => {
|
||||
|
|
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
@ -48,9 +47,6 @@ func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSear
|
|||
Description: strings.TrimSpace(description),
|
||||
}
|
||||
results = append(results, result)
|
||||
if debugMode {
|
||||
log.Printf("Processed DuckDuckGo result: %+v\n", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,9 +108,6 @@ func parseResults(doc *goquery.Document) []TextSearchResult {
|
|||
Description: description,
|
||||
}
|
||||
results = append(results, result)
|
||||
if debugMode {
|
||||
log.Printf("Google result: %+v\n", result)
|
||||
}
|
||||
})
|
||||
|
||||
return results
|
||||
|
|
10
text.go
10
text.go
|
@ -90,15 +90,11 @@ func prefetchPage(query, safe, lang string, page int) {
|
|||
|
||||
func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
||||
var results []TextSearchResult
|
||||
var err error
|
||||
var duration time.Duration
|
||||
|
||||
for attempts := 0; attempts < len(textSearchEngines); attempts++ {
|
||||
engine := selectSearchEngine(textSearchEngines)
|
||||
for _, engine := range textSearchEngines {
|
||||
log.Printf("Using search engine: %s", engine.Name)
|
||||
|
||||
var searchResults []SearchResult
|
||||
searchResults, duration, err = engine.Func(query, safe, lang, page)
|
||||
searchResults, duration, err := engine.Func(query, safe, lang, page)
|
||||
updateEngineMetrics(&engine, duration, err == nil)
|
||||
if err != nil {
|
||||
log.Printf("Error performing search with %s: %v", engine.Name, err)
|
||||
|
@ -158,6 +154,7 @@ func displayResults(w http.ResponseWriter, results []TextSearchResult, query, la
|
|||
HasNextPage bool
|
||||
LanguageOptions []LanguageOption
|
||||
CurrentLang string
|
||||
NoResults bool
|
||||
}{
|
||||
Results: results,
|
||||
Query: query,
|
||||
|
@ -167,6 +164,7 @@ func displayResults(w http.ResponseWriter, results []TextSearchResult, query, la
|
|||
HasNextPage: hasNextPage,
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: lang,
|
||||
NoResults: len(results) == 0,
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
|
|
Loading…
Add table
Reference in a new issue