diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index b0cb6494f0..a17543730d 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -14,7 +14,7 @@ from book_list.library_data import ( current_virtual_library, download_url, library_data, load_status, set_book_metadata, download_data_file_url ) -from book_list.router import back, home, open_book, report_a_load_failure, show_note, open_book_url_in_library +from book_list.router import back, home, open_book, report_a_load_failure, show_note, open_book_url_in_library, search_url_in_library from book_list.theme import ( color_scheme, get_color, get_color_as_rgba, get_font_size ) @@ -156,6 +156,20 @@ if window?: window.addEventListener('resize', debounce(adjust_all_iframes, 250)) +def hex_decode(lib): + v'const uint8Array = new Uint8Array(lib.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));' + v'lib = new TextDecoder("utf-8").decode(uint8Array);' + return lib + + +def decode_calibre_url_library_id(lib): + if lib.startswith('_hex_-'): + lib = hex_decode(lib[6:]) + elif lib is '_': + lib = current_library_id() + return lib + + def replace_calibre_links_to_books(html): parser = v'new DOMParser()' dom = parser.parseFromString(html, 'text/html') @@ -165,34 +179,46 @@ def replace_calibre_links_to_books(html): if not link.href or not link.href.startswith('calibre://'): return url = v'new URL(link.href, window.location.origin)' - if url.hostname is not 'show-book' and url.hostname is not 'view-book': + if url.hostname is not 'show-book' and url.hostname is not 'view-book' and url.hostname is not 'search': return path = url.pathname parts = str.split(path, '/') lib, book_id = parts[1], parts[2] - try: - book_id = int(book_id) - except: - return - if lib.startswith('_hex_-'): - lib = lib[6:] - v'const uint8Array = new Uint8Array(lib.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));' - v'lib = new TextDecoder("utf-8").decode(uint8Array);' - elif lib is '_': - lib = current_library_id() - if url.hostname is 'show-book': - new_href = query_as_href({'book_id': book_id + '', 'library_id': lib}, 'book_details') + lib = decode_calibre_url_library_id(lib) + if url.hostname is 'search': + q = vl = '' + if url.searchParams.get('eq'): + q = hex_decode(url.searchParams.get('eq')) + elif url.searchParams.get('q'): + q = url.searchParams.get('q') + if not q: + return + if url.searchParams.get('encoded_virtual_library'): + vl = hex_decode(url.searchParams.get('encoded_virtual_library')) + elif url.searchParams.get('virtual_library'): + vl = url.searchParams.get('virtual_library') + if vl is '_': + vl = '' + link.href = search_url_in_library(lib, q, vl) + changed = True else: - fmt = parts[3] - extra_query = {} - url.searchParams.forEach(def(val, key): - extra_query[key] = val - ) - new_href = open_book_url_in_library(lib, book_id, fmt, extra_query) - if url.search: - new_href - link.href = new_href - changed = True + try: + book_id = int(book_id) + except: + return + if url.hostname is 'show-book': + new_href = query_as_href({'book_id': book_id + '', 'library_id': lib}, 'book_details') + else: + fmt = parts[3] + extra_query = {} + url.searchParams.forEach(def(val, key): + extra_query[key] = val + ) + new_href = open_book_url_in_library(lib, book_id, fmt, extra_query) + if url.search: + new_href + link.href = new_href + changed = True ) if changed: return v'new XMLSerializer().serializeToString(dom);' diff --git a/src/pyj/book_list/router.pyj b/src/pyj/book_list/router.pyj index df2e2c5f4d..dd090a0d9e 100644 --- a/src/pyj/book_list/router.pyj +++ b/src/pyj/book_list/router.pyj @@ -86,6 +86,18 @@ def open_book_url_in_library(library_id, book_id, fmt, extra_query): return ans + encode_query(q, '#') +def search_url_in_library(library_id, search, vl, sort): + ans = absolute_path('') + q = {'mode': 'book_list', 'search': search, 'panel': 'book_list'} + if library_id: + q.library_id = library_id + if vl: + q.vl = vl + if sort: + q.sort = sort + return ans + encode_query(q, '#') + + def open_book_url(book_id, fmt, extra_query): return open_book_url_in_library(current_library_id(), book_id, fmt, extra_query)