To use an HTML file from a different site as the content of your about:blank popup, you'd typically want to fetch the HTML content via an HTTP request (using fetch, XMLHttpRequest, etc.) and then write the fetched content to the popup.
However, due to the same-origin policy enforced by web browsers for security reasons, you cannot directly fetch content from a different domain unless that domain provides appropriate CORS (Cross-Origin Resource Sharing) headers. If the other site has CORS enabled and allows your origin to fetch resources, you can do the following:
// Function that opens about:blank and sets up the content from a remote HTML file
function openCustomTab(remoteUrl) {
// Open a new about:blank window
let newTab = window.open('about:blank', '_blank');
if (!newTab) {
console.error('Pop-up was blocked. Please allow pop-ups for this site.');
return;
}
// Fetch the remote HTML content
fetch(remoteUrl)
.then(response => {
if (response.ok) return response.text();
throw new Error('Network response was not ok.');
})
.then(htmlContent => {
// Write the fetched HTML content to the new tab
newTab.document.write(htmlContent);
newTab.document.close();
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
// Call the function with the URL of the remote HTML file
openCustomTab('https://example.com/remote-content.html');
In this example, remoteUrl should point to the HTML file hosted on the other site. The fetch API is used to retrieve the content, after which the HTML is written into the about:blank popup using document.write.
If the remote server does not have CORS enabled for your site, you will encounter a CORS policy error. In that case, you have a few options:
-
Contact the owner of the remote content to ask if they can enable CORS for your domain, which allows your site to fetch the content.
-
Use a server-side proxy on your own domain to fetch the remote content. This avoids CORS issues since the request is made server-to-server, and you then relay the content to the client-side.
-
Ask for permission to host the content on your domain so that you can fetch it without encountering CORS issues.
Here's what a simple server-side proxy approach might look like in Node.js using the Express framework:
const express = require('express');
const fetch = require('node-fetch');
const app = express();
// Proxy endpoint that fetches the remote HTML content
app.get('/fetch-remote-content', (req, res) => {
const remoteUrl = 'https://example.com/remote-content.html';
fetch(remoteUrl)
.then(response => {
if (response.ok) return response.text();
throw new Error('Failed to fetch remote content.');
})
.then(htmlContent => {
res.send(htmlContent);
})
.catch(error => {
res.status(500).send(error.message);
});
});
// Run Express server on port 3000
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Your client-side JavaScript could then fetch this content from your server-side proxy endpoint:
// Client-side code
openCustomTab('/fetch-remote-content');
Make sure to adapt this example to fit your own server setup and programming language/environment. Remember to handle possible security concerns when setting up a proxy, as it could potentially expose your server to risks if not properly secured.