Add `Promise.withResolvers()` polyfill for pdf.js

It's needed for both the main page (which we generate using jinja) and
the worker script.

Closes: https://github.com/qutebrowser/qutebrowser/issues/8170
This commit is contained in:
toofar 2024-05-15 18:18:49 +12:00
parent 1f2a0016b7
commit 48d57cd5c8
1 changed files with 26 additions and 1 deletions

View File

@ -69,6 +69,21 @@ def generate_pdfjs_page(filename, url):
return html
def _generate_polyfills():
return """
if (typeof Promise.withResolvers === 'undefined') {
Promise.withResolvers = function () {
let resolve, reject
const promise = new Promise((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}
}
"""
def _generate_pdfjs_script(filename):
"""Generate the script that shows the pdf with pdf.js.
@ -83,6 +98,8 @@ def _generate_pdfjs_script(filename):
js_url = javascript.to_js(url.toString(urlutils.FormatOption.ENCODED))
return jinja.js_environment.from_string("""
{{ polyfills }}
document.addEventListener("DOMContentLoaded", function() {
if (typeof window.PDFJS !== 'undefined') {
// v1.x
@ -104,7 +121,7 @@ def _generate_pdfjs_script(filename):
});
}
});
""").render(url=js_url)
""").render(url=js_url, polyfills=_generate_polyfills())
def get_pdfjs_res_and_path(path):
@ -148,6 +165,14 @@ def get_pdfjs_res_and_path(path):
log.misc.warning("OSError while reading PDF.js file: {}".format(e))
raise PDFJSNotFound(path) from None
if path == "build/pdf.worker.mjs":
content = b"\n".join(
[
_generate_polyfills().encode("ascii"),
content,
]
)
return content, file_path