0

I have intermediate knowledge of chrome extension development.

Context:

  • Manifest has activeTab and contextMenus permissions.
  • Clicking context menu item should inject content into DOM
  • This works fine for regular web pages, but fails on default Chrome PDF viewer with the following error:
Cannot access contents of the page. Extension manifest must request permission to access the respective host.

However, if I inject the content first using a key command, then modify that content in the context menu action, it works correctly.

SO,

Why is the context menu action unable to add a DOM node in a PDF page (when regular page works)

And why does adding the DOM node using key command, then modifying it using the context menu action work?

EDIT: The code that isn't working when using the context menu action:

injected-content.js

let wrapper = document.getElementById(SUTRA_ELEMENT)

if (!wrapper) {
  wrapper = document.createElement('div')
  wrapper.id = SUTRA_ELEMENT
  document.body.prepend(wrapper)
}

// modify wrapper

That results in the above error when run via context menu action on a Chrome PDF viewer. However it works correctly when run through a key command. After running it via a key command, running via context menu action allows modification of already prepended element.

sgg
  • 179
  • 8
  • The built-in PDF viewer is a browser UI page which is handled by a built-in *component extension* shown inside an iframe (devtools kinda hides it) so you can't modify its contents. You can modify the contents of the main document in the tab. If you need more info, show the code. – wOxxOm Mar 16 '20 at 06:20
  • So I am able to inject content successfully using a key command (it runs the same code as the context menu action). So that part works correctly. It's simply that the context menu doesn't pick up the activeTab permission for some reason. – sgg Mar 16 '20 at 06:22
  • Ah, I see it in the link you gave. All I wanted is for you to put those parts in the question so I don't have to study your entire background.js... – wOxxOm Mar 16 '20 at 06:42
  • 1
    So the difference is that `commands` API grants permissions for the main document of the tab where you can inject stuff, but `contextMenus` activates the frame where the user invoked the menu, and that frame is the *component extension* that shows the PDF, you can't modify it. You can file a bug report on https://crbug.com. – wOxxOm Mar 16 '20 at 06:43
  • Yeah the actual code is basically in 5 different places, so I didn't know if it would make sense for me to weave you through those or just give you the code :) Your explanation makes sense but how come I'm able to access the element created by `commands` from within the PDF frame with `contextMenus`? – sgg Mar 16 '20 at 06:53
  • Once `commands` grants permissions for the main tab, your subsequently injected content scripts can use it. – wOxxOm Mar 16 '20 at 06:54
  • Ah got it, thanks. – sgg Mar 16 '20 at 06:55

0 Answers0