I recently developed a Firefox extension which allows me to automate some actions on several websites (mainly retrieving data).
The extension works well so I'm currently developing a Chrome version of the extension.
There are some differences between Chrome and Firefox extensions but it's mainly the same code which is running in both extensions.
Issue
Most of it works, however I'm having troubles with a specific website which works well with the Firefox extension.
On this website, I'm trying to get a table which is located into an iframe itself located into another iframe.
When accessing second iframe, I'm facing the following exception.
Uncaught SecurityError: Blocked a frame with origin "https://subdomain.domain.fr" from accessing a frame with origin "https://subdomain.domain.fr". The frame requesting access set "document.domain" to "domain.fr", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access
The page is structured as follow
<iframe id="iframe_centrale" src="/[...]">
[...]
<iframe id="iframe" src="/[...]">
[...]
<table></table>
[...]
</iframe>
[...]
</iframe>
And I'm trying to access the iframe as follow
var tableElement = null;
var iframe = null;
iframe = document.getElementById('iframe_centrale');
if(null != iframe)
{
iframe = iframe.contentWindow.document.getElementById("iframe");
if(null != iframe)
{
tableElement = iframe.contentWindow.document.evaluate("//table", iframe.contentDocument, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if((null != tableElement)
{
// Read table
}
}
}
Issue analysis
The fired exception is usually met when trying to access a ressource located on another domain.
However in my case, both iframes are located on the same domain because the src
attribute is a relative url.
The fact that the exact same code is working correctly with Firefox also confirms that I can access the second iframe.
More information
Another error I'm facing is that during the browsing process, I fill a form located into an iframe and the following exception is triggeredonce, however the form is still filled correctly.
Uncaught DOMException: Blocked a frame with origin "https://subdomain.domain.fr" from accessing a cross-origin frame.
since its in a mutation observer loop, the instruction may work the second time, it could explain why the form is filled.
After filling the form, I click a button, into an iframe, itself into an iframe. When the button is clicked I have the following exception
Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution
Once again, the click is made into a mutation observer loop so it may be triggered once, and then works, but I'm redirected to the next page so the click is working.
Investigating from Firefox
Since I can access both iframes' content from Firefox, I tried to print 'domain' and 'url' information for base document and both iframes.
#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
TypeError: iframe2 is null
[...]
#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
### IFRAME 2 : domain.fr / about:blank
[...]
#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
### IFRAME 2 : subdomain.domain.fr / https://subdomain.domain.fr/path3
So I think the issue is occurring because the second iframe changes its domain when the url is updated from about:blank
to https://subdomain.domain.fr/path3
Questions
Why are exceptions fired with iframes located on the same domain/subdomain ?
Is there another way to access the second iframe content and act on it with Chrome ?
Does somebody knows why this code is working with Firefox but not with Chrome ?