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