Gilks commented on issue #32357:
URL: https://github.com/apache/superset/issues/32357#issuecomment-2891785491
Sure. Just reiterating that this is a hack and @rusackas is correct that
injecting into an iframe is not a good idea. Put this in your
`superset_config.py`.
```
def inject_css_handler(app: Flask):
@app.after_request
def after_request(response):
if '/embedded/' in request.path and response.content_type and
response.content_type.startswith(
'text/html'):
css_handler = """
<script>
(function() {
let messagePort = null;
function handleMessage(event) {
console.log('CSS: Message received:', event.origin);
if (event.data && event.data.type ===
'__embedded_comms__') {
if (event.data.handshake === 'port transfer') {
console.log('CSS: Handshake received,
setting up port');
messagePort = event.ports[0];
messagePort.onmessage = handlePortMessage;
}
} else if (event.data &&
event.data.switchboardAction === 'emit') {
console.log('CSS: Switchboard action received');
if (event.data.method === 'customCss' &&
event.data.args && event.data.args.css) {
console.log('CSS: Applying styles:',
event.data.args.css);
const styleElement =
document.createElement('style');
styleElement.textContent =
event.data.args.css;
document.head.appendChild(styleElement);
console.log('CSS: Styles applied');
}
}
}
function handlePortMessage(event) {
console.log('CSS: Port message:',
JSON.stringify(event.data));
}
window.addEventListener('message', handleMessage, false);
console.log('CSS: Handler initialized');
})();
</script>
"""
response.direct_passthrough = False
content = response.get_data(as_text=True)
if '</body>' in content:
content = content.replace('</body>', f'{css_handler}</body>')
response.set_data(content)
return response
FLASK_APP_MUTATOR = inject_css_handler
```
As outlined above your client would simply do this:
```
const embeddedFrame = document.querySelector('iframe');
embeddedFrame.contentWindow.postMessage({
switchboardAction: 'emit',
method: 'customCss',
args: {
css: '.dashboard { background: blue !important; }'
}
}, '*');
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]