Repository: qutebrowser/qutebrowser

Issue:
pdfjs v1.6.210 does not work
pdfjs v1.6.210 was [released](https://github.com/mozilla/pdf.js/releases/tag/v1.6.210) and seems to break in qutebrowser.

When using it we simply get a grey pdfjs UI, and various tests fail when waiting for `[qute://pdfjs/*] PDF * (PDF.js: *)` in the log:

```
FAIL tests/end2end/features/test_downloads_bdd.py::test_downloading_via_pdfjs
FAIL tests/end2end/features/test_misc_bdd.py::test_pdfjs_is_used_for_pdf_files
FAIL tests/end2end/features/test_misc_bdd.py::test_downloading_a_pdf_via_pdfjs_button_issue_1214
```

Log output:

```
08:47:52.242 DEBUG    webview    tabbedbrowser:on_title_changed:500 Changing title for idx 0 to 'PDF.js viewer'
08:47:52.244 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/viewer.css, path: /web/viewer.css, host pdfjs
08:47:52.261 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/compatibility.js, path: /web/compatibility.js, host pdfjs
08:47:52.263 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/l10n.js, path: /web/l10n.js, host pdfjs
08:47:52.264 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/build/pdf.js, path: /build/pdf.js, host pdfjs
08:47:52.269 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/viewer.js, path: /web/viewer.js, host pdfjs
08:47:52.355 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/texture.png, path: /web/images/texture.png, host pdfjs
08:47:52.356 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-viewThumbnail.png, path: /web/images/toolbarButton-viewThumbnail.png, host pdfjs
08:47:52.357 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-viewOutline.png, path: /web/images/toolbarButton-viewOutline.png, host pdfjs
08:47:52.359 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-viewAttachments.png, path: /web/images/toolbarButton-viewAttachments.png, host pdfjs
08:47:52.361 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-sidebarToggle.png, path: /web/images/toolbarButton-sidebarToggle.png, host pdfjs
08:47:52.362 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-search.png, path: /web/images/toolbarButton-search.png, host pdfjs
08:47:52.363 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-pageUp.png, path: /web/images/toolbarButton-pageUp.png, host pdfjs
08:47:52.364 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-pageDown.png, path: /web/images/toolbarButton-pageDown.png, host pdfjs
08:47:52.370 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-presentationMode.png, path: /web/images/toolbarButton-presentationMode.png, host pdfjs
08:47:52.372 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-openFile.png, path: /web/images/toolbarButton-openFile.png, host pdfjs
08:47:52.373 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-print.png, path: /web/images/toolbarButton-print.png, host pdfjs
08:47:52.374 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-download.png, path: /web/images/toolbarButton-download.png, host pdfjs
08:47:52.375 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-bookmark.png, path: /web/images/toolbarButton-bookmark.png, host pdfjs
08:47:52.376 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-secondaryToolbarToggle.png, path: /web/images/toolbarButton-secondaryToolbarToggle.png, host pdfjs
08:47:52.377 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-zoomOut.png, path: /web/images/toolbarButton-zoomOut.png, host pdfjs
08:47:52.378 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-zoomIn.png, path: /web/images/toolbarButton-zoomIn.png, host pdfjs
08:47:52.379 DEBUG    misc       qutescheme:data_for_url:123 url: qute://pdfjs/web/images/toolbarButton-menuArrows.png, path: /web/images/toolbarButton-menuArrows.png, host pdfjs
08:47:52.390 DEBUG    js         webpage:javaScriptConsoleMessage:505 [http://localhost:41807/data/misc/test.pdf:422] ReferenceError: Can't find variable: PDFView
08:47:52.442 DEBUG    webview    browsertab:_set_load_status:590 load status for <qutebrowser.browser.webkit.webkittab.WebKitTab tab_id=0 url='http://localhost:41807/data/misc/test.pdf'>: LoadStatus.success
08:47:52.471 DEBUG    js         webpage:javaScriptConsoleMessage:505 [http://localhost:41807/data/misc/test.pdf:0] XMLHttpRequest cannot load qute://pdfjs/web/locale/locale.properties. Cross origin requests are only supported for HTTP.
08:47:52.472 DEBUG    js         webpage:javaScriptConsoleMessage:505 [qute://pdfjs/web/l10n.js:829] #thumbs is undefined.
08:47:52.473 DEBUG    js         webpage:javaScriptConsoleMessage:505 [qute://pdfjs/web/l10n.js:901] #thumbs is undefined.
08:47:52.473 DEBUG    js         webpage:javaScriptConsoleMessage:505 [qute://pdfjs/web/l10n.js:829] #thumbs_label is undefined.
08:47:52.473 DEBUG    js         webpage:javaScriptConsoleMessage:505 [qute://pdfjs/web/l10n.js:901] #thumbs_label is undefined.
[...]
```

Note the `ReferenceError: Can't find variable: PDFView` in there...

@Kingdread one for you?



Discussion:
Yeah, they renamed `PDFView` to `PDFViewerApplication`. I've already seen it in aeb03ab396402903a6551f7eb19710b610a5b732, but forgot that the other methods are also affected. I will apply the fix to the other pdfjs related methods and open a PR!


Resolving patch:
diff --git a/qutebrowser/browser/pdfjs.py b/qutebrowser/browser/pdfjs.py
--- a/qutebrowser/browser/pdfjs.py
+++ b/qutebrowser/browser/pdfjs.py
@@ -62,8 +62,10 @@ def _generate_pdfjs_script(url):
         url: The url of the pdf page as QUrl.
     """
     return (
-        'PDFJS.verbosity = PDFJS.VERBOSITY_LEVELS.info;\n'
-        'PDFView.open("{url}");\n'
+        '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)))
 
 


Test patch:
diff --git a/tests/unit/browser/test_pdfjs.py b/tests/unit/browser/test_pdfjs.py
--- a/tests/unit/browser/test_pdfjs.py
+++ b/tests/unit/browser/test_pdfjs.py
@@ -36,11 +36,11 @@
      'http://foobar/%22);alert(%22attack!%22);'),
 ])
 def test_generate_pdfjs_script(url, expected):
-    expected_code = ('PDFJS.verbosity = PDFJS.VERBOSITY_LEVELS.info;\n'
-                     'PDFView.open("{}");\n'.format(expected))
+    expected_open = 'open("{}");'.format(expected)
     url = QUrl(url)
     actual = pdfjs._generate_pdfjs_script(url)
-    assert actual == expected_code
+    assert expected_open in actual
+    assert 'PDFView' in actual
 
 
 def test_fix_urls():