Add Commonly Visited sites to SearXNG homepage
This revision is from 2024/07/25 22:13. You can Restore it.
/usr/local/searxng/searxng-src/searx/webapp.py
def get_most_visited_sites(limit=9):
sites = []
file_path = os.path.join(os.path.dirname(__file__), 'most_visited_sites.txt')
try:
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split('|')
if len(parts) == 3:
sites.append({
"url": parts[0],
"title": parts[1],
"favicon": parts[2]
})
if len(sites) == limit:
break
except FileNotFoundError:
print(f"Warning: {file_path} not found. Using default sites.")
# Fallback to default sites if file is not found
sites = [
{"url": "https://imtcoin.com", "title": "Immortality", "favicon": "https://imtcoin.com/favicon.ico"},
{"url": "https://wikipedia.org", "title": "Wikipedia", "favicon": "https://wikipedia.org/favicon.ico"},
{"url": "https://github.com", "title": "GitHub", "favicon": "https://github.com/favicon.ico"},
]
return sites[:limit]
@app.route('/', methods=['GET', 'POST'])
def index():
"""Render index page."""
# redirect to search if there's a query in the request
if request.form.get('q'):
query = ('?' + request.query_string.decode()) if request.query_string else ''
return redirect(url_for('search') + query, 308)
# Get the most visited sites
most_visited_sites = get_most_visited_sites(limit=9)
return render(
'index.html',
selected_categories=get_selected_categories(request.preferences, request.form),
current_locale=request.preferences.get_value("locale"),
most_visited_sites=most_visited_sites
)
/usr/local/searxng/searxng-src/searx/templates/index.html
{% extends "simple/base.html" %}
{% from 'simple/icons.html' import icon_big %}
{% block content %}
<div class="index">
<div class="title"><h1>SearXNG</h1></div>
{% include 'simple/simple_search.html' %}
{% if most_visited_sites %}
<div class="top-sites">
<h2>Top Sites</h2>
<div class="site-grid">
{% for site in most_visited_sites %}
<a href="{{ site.url }}" class="site-tile">
<img src="{{ site.favicon }}" alt="{{ site.title }} favicon">
<span>{{ site.title }}</span>
</a>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endblock %}
Optional CSS
.site-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
max-width: 600px;
margin: 0 auto;
}.site-tile {
display: flex;
flex-direction: column;
align-items: center;
text-decoration: none;
color: inherit;
padding: 10px;
border-radius: 5px;
transition: background-color 0.2s;
}.site-tile:hover {
background-color: rgba(0, 0, 0, 0.1);
}.site-tile img {
width: 16px; /* Set a fixed width */
height: 16px; /* Set a fixed height */
object-fit: contain; /* Ensure the entire image fits within the dimensions */
margin-bottom: 5px;
}.site-tile span {
text-align: center;
word-break: break-word; /* Allows long titles to wrap */
max-width: 100%; /* Ensures text doesn't overflow the tile */
}