1

I'm testing a Chrome extension that can display some information about a PDF file the user is currently viewing, ideally inserted as an iFrame. I don't want this to happen automatically, so I'm using a background script to listen for the click event, and then injecting the content script. (The extension communicates with an external server but that isn't relevant to this question.)

Here is my manifest.json:

{
  "name": "Test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Test",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Test"
  },
  "permissions": [
    "activeTab"
  ]
}

Here is background.js:

hrome.browserAction.onClicked.addListener(function (tab) {

    chrome.tabs.executeScript({'file': 'popup.js'})
    
});

And here is popup.js:

var widgetHtml = 
  '<div id="main" style="position: fixed;top: 14px;">'+
  '  <p>some stuff goes here</p>'+
  '</div>';

var iframe = document.createElement('iframe');
document.body.insertBefore(iframe,document.body.firstChild)
iframe.contentDocument.body.innerHTML = widgetHtml;

This works like a charm on non-PDF pages, but it doesn't inject anything on PDF pages. If I use the exact popup.js script in the console, it of course works fine as well.

I've read through Run a Chrome Plugin On a PDF Page, which tries to automatically detect PDFs, and this one, How to inject content script view on the PDF viewer?, but it doesn't seem to work. What am I missing?

tchaymore
  • 3,728
  • 13
  • 55
  • 86
  • 1
    This code works for me so I guess you may be using a custom PDF viewer extension. – wOxxOm Jan 30 '21 at 18:40
  • Well, that's good to know but also confusing! As far as I can tell, I haven't ever installed a PDF-related extension except this one. – tchaymore Jan 30 '21 at 18:49
  • 1
    Anyway you're not missing anything, the code is correct. – wOxxOm Jan 30 '21 at 18:58
  • Appreciate it. Looks like I'm using the default pdf-viewer.js, so not sure if I can do anything about that. – tchaymore Jan 30 '21 at 19:04
  • Chrome does not use pdf-viewer.js, it has a built-in PDF viewer in native code. – wOxxOm Jan 30 '21 at 19:05
  • Maybe I'm reading it wrong, but it looks like the same built-in extension that's referenced here: https://stackoverflow.com/questions/53866323/where-is-chrome-pdf-viewer-extension-mhjfbmdgcfjbbpaeojofohoefgiehjai-locate – tchaymore Jan 30 '21 at 19:11
  • 1
    The screenshot in that answer is of a custom extension, not the built-in PDF viewer. You can see its chrome-extension URL in the address bar. – wOxxOm Jan 30 '21 at 19:18
  • @tchaymore did you ever get this to work? I don't think wOxxOm is correct in claiming that it must be a custom extension (if so I would love to be proven wrong). The default pdf-viewer in Chromium runs as a plug-in with "mhjfbmdgcfjbbpaeojofohoefgiehjai" as the id. See [here](https://source.chromium.org/search?q=mhjfbmdgcfjbbpaeojofohoefgiehjai&ss=chromium&start=1). – jimmy May 07 '22 at 23:00

0 Answers0