问题
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.
来源:https://stackoverflow.com/questions/60701095/permissions-for-modifying-dom-in-chrome-extension-context-menu