Problem
First, we know that the problem is a security problem, what exactly is the problem? Well, when you try to load an extension resource in an iframe by installing iframe src, the browser complains about security and does not allow the iframe to connect to another protocol, in this case "moz-extension: //".
Decision
Download html etc. from the extension context and enter as a string.
Nitty gritty
To get around this, we can set the src iframe attribute to data:text/html;charset=utf8,${markup} .
This directly tells iframe that the content is html, it uses utf8 encoding, followed by raw markup. We completely circumvent the need for an iframe to load any resources over the network.
The execution context of the contents of the Firefox script is separate from the page to which it was loaded. This means that you can make an xhr request without breaking the CSP.
If you make an xhr request for your markup, you can get the contents of the response as a string and directly insert it into the iframe src attribute.
So the contents of the script:
function loaded (evt) { if (this.readyState === 4 && this.status === 200) { var html = this.responseText; console.log(html); var iframe = document.createElement('iframe'); iframe.src = 'data:text/html;charset=utf-8,' + html; document.body.appendChild(iframe); console.log('iframe.contentWindow =', iframe.contentWindow); } else { console.log('problem loading'); } } var xhr = new XMLHttpRequest(); xhr.overrideMimeType("text/html"); xhr.open("GET", browser.extension.getURL('test.html'), false); xhr.addEventListener("readystatechange", loaded); xhr.send(null);
Using a simple HTML file
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test</title> <meta charset="utf8" /> </head> <body> <h1>Hello World</h1> </body> </html>
So now you have successfully entered the html template in the target iframe.
If you need images, scripts, css files, etc., you will need to write a loader based on the method described above, introducing new script tags, etc. directly into the iframe document.