I'm trying to access inside a TradingView stock chart widget (https://www.tradingview.com/widget/advanced-chart/), which loads in an iframe. The goal is to programatically draw on the canvas tag of the widget after it has loaded, but I'm getting the following error:
Blocked a frame with origin "http://localhost:3000" from accessing a cross-origin frame
I tried basically every answer here: How to enable cors nodejs with express?, but I think this might be a different context than trying to access an iframe. I saw some other information about using postMessage
, but I also don't think that's relevant for interacting with HTML elements.
Here's the full code. It's the last line within the draw
function that creates the error.
index.js
const cors = require('cors');
const express = require('express');
const app = express();
app.use(cors())
app.get('/', (req, res, next) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/test', (req, res, next) => {
res.send("success")
});
const port = 3000
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
index.html
<html>
</script>
<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
<div id="technical-analysis-chart-demo"></div>
<div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/symbols/AAPL/" rel="noopener" target="_blank"><span class="blue-text">AAPL stock chart</span></a> by TradingView</div>
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
<script type="text/javascript">
new TradingView.widget(
{
"container_id": "technical-analysis-chart-demo",
"width": "100%",
"height": "100%",
"autosize": true,
"symbol": "AAPL",
"interval": "D",
"timezone": "exchange",
"theme": "light",
"style": "1",
"toolbar_bg": "#f1f3f6",
"withdateranges": true,
"hide_side_toolbar": false,
"allow_symbol_change": true,
"save_image": false,
"studies": [
"ROC@tv-basicstudies",
"StochasticRSI@tv-basicstudies",
"MASimple@tv-basicstudies"
],
"show_popup_button": true,
"popup_width": "1000",
"popup_height": "650",
"locale": "en"
}
);
</script>
</div>
<!-- TradingView Widget END -->
<script>
function draw() {
var elem = document.querySelector('#technical-analysis-chart-demo > div > div > iframe');
console.log(elem.contentWindow)
console.log(elem.contentWindow.querySelector("div"))
}
window.setTimeout(draw, 3000);
</script>script>
</html>
I'm wondering if there's a workaround to this. If not, I'm wondering if I could overlay another canvas on top of the widget but still allow the user to interact with the widget at the same time.