unfortunately you will not be able to modify the style of the contents of an iframe that points to another domain, since you will not have access to document
of the same.
if the destination page allows CORS
, you can make a request AJAX
and paste direct HTML into some element, for example a <div>
.
If the destination page does not accept CORS, you can use a proxy or write your own, an example proxy for CORS is the 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’s an example of how to use a proxy
to circumvent the CORS
and play the content inside 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://en.wikipedia.org/wiki/Constitution">
</iframe>
Due to a Stackoverflow limitation, the above example will not work, but you can check it in the following Jsfiddle
Remembering that it is much simpler, 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://en.wikipedia.org/wiki/Constitution">
</div>
You want to style the content of iframes?
– Rafael Telles
what would be such an "Object" and what do you mean by "add another specific formatting to those pages that I’m importing" ?
– Daniel Omine
You cannot manipulate the content of an iframe if it is not in the same domain as your Site. what you can do, is send an AJAX request to your site, your server downloads the desired HTML and returns the desired HTML snippet.
– Tobias Mesquita
@Tobymosque How to do this? There are some examples. I don’t know anything about Ajax. And just to be clear, the project is: Import the url of the Laws, for example the Constitution and apply a new training for better reading or even annotation on the articles of law.
– lf.frella
What is Server-Side Technology? PHP, ASP.NET, Java, Nodejs, Ruby, etc? Or are you using a Wordpress-like content manager?
– Tobias Mesquita
@Tobymosque I haven’t thought about it yet. I was hoping for an <iframe> or <Object> resolve, which didn’t happen. But Wordpress wouldn’t be problem.
– lf.frella
You can add an example of the link or content, maybe the site accepts CORS or make available a Feed for the material you want.
– Tobias Mesquita
None of the answers solved the problem?
– Machado