mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Content server: Also translate calibre://search URLs
This commit is contained in:
parent
4cf2cd30a9
commit
0ca74e7dae
@ -14,7 +14,7 @@ from book_list.library_data import (
|
|||||||
current_virtual_library, download_url, library_data, load_status,
|
current_virtual_library, download_url, library_data, load_status,
|
||||||
set_book_metadata, download_data_file_url
|
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 (
|
from book_list.theme import (
|
||||||
color_scheme, get_color, get_color_as_rgba, get_font_size
|
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))
|
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):
|
def replace_calibre_links_to_books(html):
|
||||||
parser = v'new DOMParser()'
|
parser = v'new DOMParser()'
|
||||||
dom = parser.parseFromString(html, 'text/html')
|
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://'):
|
if not link.href or not link.href.startswith('calibre://'):
|
||||||
return
|
return
|
||||||
url = v'new URL(link.href, window.location.origin)'
|
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
|
return
|
||||||
path = url.pathname
|
path = url.pathname
|
||||||
parts = str.split(path, '/')
|
parts = str.split(path, '/')
|
||||||
lib, book_id = parts[1], parts[2]
|
lib, book_id = parts[1], parts[2]
|
||||||
try:
|
lib = decode_calibre_url_library_id(lib)
|
||||||
book_id = int(book_id)
|
if url.hostname is 'search':
|
||||||
except:
|
q = vl = ''
|
||||||
return
|
if url.searchParams.get('eq'):
|
||||||
if lib.startswith('_hex_-'):
|
q = hex_decode(url.searchParams.get('eq'))
|
||||||
lib = lib[6:]
|
elif url.searchParams.get('q'):
|
||||||
v'const uint8Array = new Uint8Array(lib.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));'
|
q = url.searchParams.get('q')
|
||||||
v'lib = new TextDecoder("utf-8").decode(uint8Array);'
|
if not q:
|
||||||
elif lib is '_':
|
return
|
||||||
lib = current_library_id()
|
if url.searchParams.get('encoded_virtual_library'):
|
||||||
if url.hostname is 'show-book':
|
vl = hex_decode(url.searchParams.get('encoded_virtual_library'))
|
||||||
new_href = query_as_href({'book_id': book_id + '', 'library_id': lib}, 'book_details')
|
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:
|
else:
|
||||||
fmt = parts[3]
|
try:
|
||||||
extra_query = {}
|
book_id = int(book_id)
|
||||||
url.searchParams.forEach(def(val, key):
|
except:
|
||||||
extra_query[key] = val
|
return
|
||||||
)
|
if url.hostname is 'show-book':
|
||||||
new_href = open_book_url_in_library(lib, book_id, fmt, extra_query)
|
new_href = query_as_href({'book_id': book_id + '', 'library_id': lib}, 'book_details')
|
||||||
if url.search:
|
else:
|
||||||
new_href
|
fmt = parts[3]
|
||||||
link.href = new_href
|
extra_query = {}
|
||||||
changed = True
|
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:
|
if changed:
|
||||||
return v'new XMLSerializer().serializeToString(dom);'
|
return v'new XMLSerializer().serializeToString(dom);'
|
||||||
|
@ -86,6 +86,18 @@ def open_book_url_in_library(library_id, book_id, fmt, extra_query):
|
|||||||
return ans + encode_query(q, '#')
|
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):
|
def open_book_url(book_id, fmt, extra_query):
|
||||||
return open_book_url_in_library(current_library_id(), book_id, fmt, extra_query)
|
return open_book_url_in_library(current_library_id(), book_id, fmt, extra_query)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user