Styling iFrame Widget with Custom CSS (React, MUI)

I'm currently working on personalizing a fiat-to-crypto integration widget to align with the style of my react application.

The setup involves a reactjs platform using material ui, with the widget embedded in an iFrame.

An individual has successfully accomplished this customization here:

After inspecting their implementation, I'm facing challenges replicating it within my react app.

I've experimented with two approaches:

APPROACH ONE

Attempting to access contentWindow.document - unfortunately, querying reveals no visible document:

function changeStuff() {
    const onramperWindow = onramper.contentWindow;
    console.log(onramperWindow)
    const onramperDocument = onramperWindow.document
    console.log(onramperDocument)
 }

This approach encounters errors as the document doesn't appear accessible outside of the parent element which isn't the target for editing.

APPROACH TWO

Given that my react app utilizes MUI, I tried incorporating useStyles:

const useStyles = makeStyles({
    embedContainer: {
      "& iframe": {
          position: "absolute",
          top: 0,
          left: 0,
          width: "50%",
          height: "100%",
          color: "#000000"
      }
    }
  });

Followed by integrating the iframe:

<iframe
id="onramper"
src="https://widget.onramper.comcolor=346eeb&apiKey=pk_test_x5M_5fdXzn1fxK04seu0JgFjGsu7CH8lOvS9xZWzuSM0"
height="595px"
width="800px"
title="Onramper widget"
frameborder="no"
allow="accelerometer;
autoplay; camera; gyroscope; payment"
className={classes.embedContainer}
>
<a href="https://widget.onramper.com" target="_blank">Buy crypto</a>
</iframe>

However, this method results in the entire screen turning black due to injecting css across the entire page.

If anyone has insights, tips, or resources on how to customize the CSS of this iframe widget, I would greatly appreciate it.

Answer №1

By default, web browsers have a security feature that isolates iframes from interacting with each other and the main page. This restriction is important for security reasons and should not be bypassed.

Imagine creating a website like fakebank.com with an iframe from bank.com, allowing you to monitor and collect user information without detection.

However, there is a workaround where you can download the content of the iframe and embed it on your own page to gain full access.

For example: Using jQuery:

$('#divframe').load(
    'http://www.corsproxy.com/' +
    'www.cptec.inpe.br/widget/widget.php?p=3819&amp;w=h&amp;c=474647&amp;f=ffffff .tabtop',  function() {
         $('#divframe').html( $('#divframe').html().replace(new RegExp('src="', 'g'),'src="http://www.cptec.inpe.br/widget/') );
    });

HTML:

<div id="divframe"></div>

Alternatively, using a custom PHP proxy:

proxy.php (with only this content, nothing else)

<?php
$cache_file = 'proxy_cache.html'; // filename to store cache

if (!@file_exists($cache_file) || (time() - @filemtime($cache_file) > (60 * 60 * 6))) { // check if cache has expired or make new request
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.cptec.inpe.br/widget/widget.php?p=3819&amp;w=h&amp;c=474647&amp;f=ffffff");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = str_replace('src="','src="http://www.cptec.inpe.br/widget/',curl_exec($ch)); // update src attribute values
    curl_close($ch);
    file_put_contents($cache_file, $response); // save response as cache
} else { // use cached content if not expired
    $response = file_get_contents($cache_file);
}

echo $response; // display content
?> 

The following content should be in a separate file along with CSS and any dependencies

Using jQuery:

$('#divframe').load(
    'proxy.php .tabtop',  function() {
        $('#divframe .tab_content').hide();
        $('#divframe .tabs li:first').addClass('active');
        $('#divframe .tab_content:first').show();
        $('#divframe .tabs li').click(function(){
            $('#divframe .tabs li').removeClass('active');
            $(this).addClass('active');
            $('#divframe .tab_content').hide();
            $($(this).children('a').attr('href')).show();
        });
    });

HTML:

<div id="divframe"></div>

Apologies for any language errors, English is not my first language =D

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for fetching a particular item from a getItem dynamoDB in JavaScript

I am trying to retrieve an item from my AWS database. The item 'test2' is printing correctly in my console. However, I want to extract an attribute or variable from this item and store it as a variable named test. How can I accomplish this? For i ...

What is the best way to send a promise back from my service to my controller?

While I have posed this question in various forms, I now find myself stuck with a piece of code that contains an elusive bug. My Angular service looks like this: .service("lookupDataService", [ '$q', '$http', '$timeout&ap ...

What is the best way to ensure my Form stays fresh when I switch tabs?

I've been searching for an answer to this issue for two days now. I'm hoping someone can assist me. Here is the index HTML code I am working with: <div class="container-fluid" ng-controller="citizensController as citizensCtrl"> <h1& ...

Create a grid div that expands vertically to completely fill the available space

I am encountering an issue with my grid layout involving the menu, header, and content. The problem lies in the fact that I want the content (blue box) to expand vertically within the grid element (yellow box). Currently, the grid stretches vertically, bu ...

Ideas for enhancing your flutter calendar experience

Can anyone recommend a good calendar widget package for Flutter? I have tested the Syncfusion calendar and liked it. Looking for something similar to that in terms of features and functionality. Check out the Syncfusion calendar here. ...

Implement CSS to stack images upon zooming

On my menu page, I have two images that I want to display side by side in the web browser and stacked on top of each other in a column when viewed on mobile. Currently, with framework7's row and column classes, the images are positioned next to each o ...

Identifying a loop within a hierarchy of JavaScript elements

I am facing a challenge with a list of elements that have unique IDs and parent IDs. My goal is to identify any loops in this hierarchical structure and pinpoint the ID that initiates the loop. list = [ { id: '1', parent: '2' ...

Issues with jQuery causing responsive CSS triangles to fail implementations

Recently, I stumbled upon the interesting concept of CSS triangles created using element borders. I decided to incorporate this design into one of my custom Wordpress themes, but I encountered a problem. I wanted to add a triangle at the bottom of a div, l ...

Tips for detecting successful file downloads from the client side using Mean/AngularJS

I have developed a chat application with the capability to send files through chat windows. I am now looking to automatically delete files from the server once they have been successfully downloaded by clients. My technology stack includes MEAN. rou ...

What is the best way to fetch this JavaScript function?

I'm currently working with the following code snippet: var _initCarousel = function () { _carouselIndicators.empty(); _steps.each(function(index, element) { var li = $( '<li></li>' ); $(li).click(functio ...

Caution: Using functions as a React child is not allowed. This error can occur when you mistakenly return a Component instead of <Component />

import React, { Component } from "react"; class Example extends Component { constructor(props) { super(); this.colors = ["red", "green", "blue", "indigo", "violet"]; } displayColor ...

I have created a textbox with a button, but I am unsure of how to delete it

var emailFields = document.getElementById('emails'), addLinkButton = document.createElement('a'), fieldTemplate = emailFields.getElementsByTagName('div'), currentFieldCount = fieldTemplate.length, maxFields ...

Explain the concept of utilizing curried state handlers within a React and Typescript application

I am currently working on defining the function that will handle change events to update the state value accordingly. This is what I envision the implementation to look like: handleChange: ChangeHandler<State> = field => value => this.set ...

How to make background color transparent in CSS for Safari browser

Does anyone know how to make the background color transparent in Safari? Right now, my PNG file is showing with a white background instead of being transparent. I have attempted: background-color: transparent; background-color: rgba(255,255,255,0); appe ...

Vue.js - dynamically applying CSS classes based on conditions

Within a VueNative project that utilizes NativeBase as a component library, with tags prefixed by nb-, the code snippet below is found within the <template> tags of the footer.vue file which houses navigation buttons. This piece of logic successfully ...

Having trouble getting AJAX to work with posting JSON data? Unsure of what steps to take next?

Hey there, can you take a look at my code? It seems to be throwing an error when I try to run it. I'm still learning, so any help would be greatly appreciated! <script type="text/javascript"> $(function() { $('#btnRegister&ap ...

Shortening properties in React components

When working with Svelte, we have the option to use shorthand notation for attributes when the attribute name is the same as its value. For example: <img src={src} alt=""> This is equivalent to: <img {src} alt=""> I'm ...

Uncovering "camouflaged" HTML using Jsoup

Seeking to access HTML data that is not initially visible in the source document but can be revealed through tools like "inspect element" in Google Chrome. Here's an example page: There are div elements with assignment data for U.S. Patent No. 9,000 ...

initiate a click event within another click event

This is an HTML Code snippet: <ul id="linkjess"> <li><a href="javascript:setfolder('medewerkers', '1000');">medewerkers 1000</a></li> <li><a href="javascript:setfolder('medewerkers', ...

How should we properly insert a line break here? Additionally, can you explain why the square is displaying the incorrect color?

Here is the code snippet that I am currently working on. <html> <head> <script> onload=function main() { function createSquare(type) { let square = document.createElemen ...