Giter Site home page Giter Site logo

Comments (7)

The-Compiler avatar The-Compiler commented on June 19, 2024 2

Looks like it's API that was added only quite recently in Chromium 119: JavaScript built-in: Promise: withResolvers | Can I use... Support tables for HTML5, CSS3, etc

But Qt 6.7 is based on 118, which is why it's not available.

Looks like it's easy to polyfill:

So we should probably do that for our PDF.js glue code.

from qutebrowser.

The-Compiler avatar The-Compiler commented on June 19, 2024 2

On Linux, we don't have a choice what people have installed OS-wide. I'm not too worried about older Qt versions (chances are distributions with older Qt have an older PDF.js too), but the newest QtWebEngine should work with the newest PDF.js default build.

from qutebrowser.

The-Compiler avatar The-Compiler commented on June 19, 2024 1

With

diff --git i/qutebrowser/browser/pdfjs.py w/qutebrowser/browser/pdfjs.py
index 841285deb..cf06f9aff 100644
--- i/qutebrowser/browser/pdfjs.py
+++ w/qutebrowser/browser/pdfjs.py
@@ -83,6 +83,17 @@ def _generate_pdfjs_script(filename):
     js_url = javascript.to_js(url.toString(urlutils.FormatOption.ENCODED))
 
     return jinja.js_environment.from_string("""
+        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 }
+            }
+        }
+
         document.addEventListener("DOMContentLoaded", function() {
             if (typeof window.PDFJS !== 'undefined') {
                 // v1.x

The same issue pops up in qute://pdfjs/build/pdf.worker.mjs instead... I suppose we could patch that on a file-level, but this will probably get a bit more hacky than I thought it would be...

As a workaround, switching to the legacy PDF.js build (pdfjs-legacy on Arch) fixes things.

from qutebrowser.

toofar avatar toofar commented on June 19, 2024

You can manipulate the files in flight in qutescheme as well. Inspried by greasemonky scripts that hook fetch() so they can inject code into workers like this.

diff --git i/qutebrowser/browser/qutescheme.py w/qutebrowser/browser/qutescheme.py
index 508d510d706e..b38fc1e98a94 100644
--- i/qutebrowser/browser/qutescheme.py
+++ w/qutebrowser/browser/qutescheme.py
@@ -553,6 +553,21 @@ def qute_pdfjs(url: QUrl) -> _HandlerRet:
             "pdfjs resource requested but not found: {}".format(e.path))
         raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path))
     mimetype = utils.guess_mimetype(url.fileName(), fallback=True)
+
+    if url.path() == '/build/pdf.worker.mjs':
+        data = b"""
+        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 }
+            }
+        }
+
+        """ + data
     return mimetype, data

from qutebrowser.

toofar avatar toofar commented on June 19, 2024

The last two issues with PDF.js have resulted in JS error logs. It looks like these tests would have caught that but it looks like they are skipped in CI. I'm guessing because it has no PDF.js installed?

What do you think about

  1. installing pdfjs in the docker containers
  2. installing libjs-pdf on ubuntu runners*
  3. running scripts/dev/update_3rdparty.py on the windows runner
  4. running scripts/dev/update_3rdparty.py on the bleeding edge CI?

*: debuntu ships a broken pdfjs package it seems, so we get file not found errors for files required for pdf.js, like tooltip images and locale files. Not sure which direction to go on this front, see toofar@afc9c87 for some words.

(Also here is a patch with the polyfill above in a common function.)
diff --git i/qutebrowser/browser/pdfjs.py w/qutebrowser/browser/pdfjs.py
index 841285deb83a..4fca7e81dddd 100644
--- i/qutebrowser/browser/pdfjs.py
+++ w/qutebrowser/browser/pdfjs.py
@@ -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,9 @@ 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 += _generate_polyfills().encode("ascii")
+
     return content, file_path

from qutebrowser.

toofar avatar toofar commented on June 19, 2024

Okay, should have a PR up tomorrow. The tests for Qt < 6.5 are failing for some different "new javascript feature" reason and all the ubuntu ones are anyway failing because of #7904. So to get some pdfjs test running on CI but without having to deal with all that breakage my current plan is to install pdfjs in the archlinux based qt6 docker containers and install it from source in the bleeding edge jobs. (And maybe in the windows tests, dunno, seems surplus to requirements.)

Edit: having fun wrangling tests and test environments and CI setups. Might be green now. ๐Ÿ˜ด

from qutebrowser.

bodograumann avatar bodograumann commented on June 19, 2024

Sounds like we should really be using the pdfjs legacy build, instead of introducing moer and more workarounds / polyfills!?

from qutebrowser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.