Suhail Doshi
Suhail Doshi

Reputation: 776

Run a Chrome Extension on a PDF Viewer page

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.

Upvotes: 1

Views: 3114

Answers (1)

Nathan
Nathan

Reputation: 21

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.

Upvotes: 2

Related Questions