Add Commonly Visited sites to SearXNG homepage

This revision is from 2024/07/26 03:16. 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">

<div class="site-grid" style="margin-top: 33px">

{% for site in most_visited_sites %}

<a href="{{ site.url }}" class="site-tile">

<img src="{{ site.favicon }}" alt="{{ site.title }} favicon" width="32">

</a>

{% endfor %}

</div>

</div>

{% endif %}

<button id="toggleAddSiteForm" class="toggle-button" style="margin-top: 40px">Add New Site</button>

<div id="addSiteForm" class="add-site-form" style="display: none; padding: 40px;">

<form method="post">

<input type="url" name="new_url" placeholder="https://example.com" required>

<input type="text" name="new_title" placeholder="Site Title" required>

<button type="submit">Add Site</button>

</form>

</div>

</div>

<script>

var form = document.getElementById('addSiteForm');

if (form.style.display === 'none') {

form.style.display = 'block';

this.textContent = 'Hide Add Site Form';

} else {

form.style.display = 'none';

this.textContent = 'Add New Site';

}

});

</script>

{% endblock %}

  

📝 📜 ⏱️ ⬆️