Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending an iframe in ContentScript causes a Content Security Policy error. #139

Open
chengfengfengwang opened this issue Mar 16, 2024 · 1 comment

Comments

@chengfengfengwang
Copy link

// contentScript.ts
const freamUrl = chrome.runtime.getURL('src/entries/fream/index.html');
const ifream = document.createElement('iframe');
ifream.src = freamUrl;
document.body.appendChild(ifream)

The above code cause CSP error:
image

@keyding
Copy link

keyding commented Mar 31, 2024

Hi, @chengfengfengwang

You need to put your html file in the public:

public folder

Here is my sample code:

// contentScript.ts

function renderIframeContent(page: string, render: (iframe: HTMLIFrameElement) => void) {
  const iFrameRoot = document.createElement('div')
  const iFrame = document.createElement('iframe')
  iFrame.src = browser.runtime.getURL(page)

  iFrameRoot.appendChild(iFrame)
  document.body.appendChild(iFrameRoot)

  render(iFrame)
}

renderIframeContent('iframe/index.html', (iframe) => {
  Object.assign(iframe.style, {
    position: 'fixed',
    top: '0',
    right: '0',
    bottom: '0',
    left: '0',
    border: '0',
    zIndex: '9999999',
    overflow: 'visible',
  })
})

I hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants