Permissions for modifying DOM in Chrome extension context menu

梦想的初衷 提交于 2021-02-11 14:41:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!