Content server: Also translate calibre://search URLs

This commit is contained in:
Kovid Goyal 2025-01-13 21:21:14 +05:30
parent 4cf2cd30a9
commit 0ca74e7dae
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 62 additions and 24 deletions

View File

@ -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,21 +179,33 @@ 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]
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:
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')
else:

View File

@ -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)