英文:
Link to data url doesn't open until I hit return in url bar
问题
以下是您要翻译的内容:
我有一段HTML代码,我希望在有人在浏览器中点击链接(嵌入在后端返回的某些文本中)时,在另一个窗口中呈现它。因此在Java后端,我对其进行base64编码,并将其放入发送到前端的文本中:
String encoded = DatatypeConverter.printBase64Binary(resourceRecord.getHtml().getBytes());
a.attr("href", "data:text/html;base64," + encoded)
.attr("target", "_blank")
.attr("rel", "nofollow");
当文本被渲染时,在Web检查器中会看到一个蓝色下划线链接,看起来像是这样的:
所以目前为止还不错。不幸的是,当我点击链接时,它只会打开一个新的空白标签页。在Firefox和Chrome中,如果我在新标签中点击地址栏,它会在地址栏中显示数据URL,如果我按回车键,预期的HTML将被呈现出来。
我如何使HTML在没有“点击地址栏并按回车键”的情况下实际呈现出来?或者这种可能性存在吗?
更新 添加了一个内容安全策略,其中包括 navigate-to 'self' https: http: data:
,但没有起作用。我还尝试了不进行base64编码,因为我在一个网站上读到说Chrome仅限制了基于base64的顶级数据URL,但没有起作用,其他来源则讨论了对所有数据URL进行限制。我看到的每个解决方案都涉及打开一个窗口并对其进行写入,而不是使用<a>
标签,所以我得出结论,这似乎是不可能的。
英文:
I have a chunk of html that I want to render in another window when somebody clicks on a link (embedded in some text that comes from the back-end) in the browser. So in the Java back-end, I base64 encode it and put it into the text that's being sent to the front end:
String encoded = DatatypeConverter.printBase64Binary(resourceRecord.getHtml().getBytes());
a.attr("href", "data:text/html;base64," + encoded)
.attr("target", "_blank")
.attr("rel", "nofollow");
And when the text is rendered, I see a blue underlined link that looks like this in the web inspector:
So far so good. Unfortunately, when I click the link, it just opened a new blank tab. In both Firefox and Chrome, if I click on the address bar in that new tab, it shows the data url in the address bar, and if I hit return the expected html is rendered.
How can I make that html actually render without the "click in the address bar and hit return" step? Or is it even possible?
Update Added a Content Security Policy with navigate-to 'self' https: http: data:
and that didn't help. I also tried not base64 encoding because I read one site that said that Chrome was restricting top level data urls only for base64 encoding, but that didn't help, and other sources talked about restricting it for all data urls. Every solution I saw involved opening a window and doing a write into it, rather than using an <a>
tag, so I'm coming to the conclusion that this just isn't possible.
专注分享java语言的经验与见解,让所有开发者获益!
评论