mirror of https://github.com/kiwix/libkiwix.git
moved blockExternalLink outside of taskbar
- `setBlockExternalLinks()` on server - zero-dependency JS code - JS script added in `inject_externallinks_blocker()` - changed URL to `/catch/external?source=<source>`
This commit is contained in:
parent
0ad8bf45fc
commit
412f0d9c61
|
@ -56,9 +56,9 @@ namespace kiwix
|
||||||
void setNbThreads(int threads) { m_nbThreads = threads; }
|
void setNbThreads(int threads) { m_nbThreads = threads; }
|
||||||
void setVerbose(bool verbose) { m_verbose = verbose; }
|
void setVerbose(bool verbose) { m_verbose = verbose; }
|
||||||
void setTaskbar(bool withTaskbar, bool withLibraryButton)
|
void setTaskbar(bool withTaskbar, bool withLibraryButton)
|
||||||
{ setTaskbar(withTaskbar, withLibraryButton, m_blockExternalLinks); }
|
{ m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; }
|
||||||
void setTaskbar(bool withTaskbar, bool withLibraryButton, bool blockExternalLinks)
|
void setBlockExternalLinks(bool blockExternalLinks)
|
||||||
{ m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; m_blockExternalLinks = blockExternalLinks; }
|
{ m_blockExternalLinks = blockExternalLinks; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Library* mp_library;
|
Library* mp_library;
|
||||||
|
|
|
@ -346,7 +346,7 @@ Response InternalServer::handle_request(const RequestContext& request)
|
||||||
if (request.get_url() == "/random")
|
if (request.get_url() == "/random")
|
||||||
return handle_random(request);
|
return handle_random(request);
|
||||||
|
|
||||||
if (request.get_url() == "/external")
|
if (request.get_url() == "/catch/external")
|
||||||
return handle_captured_external(request);
|
return handle_captured_external(request);
|
||||||
|
|
||||||
return handle_content(request);
|
return handle_content(request);
|
||||||
|
@ -732,11 +732,11 @@ Response InternalServer::handle_captured_external(const RequestContext& request)
|
||||||
|
|
||||||
auto data = get_default_data();
|
auto data = get_default_data();
|
||||||
data.set("source", source);
|
data.set("source", source);
|
||||||
auto response = get_default_response();
|
Response response = Response(m_root, m_verbose.load(), m_withTaskbar, m_withLibraryButton, false);
|
||||||
response.set_template(RESOURCE::templates::captured_external_html, data);
|
response.set_template(RESOURCE::templates::captured_external_html, data);
|
||||||
response.set_mimeType("text/html; charset=utf-8");
|
response.set_mimeType("text/html; charset=utf-8");
|
||||||
response.set_compress(true);
|
response.set_compress(true);
|
||||||
response.set_taskbar("", "", false);
|
response.set_taskbar("", "");
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -117,7 +117,7 @@ void Response::introduce_taskbar()
|
||||||
auto head_content = render_template(RESOURCE::templates::head_part_html, data);
|
auto head_content = render_template(RESOURCE::templates::head_part_html, data);
|
||||||
m_content = appendToFirstOccurence(
|
m_content = appendToFirstOccurence(
|
||||||
m_content,
|
m_content,
|
||||||
"<head>",
|
"<head>\n",
|
||||||
head_content);
|
head_content);
|
||||||
|
|
||||||
auto taskbar_part = render_template(RESOURCE::templates::taskbar_part_html, data);
|
auto taskbar_part = render_template(RESOURCE::templates::taskbar_part_html, data);
|
||||||
|
@ -125,16 +125,19 @@ void Response::introduce_taskbar()
|
||||||
m_content,
|
m_content,
|
||||||
"<body[^>]*>",
|
"<body[^>]*>",
|
||||||
taskbar_part);
|
taskbar_part);
|
||||||
|
}
|
||||||
|
|
||||||
if ( m_blockExternalLinks ) {
|
|
||||||
const std::string capture_external_part = getResource("templates/block_external.js");
|
void Response::inject_externallinks_blocker()
|
||||||
|
{
|
||||||
|
kainjow::mustache::data data;
|
||||||
|
data.set("root", m_root);
|
||||||
|
auto script_tag = render_template(RESOURCE::templates::external_blocker_part_html, data);
|
||||||
m_content = appendToFirstOccurence(
|
m_content = appendToFirstOccurence(
|
||||||
m_content,
|
m_content,
|
||||||
"block external links\n",
|
"<head>\n",
|
||||||
capture_external_part);
|
script_tag);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int Response::send(const RequestContext& request, MHD_Connection* connection)
|
int Response::send(const RequestContext& request, MHD_Connection* connection)
|
||||||
|
@ -145,6 +148,9 @@ int Response::send(const RequestContext& request, MHD_Connection* connection)
|
||||||
if (m_addTaskbar) {
|
if (m_addTaskbar) {
|
||||||
introduce_taskbar();
|
introduce_taskbar();
|
||||||
}
|
}
|
||||||
|
if ( m_blockExternalLinks ) {
|
||||||
|
inject_externallinks_blocker();
|
||||||
|
}
|
||||||
|
|
||||||
bool shouldCompress = m_compress && request.can_compress();
|
bool shouldCompress = m_compress && request.can_compress();
|
||||||
shouldCompress &= m_mimeType.find("text/") != string::npos
|
shouldCompress &= m_mimeType.find("text/") != string::npos
|
||||||
|
@ -248,12 +254,11 @@ void Response::set_entry(const Entry& entry) {
|
||||||
m_mode = ResponseMode::ENTRY;
|
m_mode = ResponseMode::ENTRY;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::set_taskbar(const std::string& bookName, const std::string& bookTitle, bool blockExternalLinks)
|
void Response::set_taskbar(const std::string& bookName, const std::string& bookTitle)
|
||||||
{
|
{
|
||||||
m_addTaskbar = true;
|
m_addTaskbar = true;
|
||||||
m_bookName = bookName;
|
m_bookName = bookName;
|
||||||
m_bookTitle = bookTitle;
|
m_bookTitle = bookTitle;
|
||||||
m_blockExternalLinks = blockExternalLinks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -57,14 +57,14 @@ class Response {
|
||||||
void set_code(int code) { m_returnCode = code; }
|
void set_code(int code) { m_returnCode = code; }
|
||||||
void set_cache(bool cache) { m_useCache = cache; }
|
void set_cache(bool cache) { m_useCache = cache; }
|
||||||
void set_compress(bool compress) { m_compress = compress; }
|
void set_compress(bool compress) { m_compress = compress; }
|
||||||
void set_taskbar(const std::string& bookName, const std::string& bookTitle) { return set_taskbar(bookName, bookTitle, m_blockExternalLinks); }
|
void set_taskbar(const std::string& bookName, const std::string& bookTitle);
|
||||||
void set_taskbar(const std::string& bookName, const std::string& bookTitle, bool blockExternalLinks);
|
|
||||||
void set_range_first(uint64_t start) { m_startRange = start; }
|
void set_range_first(uint64_t start) { m_startRange = start; }
|
||||||
void set_range_len(uint64_t len) { m_lenRange = len; }
|
void set_range_len(uint64_t len) { m_lenRange = len; }
|
||||||
|
|
||||||
int getReturnCode() { return m_returnCode; }
|
int getReturnCode() { return m_returnCode; }
|
||||||
|
|
||||||
void introduce_taskbar();
|
void introduce_taskbar();
|
||||||
|
void inject_externallinks_blocker();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_verbose;
|
bool m_verbose;
|
||||||
|
|
|
@ -86,9 +86,9 @@ Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboo
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton, jboolean blockExternalLinks)
|
Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject obj, jboolean blockExternalLinks)
|
||||||
{
|
{
|
||||||
SERVER->setTaskbar(withTaskbar, withLibraryButton, blockExternalLinks);
|
SERVER->setBlockExternalLinks(blockExternalLinks);
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL
|
JNIEXPORT jboolean JNICALL
|
||||||
|
|
|
@ -33,7 +33,8 @@ public class JNIKiwixServer
|
||||||
public native void setNbThreads(int nbTreads);
|
public native void setNbThreads(int nbTreads);
|
||||||
|
|
||||||
public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton);
|
public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton);
|
||||||
public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton, boolean blockExternalLinks);
|
|
||||||
|
public native void setBlockExternalLinks(boolean blockExternalLinks);
|
||||||
|
|
||||||
public native boolean start();
|
public native boolean start();
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ skin/jquery-ui/jquery-ui.min.css
|
||||||
skin/caret.png
|
skin/caret.png
|
||||||
skin/taskbar.js
|
skin/taskbar.js
|
||||||
skin/taskbar.css
|
skin/taskbar.css
|
||||||
|
skin/block_external.js
|
||||||
templates/search_result.html
|
templates/search_result.html
|
||||||
templates/no_search_result.html
|
templates/no_search_result.html
|
||||||
templates/404.html
|
templates/404.html
|
||||||
|
@ -28,6 +29,6 @@ templates/index.html
|
||||||
templates/suggestion.json
|
templates/suggestion.json
|
||||||
templates/head_part.html
|
templates/head_part.html
|
||||||
templates/taskbar_part.html
|
templates/taskbar_part.html
|
||||||
|
templates/external_blocker_part.html
|
||||||
templates/captured_external.html
|
templates/captured_external.html
|
||||||
templates/block_external.js
|
|
||||||
opensearchdescription.xml
|
opensearchdescription.xml
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
// called only on external links
|
||||||
|
function capture_event(e) { e.target.setAttribute("href", encodeURI("/catch/external?source=" + e.target.href)); }
|
||||||
|
|
||||||
|
// called on all link clicks. filters external and call capture_event
|
||||||
|
function on_click_event(e) {
|
||||||
|
if ("target" in e && "href" in e.target) {
|
||||||
|
var href = e.target.href;
|
||||||
|
if (href.indexOf(window.location.origin) == 0)
|
||||||
|
return;
|
||||||
|
if (href.substr(0, 2) == "//")
|
||||||
|
return capture_event(e);
|
||||||
|
if (href.substr(0, 5) == "http:")
|
||||||
|
return capture_event(e);
|
||||||
|
if (href.substr(0, 6) == "https:")
|
||||||
|
return capture_event(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// script entrypoint (called on document ready)
|
||||||
|
function run() { live('a', 'click', on_click_event); }
|
||||||
|
|
||||||
|
// matches polyfill
|
||||||
|
this.Element && function(ElementPrototype) {
|
||||||
|
ElementPrototype.matches = ElementPrototype.matches ||
|
||||||
|
ElementPrototype.matchesSelector ||
|
||||||
|
ElementPrototype.webkitMatchesSelector ||
|
||||||
|
ElementPrototype.msMatchesSelector ||
|
||||||
|
function(selector) {
|
||||||
|
var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
|
||||||
|
while (nodes[++i] && nodes[i] != node);
|
||||||
|
return !!nodes[i];
|
||||||
|
}
|
||||||
|
}(Element.prototype);
|
||||||
|
|
||||||
|
// helper for enabling IE 8 event bindings
|
||||||
|
function addEvent(el, type, handler) {
|
||||||
|
if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// live binding helper using matchesSelector
|
||||||
|
function live(selector, event, callback, context) {
|
||||||
|
addEvent(context || document, event, function(e) {
|
||||||
|
var found, el = e.target || e.srcElement;
|
||||||
|
while (el && el.matches && el !== context && !(found = el.matches(selector))) el = el.parentElement;
|
||||||
|
if (found) callback.call(el, e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// in case the document is already rendered
|
||||||
|
if (document.readyState!='loading') run();
|
||||||
|
// modern browsers
|
||||||
|
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', run);
|
||||||
|
// IE <= 8
|
||||||
|
else document.attachEvent('onreadystatechange', function(){
|
||||||
|
if (document.readyState=='complete') run();
|
||||||
|
});
|
|
@ -1,17 +0,0 @@
|
||||||
function capture(e) { $(e.target).attr("href", encodeURI("/external?source=" + e.target.href)); }
|
|
||||||
jk( document ).ready(function() {
|
|
||||||
jk("a").on({click: function(e) {
|
|
||||||
if ("target" in e && "href" in e.target) {
|
|
||||||
var href = e.target.href;
|
|
||||||
if (href.indexOf(window.location.origin) == 0)
|
|
||||||
return;
|
|
||||||
if (href.substr(0, 2) == "//")
|
|
||||||
return capture(e);
|
|
||||||
if (href.substr(0, 5) == "http:")
|
|
||||||
return capture(e);
|
|
||||||
if (href.substr(0, 6) == "https:")
|
|
||||||
return capture(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}});
|
|
||||||
});
|
|
|
@ -0,0 +1 @@
|
||||||
|
<script type="text/javascript" src="{{root}}/skin/block_external.js"></script>
|
|
@ -5,7 +5,6 @@
|
||||||
<script type="text/javascript" src="{{root}}/skin/jquery-ui/jquery-ui.min.js"></script>
|
<script type="text/javascript" src="{{root}}/skin/jquery-ui/jquery-ui.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var jk = jQuery.noConflict();
|
var jk = jQuery.noConflict();
|
||||||
// block external links
|
|
||||||
jk(function() {
|
jk(function() {
|
||||||
jk( "#kiwixsearchbox" ).autocomplete({
|
jk( "#kiwixsearchbox" ).autocomplete({
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue