From 0b81faa003d88b10497c0c39f5747f56b734ce19 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 16 Jan 2025 20:01:11 +0100 Subject: [PATCH] [feat] image results: dismiss image viewer on back button press on mobile devices --- searx/static/themes/simple/src/js/main/results.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/searx/static/themes/simple/src/js/main/results.js b/searx/static/themes/simple/src/js/main/results.js index 5e3f8ed40..365f7d6d7 100644 --- a/searx/static/themes/simple/src/js/main/results.js +++ b/searx/static/themes/simple/src/js/main/results.js @@ -52,6 +52,7 @@ this.innerText = this.dataset.copiedText; }); + const isMobile = screen.orientation.type.startsWith('portrait'); searxng.selectImage = function (resultElement) { /* eslint no-unused-vars: 0 */ if (resultElement) { @@ -82,6 +83,11 @@ } } d.getElementById('results').classList.add('image-detail-open'); + + // add a hash to the browser history so that pressing back doesn't return to the previous page + // this allows us to dismiss the image details on pressing the back button on mobile devices + if (isMobile) window.location.hash = 'image-viewer'; + searxng.scrollPageToSelected(); } @@ -102,6 +108,13 @@ searxng.selectNext(false); }); + // listen for the back button to be pressed and dismiss the image details when called + if (isMobile) { + window.addEventListener('hashchange', () => { + if (!window.location.hash) searxng.closeDetail(); + }) + } + w.addEventListener('scroll', function () { var e = d.getElementById('backToTop'), scrollTop = document.documentElement.scrollTop || document.body.scrollTop,