1

If you use Chrome and go to this page: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf I am wondering if you can run your Chrome Extension on that page.

I'd be fine with either content_script or background.js working.

when I console.log on mouse movements or just when content_script is usually injected, nothing shows up.

I noticed this post Run a Chrome Plugin On a PDF Page but it, unfortunately, didn't work for me.

This is my manifest.json:

{
  "name": "Extension",
  "description": "Fancy stuff",
  "version": "1.0.0",
  "manifest_version": 2,
  "permissions": [
    "https://*/*",
    "http://*/*",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "webNavigation",
    "management",
    "sessions",
    "<all_urls>",
    "activeTab"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*" ],
      "js": ["content_script.js"],
      "match_about_blank": true, // Needed for docs.google.com. Dunno why!
      "all_frames": true,
      "run_at": "document_start"
    },
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["document_idle.js"],
      "match_about_blank": true, // Needed for docs.google.com. Dunno why!
      "all_frames": true,
      "run_at": "document_idle"
    }
  ]
}

Right now, I have a console.log('hi') in content_script.js but I don't see the output of that when I open inspector which is leading me to believe it never runs.

Suhail Doshi
  • 716
  • 1
  • 10
  • 23
  • Assuming you're using the built-in PDF viewer, not a third-party extension, it's certainly possible to run content scripts on pdf files in Chrome so there must be something wrong in your code. However note that the actual contents of the PDF is shown inside an `` element that runs an internal plugin so naturally it won't bubble the mouse events in the main document. For that you'll have to use a transparent div over the entire page, but that would break the functionality of the viewer. – wOxxOm May 27 '20 at 09:33
  • Right now, I have a `console.log('hi')` in `content_script.js` but I don't see the output of that when I open inspector which is leading me to believe it never runs. – Suhail Doshi May 27 '20 at 15:54
  • There must be something else at play in your case. For example, as I already suggested maybe you're using a custom viewer. There could be other reasons but I don't like guessing: the proper method is debugging i.e. try in a new profile without extensions, try to remove all other parts from your extension, try an older/newer version of the browser, and so on. Also, console output may be cleared by the page or an extension so don't rely on it. Use devtools to inspect your content script code and set breakpoints. – wOxxOm May 27 '20 at 16:57
  • 1
    I talked to someone in Chrome Extension dev rel and they said this wasn't possible. I quote: "As for your PDF question, no, you can't inject in that because the viewer itself is an extension. You might be able to do something with the Chrome DevTools Protocol, but at that point you're way off in the weeds and YMMV." – Suhail Doshi May 27 '20 at 17:01
  • They simply didn't understand what you were asking, they're not really good with understanding normal people as I observed over the years. They thought you want to inject into the actual plugin which is not possible. The tab itself where the pdf is opened is perfectly injectable. – wOxxOm May 27 '20 at 17:04
  • Here, I even ran a quick test in Violentmonkey on your pdf url https://puu.sh/FPuoq/0e0883521f.png – wOxxOm May 27 '20 at 17:09

1 Answers1

2

It ran but it did not run on the Chrome internal PDF viewer, which is a chrome extension.

When you open the DevTools by Right-click -> Inspect, the DevTools is not looking at the PDF file but a embed element of chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html. Therefore, you didn't see the output of console.log('hi')

The content_script.js ran on a different context, you can see the console.log output by open the DevTools by pressing F12 in Windows or from More tools, it is the context of your injected content_script.js

you can check the context difference by execute:

window.location.href

on both Consoles.

And since there is no way to run content_script.js on the chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html, I don't think you can manipulate the content of the pdf using the default Chrome PDF viewer.

Nathan
  • 21
  • 1