{"action":"created","comment":{"url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/comments/46937788","id":46937788,"diff_hunk":"@@ -0,0 +1,151 @@\n+# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:\n+\n+# Copyright 2015 Daniel Schadt\n+#\n+# This file is part of qutebrowser.\n+#\n+# qutebrowser is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# qutebrowser is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.\n+\n+\"\"\"pdf.js integration for qutebrowser.\"\"\"\n+\n+import os\n+\n+from PyQt5.QtCore import QUrl\n+\n+from qutebrowser.browser import webelem\n+from qutebrowser.utils import utils\n+\n+\n+class PDFJSNotFound(Exception):\n+\n+    \"\"\"Raised when no pdf.js installation is found.\"\"\"\n+\n+    pass\n+\n+\n+def generate_pdfjs_page(url):\n+    \"\"\"Return the html content of a page that displays url with pdfjs.\n+\n+    Returns a string.\n+\n+    Args:\n+        url: The url of the pdf as QUrl.\n+    \"\"\"\n+    viewer = get_pdfjs_res('web/viewer.html').decode('utf-8')\n+    script = _generate_pdfjs_script(url)\n+    html_page = viewer.replace(\n+        '</body>', '</body><script>{}</script>'.format(script)\n+    )\n+    return html_page\n+\n+\n+def _generate_pdfjs_script(url):\n+    \"\"\"Generate the script that shows the pdf with pdf.js.\n+\n+    Args:\n+        url: The url of the pdf page as QUrl.\n+    \"\"\"\n+    return (\n+        'PDFView.open(\"{url}\");\\n'\n+    ).format(url=webelem.javascript_escape(url.toString(QUrl.FullyEncoded)))\n+\n+\n+def fix_urls(asset):\n+    \"\"\"Take a html page and replace each relative URL wth an absolute.\n+\n+    This is specialized for pdf.js files and not a general purpose function.\n+\n+    Args:\n+        asset: js file or html page as string.\n+    \"\"\"\n+    new_urls = {\n+        'viewer.css': 'qute://pdfjs/web/viewer.css',\n+        'compatibility.js': 'qute://pdfjs/web/compatibility.js',\n+        'locale/locale.properties':\n+            'qute://pdfjs/web/locale/locale.properties',\n+        'l10n.js': 'qute://pdfjs/web/l10n.js',\n+        '../build/pdf.js': 'qute://pdfjs/build/pdf.js',\n+        'debugger.js': 'qute://pdfjs/web/debugger.js',\n+        'viewer.js': 'qute://pdfjs/web/viewer.js',\n+        'compressed.tracemonkey-pldi-09.pdf': '',\n+        './images/': 'qute://pdfjs/web/images/',\n+        '../build/pdf.worker.js': 'qute://pdfjs/build/pdf.worker.js',\n+        '../web/cmaps/': 'qute://pdfjs/web/cmaps/',\n+    }\n+    for original, new in new_urls.items():\n+        asset = asset.replace(original, new)\n+    return asset\n+\n+\n+SYSTEM_PDFJS_PATHS = [\n+    '/usr/share/pdf.js/',  # Debian pdf.js-common\n+    '/usr/share/javascript/pdf/',  # Debian libjs-pdf\n+    os.path.expanduser('~/.local/share/qutebrowser/pdfjs/'),  # fallback\n+]\n+\n+\n+def get_pdfjs_res(path):\n+    \"\"\"Get a pdf.js resource in binary format.\n+\n+    Args:\n+        path: The path inside the pdfjs directory.\n+    \"\"\"\n+    path = path.lstrip('/')\n+    content = None\n+\n+    # First try a system wide installation\n+    # System installations might strip off the 'build/' or 'web/' prefixes.\n+    # qute expects them, so we need to adjust for it.\n+    names_to_try = [path, path[path.index('/') + 1:]]","path":"qutebrowser/browser/pdfjs.py","position":110,"original_position":110,"commit_id":"e17a9a9ce5c21faa33e826391c6d2292b7fd44d3","original_commit_id":"e17a9a9ce5c21faa33e826391c6d2292b7fd44d3","user":{"login":"The-Compiler","id":625793,"avatar_url":"https://avatars.githubusercontent.com/u/625793?v=3","gravatar_id":"","url":"https://api.github.com/users/The-Compiler","html_url":"https://github.com/The-Compiler","followers_url":"https://api.github.com/users/The-Compiler/followers","following_url":"https://api.github.com/users/The-Compiler/following{/other_user}","gists_url":"https://api.github.com/users/The-Compiler/gists{/gist_id}","starred_url":"https://api.github.com/users/The-Compiler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/The-Compiler/subscriptions","organizations_url":"https://api.github.com/users/The-Compiler/orgs","repos_url":"https://api.github.com/users/The-Compiler/repos","events_url":"https://api.github.com/users/The-Compiler/events{/privacy}","received_events_url":"https://api.github.com/users/The-Compiler/received_events","type":"User","site_admin":false},"body":"I'd prefer something like `path.split('/', maxsplit=1)[1:])` to remove the prefix - makes it clearer to see what's going on, IMHO. That'll raise an `IndexError` if path has no `/` in it - but I think that won't happen?","created_at":"2015-12-08T10:39:58Z","updated_at":"2015-12-08T10:39:58Z","html_url":"https://github.com/The-Compiler/qutebrowser/pull/1112#discussion_r46937788","pull_request_url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112","_links":{"self":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/comments/46937788"},"html":{"href":"https://github.com/The-Compiler/qutebrowser/pull/1112#discussion_r46937788"},"pull_request":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112"}}},"pull_request":{"url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112","id":50569270,"html_url":"https://github.com/The-Compiler/qutebrowser/pull/1112","diff_url":"https://github.com/The-Compiler/qutebrowser/pull/1112.diff","patch_url":"https://github.com/The-Compiler/qutebrowser/pull/1112.patch","issue_url":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/1112","number":1112,"state":"open","locked":false,"title":"Add pdf.js support","user":{"login":"Kingdread","id":739796,"avatar_url":"https://avatars.githubusercontent.com/u/739796?v=3","gravatar_id":"","url":"https://api.github.com/users/Kingdread","html_url":"https://github.com/Kingdread","followers_url":"https://api.github.com/users/Kingdread/followers","following_url":"https://api.github.com/users/Kingdread/following{/other_user}","gists_url":"https://api.github.com/users/Kingdread/gists{/gist_id}","starred_url":"https://api.github.com/users/Kingdread/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kingdread/subscriptions","organizations_url":"https://api.github.com/users/Kingdread/orgs","repos_url":"https://api.github.com/users/Kingdread/repos","events_url":"https://api.github.com/users/Kingdread/events{/privacy}","received_events_url":"https://api.github.com/users/Kingdread/received_events","type":"User","site_admin":false},"body":"Related issue: #207 \r\n\r\nI'm bypassing the same-domain restriction by generating a HTML file and embedding the PDF there in a JS array.\r\n\r\nThe viewer can be toggled with the setting `content -> enable-pdfjs`.\r\n\r\nWorks here, though I'm not that confident if I did everything right in the \"include files in package\"-stuff :wink:\r\n\r\n(The commits are a bit messy, though I can squash them if you want)","created_at":"2015-11-12T21:19:09Z","updated_at":"2015-12-08T10:39:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"76b5dfd291d0c3d18e2e169e8361795ed06b8e57","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112/commits","review_comments_url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112/comments","review_comment_url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/comments{/number}","comments_url":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/1112/comments","statuses_url":"https://api.github.com/repos/The-Compiler/qutebrowser/statuses/e17a9a9ce5c21faa33e826391c6d2292b7fd44d3","head":{"label":"Kingdread:pdfjs","ref":"pdfjs","sha":"e17a9a9ce5c21faa33e826391c6d2292b7fd44d3","user":{"login":"Kingdread","id":739796,"avatar_url":"https://avatars.githubusercontent.com/u/739796?v=3","gravatar_id":"","url":"https://api.github.com/users/Kingdread","html_url":"https://github.com/Kingdread","followers_url":"https://api.github.com/users/Kingdread/followers","following_url":"https://api.github.com/users/Kingdread/following{/other_user}","gists_url":"https://api.github.com/users/Kingdread/gists{/gist_id}","starred_url":"https://api.github.com/users/Kingdread/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kingdread/subscriptions","organizations_url":"https://api.github.com/users/Kingdread/orgs","repos_url":"https://api.github.com/users/Kingdread/repos","events_url":"https://api.github.com/users/Kingdread/events{/privacy}","received_events_url":"https://api.github.com/users/Kingdread/received_events","type":"User","site_admin":false},"repo":{"id":40366648,"name":"qutebrowser","full_name":"Kingdread/qutebrowser","owner":{"login":"Kingdread","id":739796,"avatar_url":"https://avatars.githubusercontent.com/u/739796?v=3","gravatar_id":"","url":"https://api.github.com/users/Kingdread","html_url":"https://github.com/Kingdread","followers_url":"https://api.github.com/users/Kingdread/followers","following_url":"https://api.github.com/users/Kingdread/following{/other_user}","gists_url":"https://api.github.com/users/Kingdread/gists{/gist_id}","starred_url":"https://api.github.com/users/Kingdread/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kingdread/subscriptions","organizations_url":"https://api.github.com/users/Kingdread/orgs","repos_url":"https://api.github.com/users/Kingdread/repos","events_url":"https://api.github.com/users/Kingdread/events{/privacy}","received_events_url":"https://api.github.com/users/Kingdread/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Kingdread/qutebrowser","description":"A keyboard-driven, vim-like browser based on PyQt5 and QtWebKit.","fork":true,"url":"https://api.github.com/repos/Kingdread/qutebrowser","forks_url":"https://api.github.com/repos/Kingdread/qutebrowser/forks","keys_url":"https://api.github.com/repos/Kingdread/qutebrowser/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Kingdread/qutebrowser/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Kingdread/qutebrowser/teams","hooks_url":"https://api.github.com/repos/Kingdread/qutebrowser/hooks","issue_events_url":"https://api.github.com/repos/Kingdread/qutebrowser/issues/events{/number}","events_url":"https://api.github.com/repos/Kingdread/qutebrowser/events","assignees_url":"https://api.github.com/repos/Kingdread/qutebrowser/assignees{/user}","branches_url":"https://api.github.com/repos/Kingdread/qutebrowser/branches{/branch}","tags_url":"https://api.github.com/repos/Kingdread/qutebrowser/tags","blobs_url":"https://api.github.com/repos/Kingdread/qutebrowser/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Kingdread/qutebrowser/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Kingdread/qutebrowser/git/refs{/sha}","trees_url":"https://api.github.com/repos/Kingdread/qutebrowser/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Kingdread/qutebrowser/statuses/{sha}","languages_url":"https://api.github.com/repos/Kingdread/qutebrowser/languages","stargazers_url":"https://api.github.com/repos/Kingdread/qutebrowser/stargazers","contributors_url":"https://api.github.com/repos/Kingdread/qutebrowser/contributors","subscribers_url":"https://api.github.com/repos/Kingdread/qutebrowser/subscribers","subscription_url":"https://api.github.com/repos/Kingdread/qutebrowser/subscription","commits_url":"https://api.github.com/repos/Kingdread/qutebrowser/commits{/sha}","git_commits_url":"https://api.github.com/repos/Kingdread/qutebrowser/git/commits{/sha}","comments_url":"https://api.github.com/repos/Kingdread/qutebrowser/comments{/number}","issue_comment_url":"https://api.github.com/repos/Kingdread/qutebrowser/issues/comments{/number}","contents_url":"https://api.github.com/repos/Kingdread/qutebrowser/contents/{+path}","compare_url":"https://api.github.com/repos/Kingdread/qutebrowser/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Kingdread/qutebrowser/merges","archive_url":"https://api.github.com/repos/Kingdread/qutebrowser/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Kingdread/qutebrowser/downloads","issues_url":"https://api.github.com/repos/Kingdread/qutebrowser/issues{/number}","pulls_url":"https://api.github.com/repos/Kingdread/qutebrowser/pulls{/number}","milestones_url":"https://api.github.com/repos/Kingdread/qutebrowser/milestones{/number}","notifications_url":"https://api.github.com/repos/Kingdread/qutebrowser/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Kingdread/qutebrowser/labels{/name}","releases_url":"https://api.github.com/repos/Kingdread/qutebrowser/releases{/id}","created_at":"2015-08-07T15:15:32Z","updated_at":"2015-08-07T15:15:33Z","pushed_at":"2015-12-03T21:51:46Z","git_url":"git://github.com/Kingdread/qutebrowser.git","ssh_url":"git@github.com:Kingdread/qutebrowser.git","clone_url":"https://github.com/Kingdread/qutebrowser.git","svn_url":"https://github.com/Kingdread/qutebrowser","homepage":null,"size":11843,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"The-Compiler:master","ref":"master","sha":"40c7990d3ae018de920c1c7a0a77ee43843240e4","user":{"login":"The-Compiler","id":625793,"avatar_url":"https://avatars.githubusercontent.com/u/625793?v=3","gravatar_id":"","url":"https://api.github.com/users/The-Compiler","html_url":"https://github.com/The-Compiler","followers_url":"https://api.github.com/users/The-Compiler/followers","following_url":"https://api.github.com/users/The-Compiler/following{/other_user}","gists_url":"https://api.github.com/users/The-Compiler/gists{/gist_id}","starred_url":"https://api.github.com/users/The-Compiler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/The-Compiler/subscriptions","organizations_url":"https://api.github.com/users/The-Compiler/orgs","repos_url":"https://api.github.com/users/The-Compiler/repos","events_url":"https://api.github.com/users/The-Compiler/events{/privacy}","received_events_url":"https://api.github.com/users/The-Compiler/received_events","type":"User","site_admin":false},"repo":{"id":21244505,"name":"qutebrowser","full_name":"The-Compiler/qutebrowser","owner":{"login":"The-Compiler","id":625793,"avatar_url":"https://avatars.githubusercontent.com/u/625793?v=3","gravatar_id":"","url":"https://api.github.com/users/The-Compiler","html_url":"https://github.com/The-Compiler","followers_url":"https://api.github.com/users/The-Compiler/followers","following_url":"https://api.github.com/users/The-Compiler/following{/other_user}","gists_url":"https://api.github.com/users/The-Compiler/gists{/gist_id}","starred_url":"https://api.github.com/users/The-Compiler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/The-Compiler/subscriptions","organizations_url":"https://api.github.com/users/The-Compiler/orgs","repos_url":"https://api.github.com/users/The-Compiler/repos","events_url":"https://api.github.com/users/The-Compiler/events{/privacy}","received_events_url":"https://api.github.com/users/The-Compiler/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/The-Compiler/qutebrowser","description":"A keyboard-driven, vim-like browser based on PyQt5 and QtWebKit.","fork":false,"url":"https://api.github.com/repos/The-Compiler/qutebrowser","forks_url":"https://api.github.com/repos/The-Compiler/qutebrowser/forks","keys_url":"https://api.github.com/repos/The-Compiler/qutebrowser/keys{/key_id}","collaborators_url":"https://api.github.com/repos/The-Compiler/qutebrowser/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/The-Compiler/qutebrowser/teams","hooks_url":"https://api.github.com/repos/The-Compiler/qutebrowser/hooks","issue_events_url":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/events{/number}","events_url":"https://api.github.com/repos/The-Compiler/qutebrowser/events","assignees_url":"https://api.github.com/repos/The-Compiler/qutebrowser/assignees{/user}","branches_url":"https://api.github.com/repos/The-Compiler/qutebrowser/branches{/branch}","tags_url":"https://api.github.com/repos/The-Compiler/qutebrowser/tags","blobs_url":"https://api.github.com/repos/The-Compiler/qutebrowser/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/The-Compiler/qutebrowser/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/The-Compiler/qutebrowser/git/refs{/sha}","trees_url":"https://api.github.com/repos/The-Compiler/qutebrowser/git/trees{/sha}","statuses_url":"https://api.github.com/repos/The-Compiler/qutebrowser/statuses/{sha}","languages_url":"https://api.github.com/repos/The-Compiler/qutebrowser/languages","stargazers_url":"https://api.github.com/repos/The-Compiler/qutebrowser/stargazers","contributors_url":"https://api.github.com/repos/The-Compiler/qutebrowser/contributors","subscribers_url":"https://api.github.com/repos/The-Compiler/qutebrowser/subscribers","subscription_url":"https://api.github.com/repos/The-Compiler/qutebrowser/subscription","commits_url":"https://api.github.com/repos/The-Compiler/qutebrowser/commits{/sha}","git_commits_url":"https://api.github.com/repos/The-Compiler/qutebrowser/git/commits{/sha}","comments_url":"https://api.github.com/repos/The-Compiler/qutebrowser/comments{/number}","issue_comment_url":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/comments{/number}","contents_url":"https://api.github.com/repos/The-Compiler/qutebrowser/contents/{+path}","compare_url":"https://api.github.com/repos/The-Compiler/qutebrowser/compare/{base}...{head}","merges_url":"https://api.github.com/repos/The-Compiler/qutebrowser/merges","archive_url":"https://api.github.com/repos/The-Compiler/qutebrowser/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/The-Compiler/qutebrowser/downloads","issues_url":"https://api.github.com/repos/The-Compiler/qutebrowser/issues{/number}","pulls_url":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls{/number}","milestones_url":"https://api.github.com/repos/The-Compiler/qutebrowser/milestones{/number}","notifications_url":"https://api.github.com/repos/The-Compiler/qutebrowser/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/The-Compiler/qutebrowser/labels{/name}","releases_url":"https://api.github.com/repos/The-Compiler/qutebrowser/releases{/id}","created_at":"2014-06-26T14:59:34Z","updated_at":"2015-12-07T16:18:34Z","pushed_at":"2015-12-08T09:42:56Z","git_url":"git://github.com/The-Compiler/qutebrowser.git","ssh_url":"git@github.com:The-Compiler/qutebrowser.git","clone_url":"https://github.com/The-Compiler/qutebrowser.git","svn_url":"https://github.com/The-Compiler/qutebrowser","homepage":null,"size":12417,"stargazers_count":815,"watchers_count":815,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":122,"mirror_url":null,"open_issues_count":340,"forks":122,"open_issues":340,"watchers":815,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112"},"html":{"href":"https://github.com/The-Compiler/qutebrowser/pull/1112"},"issue":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/1112"},"comments":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/issues/1112/comments"},"review_comments":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112/comments"},"review_comment":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/pulls/1112/commits"},"statuses":{"href":"https://api.github.com/repos/The-Compiler/qutebrowser/statuses/e17a9a9ce5c21faa33e826391c6d2292b7fd44d3"}}}}