Simplify getting pdfjs main page
This commit is contained in:
parent
df67c254f8
commit
24fb3b0d1b
@ -42,39 +42,6 @@ class PDFJSNotFound(Exception):
|
|||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
def generate_pdfjs_page(url):
|
|
||||||
"""Return the html content of a page that displays url with pdfjs.
|
|
||||||
|
|
||||||
Returns a string.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
url: The url of the pdf as QUrl.
|
|
||||||
"""
|
|
||||||
if not is_available():
|
|
||||||
return jinja.render('no_pdfjs.html',
|
|
||||||
url=url.toDisplayString(),
|
|
||||||
title="PDF.js not found")
|
|
||||||
viewer = get_pdfjs_res('web/viewer.html').decode('utf-8')
|
|
||||||
script = _generate_pdfjs_script(url)
|
|
||||||
html_page = viewer.replace('</body>',
|
|
||||||
'</body><script>{}</script>'.format(script))
|
|
||||||
return html_page
|
|
||||||
|
|
||||||
|
|
||||||
def _generate_pdfjs_script(url):
|
|
||||||
"""Generate the script that shows the pdf with pdf.js.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
url: The url of the pdf page as QUrl.
|
|
||||||
"""
|
|
||||||
return (
|
|
||||||
'document.addEventListener("DOMContentLoaded", function() {{\n'
|
|
||||||
' PDFJS.verbosity = PDFJS.VERBOSITY_LEVELS.info;\n'
|
|
||||||
' (window.PDFView || window.PDFViewerApplication).open("{url}");\n'
|
|
||||||
'}});\n'
|
|
||||||
).format(url=javascript.string_escape(url.toString(QUrl.FullyEncoded)))
|
|
||||||
|
|
||||||
|
|
||||||
def fix_urls(asset):
|
def fix_urls(asset):
|
||||||
"""Take an html page and replace each relative URL with an absolute.
|
"""Take an html page and replace each relative URL with an absolute.
|
||||||
|
|
||||||
@ -220,7 +187,7 @@ def should_use_pdfjs(mimetype):
|
|||||||
|
|
||||||
def get_main_url(filename):
|
def get_main_url(filename):
|
||||||
"""Get the URL to be opened to view a local PDF."""
|
"""Get the URL to be opened to view a local PDF."""
|
||||||
url = QUrl('qute://pdfjs')
|
url = QUrl('qute://pdfjs/web/viewer.html')
|
||||||
query = QUrlQuery()
|
query = QUrlQuery()
|
||||||
query.addQueryItem('file', QUrl.fromLocalFile(filename).toString())
|
query.addQueryItem('file', QUrl.fromLocalFile(filename).toString())
|
||||||
url.setQuery(query)
|
url.setQuery(query)
|
||||||
|
@ -523,11 +523,6 @@ def qute_pastebin_version(_url):
|
|||||||
@add_handler('pdfjs')
|
@add_handler('pdfjs')
|
||||||
def qute_pdfjs(url):
|
def qute_pdfjs(url):
|
||||||
"""Handler for qute://pdfjs. Return the pdf.js viewer."""
|
"""Handler for qute://pdfjs. Return the pdf.js viewer."""
|
||||||
if url.path() == '/':
|
|
||||||
filepath = QUrlQuery(url).queryItemValue("file")
|
|
||||||
data = pdfjs.generate_pdfjs_page(QUrl.fromLocalFile(filepath))
|
|
||||||
return 'text/html', data
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = pdfjs.get_pdfjs_res(url.path())
|
data = pdfjs.get_pdfjs_res(url.path())
|
||||||
except pdfjs.PDFJSNotFound as e:
|
except pdfjs.PDFJSNotFound as e:
|
||||||
|
@ -25,24 +25,6 @@ from PyQt5.QtCore import QUrl
|
|||||||
from qutebrowser.browser import pdfjs
|
from qutebrowser.browser import pdfjs
|
||||||
|
|
||||||
|
|
||||||
# Note that we got double protection, once because we use QUrl.FullyEncoded and
|
|
||||||
# because we use qutebrowser.utils.javascript.string_escape. Characters
|
|
||||||
# like " are already replaced by QUrl.
|
|
||||||
@pytest.mark.parametrize('url, expected', [
|
|
||||||
('http://foo.bar', "http://foo.bar"),
|
|
||||||
('http://"', ''),
|
|
||||||
('\0', '%00'),
|
|
||||||
('http://foobar/");alert("attack!");',
|
|
||||||
'http://foobar/%22);alert(%22attack!%22);'),
|
|
||||||
])
|
|
||||||
def test_generate_pdfjs_script(url, expected):
|
|
||||||
expected_open = 'open("{}");'.format(expected)
|
|
||||||
url = QUrl(url)
|
|
||||||
actual = pdfjs._generate_pdfjs_script(url)
|
|
||||||
assert expected_open in actual
|
|
||||||
assert 'PDFView' in actual
|
|
||||||
|
|
||||||
|
|
||||||
def test_fix_urls():
|
def test_fix_urls():
|
||||||
page = textwrap.dedent("""
|
page = textwrap.dedent("""
|
||||||
<html>
|
<html>
|
||||||
|
Loading…
Reference in New Issue
Block a user