From 369406fb5d2d6c9ecf90defed330da544be03b46 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 10 Aug 2022 13:08:40 +0400 Subject: [PATCH] Viewer settings Made the viewer respect the `--blockexternal` and `--nolibrarybutton` options of `kiwix-serve`. Those options are passed to the viewer via the dynamically generated resource `/viewer_settings.js`. --- src/server/internalServer.cpp | 16 ++++++++++++++++ src/server/internalServer.h | 1 + static/resources_list.txt | 1 + static/skin/viewer.js | 13 ++++++++++--- static/templates/viewer_settings.js | 4 ++++ static/viewer.html | 1 + test/server.cpp | 2 +- 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 static/templates/viewer_settings.js diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 604d8503d..b1bb904e2 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -556,6 +556,9 @@ std::unique_ptr InternalServer::handle_request(const RequestContext& r if (isEndpointUrl(url, "viewer") || isEndpointUrl(url, "skin")) return handle_skin(request); + if (url == "/viewer_settings.js") + return handle_viewer_settings(request); + if (isEndpointUrl(url, "content")) return handle_content(request); @@ -713,6 +716,19 @@ std::unique_ptr InternalServer::handle_suggest(const RequestContext& r return std::move(response); } +std::unique_ptr InternalServer::handle_viewer_settings(const RequestContext& request) +{ + if (m_verbose.load()) { + printf("** running handle_viewer_settings\n"); + } + + const kainjow::mustache::object data{ + {"enable_link_blocking", m_blockExternalLinks ? "true" : "false" }, + {"enable_library_button", m_withLibraryButton ? "true" : "false" } + }; + return ContentResponse::build(*this, RESOURCE::templates::viewer_settings_js, data, "application/javascript; charset=utf-8"); +} + std::unique_ptr InternalServer::handle_skin(const RequestContext& request) { if (m_verbose.load()) { diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 3e4943ad5..a73a7d424 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -126,6 +126,7 @@ class InternalServer { std::unique_ptr handle_request(const RequestContext& request); std::unique_ptr build_redirect(const std::string& bookName, const zim::Item& item) const; std::unique_ptr build_homepage(const RequestContext& request); + std::unique_ptr handle_viewer_settings(const RequestContext& request); std::unique_ptr handle_skin(const RequestContext& request); std::unique_ptr handle_catalog(const RequestContext& request); std::unique_ptr handle_catalog_v2(const RequestContext& request); diff --git a/static/resources_list.txt b/static/resources_list.txt index 5e1b30f73..4ff6cc3ce 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -30,6 +30,7 @@ templates/catalog_v2_entry.xml templates/catalog_v2_categories.xml templates/catalog_v2_languages.xml templates/url_of_search_results_css +templates/viewer_settings.js opensearchdescription.xml ft_opensearchdescription.xml catalog_v2_searchdescription.xml diff --git a/static/skin/viewer.js b/static/skin/viewer.js index 021c05e0f..326d3c4c3 100644 --- a/static/skin/viewer.js +++ b/static/skin/viewer.js @@ -197,7 +197,6 @@ const block_path = `${root}/catch/external`; function blockLink(target) { const encodedHref = encodeURIComponent(target.href); target.setAttribute("href", block_path + "?source=" + encodedHref); - target.setAttribute("target", "_top"); } function isExternalUrl(url) { @@ -213,8 +212,12 @@ function onClickEvent(e) { const iframeDocument = contentIframe.contentDocument; const target = matchingAncestorElement(e.target, iframeDocument, "a"); if (target !== null && "href" in target) { - if ( isExternalUrl(target.href) ) - return blockLink(target); + if ( isExternalUrl(target.href) ) { + target.setAttribute("target", "_top"); + if ( viewerSettings.linkBlockingEnabled ) { + return blockLink(target); + } + } } } @@ -264,6 +267,10 @@ function on_content_load() { window.onresize = handle_visual_viewport_change; window.onhashchange = handle_location_hash_change; +if ( ! viewerSettings.libraryButtonEnabled ) { + document.getElementById("kiwix_serve_taskbar_library_button").remove(); +} + updateCurrentBook(currentBook); handle_location_hash_change(); diff --git a/static/templates/viewer_settings.js b/static/templates/viewer_settings.js new file mode 100644 index 000000000..26922d393 --- /dev/null +++ b/static/templates/viewer_settings.js @@ -0,0 +1,4 @@ +const viewerSettings = { + linkBlockingEnabled: {{enable_link_blocking}}, + libraryButtonEnabled: {{enable_library_button}} +} diff --git a/static/viewer.html b/static/viewer.html index 51667ffc2..8ddc940e7 100644 --- a/static/viewer.html +++ b/static/viewer.html @@ -6,6 +6,7 @@ + + const blankPageUrl = `${root}/skin/blank.html`;