Unfortunately you will not be able to modify the content style of an iFrame that points to another domain, since you will not have access to document
of it.
If the landing page allows CORS
, you can make a request AJAX
and paste the direct HTML into some element, for example a <div>
.
If the landing page does not accept CORS, you can use a proxy or write your own, an example proxy for CORS is crossorigin
, if you prefer to write your own proxy, basically make a web request on your server and return the HTML of this page using your domain.
Here is an example of how to use proxy
to circumvent CORS
and play content within an iFrame ...
var CORSFrame = function (iFrame) {
var self = this;
this.onload = null;
this.iFrame = iFrame;
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", self.corsProxy + self.iFrame.dataset.src, false);
httpRequest.addEventListener("readystatechange", function (event) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
var template = document.createElement("template");
template.innerHTML = httpRequest.responseText;
var content = template.content.getElementById("content");
var blob = new Blob(['<meta charset="utf-8">' + content.outerHTML], {
type: 'text/html;'
});
var bUrl = URL.createObjectURL(blob);
self.iFrame.src = bUrl;
}
}
});
httpRequest.send();
}
CORSFrame.prototype.corsProxy = "http://crossorigin.me/";
var iFrames = document.querySelectorAll("iframe[data-src]");
[].forEach.call(iFrames, function (iFrame, indice) {
var corsFrame = new CORSFrame(iFrame);
iFrame.addEventListener("load", function (event) {
var doc = iFrame.contentWindow.document;
doc.body.style.backgroundColor = "black"
doc.body.style.color = "white";
})
});
html, body {
overflow: hidden;
}
html, body, iframe {
margin: 0px;
padding: 0px;
border: 0px none transparent;
width: 100%;
height: 100%;
box-sizing: border-box;
}
<iframe id="wikipedia" data-src="https://pt.wikipedia.org/wiki/Constituição"></iframe>
DuetoaStackOverFlowlimitation,theaboveexamplewillnotwork,butyoucancheckitoutatthefollowing JSFiddle
Remembering that it is much simpler, to play the direct HTML content in a <div>
.
var CORSFrame = function (element) {
var self = this;
this.onload = null;
this.element = element;
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", self.corsProxy + self.element.dataset.src, false);
httpRequest.addEventListener("readystatechange", function (event) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
var template = document.createElement("template");
template.innerHTML = httpRequest.responseText;
var content = template.content.getElementById("content");
self.element.innerHTML = content.outerHTML;
}
}
});
httpRequest.send();
}
CORSFrame.prototype.corsProxy = "http://crossorigin.me/";
var elements = document.querySelectorAll("[data-src]");
[].forEach.call(elements, function (element, indice) {
var corsFrame = new CORSFrame(element);
});
html, body {
overflow: hidden;
}
[data-src] {
overflow: auto;
}
html, body, [data-src] {
margin: 0px;
padding: 0px;
border: 0px none transparent;
width: 100%;
height: 100%;
box-sizing: border-box;
}
#wikipedia {
background-color: black;
color: white;
}
<div id="wikipedia" data-src="https://pt.wikipedia.org/wiki/Constituição">
</div>