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?