diff options
author | Andrew Dolgov <[email protected]> | 2017-02-27 14:34:43 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2017-02-27 14:34:43 +0300 |
commit | bcf8165809d732a1f5959124bb09814edd5490da (patch) | |
tree | ef8dfe4ea347f993efd36f259f5aa13b75d39903 | |
parent | 05524168a957704eabbd3713a6d8efea9f055b80 (diff) |
support dictd lookups on word selection
-rw-r--r-- | backend.php | 35 | ||||
-rw-r--r-- | config.php-dist | 3 | ||||
-rw-r--r-- | css/read.css | 6 | ||||
-rw-r--r-- | read.html | 41 |
4 files changed, 84 insertions, 1 deletions
diff --git a/backend.php b/backend.php index b7298fa..e7a7e41 100644 --- a/backend.php +++ b/backend.php @@ -184,6 +184,41 @@ break; + case "define": + + if (defined('DICT_ENABLED') && DICT_ENABLED) { + + $word = escapeshellarg($_REQUEST["word"]); + + exec(DICT_CLIENT . " -h ". DICT_SERVER ." $word 2>&1", $output, $rc); + + if ($rc == 0) { + print json_encode(["result" => $output]); + + } else if ($rc == 21) { + + $word_match = ""; + + foreach ($output as $line) { + if (preg_match('/^[^ ]+: *([^ ]+)$/', $line, $match)) { + if ($match[1]) { + $word_match = escapeshellarg($match[1]); + break; + } + } + } + + unset($output); + exec(DICT_CLIENT . " -h ". DICT_SERVER ." $word_match 2>&1", $output, $rc); + + if ($rc == 0) { + print json_encode(["result" => $output]); + } + } + } + + break; + default: header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo "Method not found."; diff --git a/config.php-dist b/config.php-dist index 7f6d737..3404f34 100644 --- a/config.php-dist +++ b/config.php-dist @@ -5,4 +5,7 @@ define('BOOKS_DIR', '/home/fox/Books'); define('CALIBRE_DB', BOOKS_DIR . "/metadata.db"); + define('DICT_ENABLED', false); + define('DICT_CLIENT', '/usr/bin/dict'); + define('DICT_SERVER', 'localhost'); diff --git a/css/read.css b/css/read.css index 6ef5db2..e6efd3e 100644 --- a/css/read.css +++ b/css/read.css @@ -92,4 +92,8 @@ ul.toc_list { overflow : auto; } - +.dict_result { + max-height : 300px; + height : auto; + overflow : auto; +} @@ -92,6 +92,23 @@ </div> </div> +<div class="modal fade" id="dict-modal" tabindex="-1" role="dialog"> + <div class="modal-dialog" role="document"> + <div class="modal-content"> + <div class="modal-header"> + <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> + <h4 class="modal-title">Dictionary Lookup</h4> + </div> + <div class="modal-body"> + <div class="dict_result"> </div> + </div> + <div class="modal-footer"> + <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> + </div> + </div> + </div> +</div> + <div class="modal fade" id="toc-modal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> @@ -354,6 +371,30 @@ } }; + + $("#reader iframe")[0].contentWindow.onmouseup = function(event) { + + if (!navigator.onLine) return; + + var wnd = this; + var sel = wnd.getSelection().toString().trim(); + + if (sel.match(/^\w+$/)) { + + $.post("backend.php", {op: 'define', word: sel}, function(data) { + if (data) { + + wnd.getSelection().removeAllRanges(); + + $(".dict_result").html(data.result.join("<br/>")); + + $("#dict-modal").modal('show'); + + } + }); + } + }; + }); book.on("renderer:keydown", hotkey_handler); |