禁止嵌入公众号网页
用uni-app开发了一个H5网页项目,主要运行在公众号上。
有个需求是,需要嵌入任意公众号文章。
像之前一样用web-view嵌入,觉得妥妥的没问题。
结果直接报错:
Refused to frame 'https://mp.weixin.qq.com/' because an ancestor violates the following Content Security Policy directive:
"frame-ancestors 'self' http://wx.qq.com https://wx.qq.com http://wx2.qq.com 太多了省略".
大概意思就是只有上面这些地址才能嵌入公众号网页。
其余的都没办法。
这样做试试
一开始想到的有两个办法。
第一种location.href直接跳转,但担心用户返回时会丢失状态数据;
第二种window.open打开新窗口。
测试了一下,第二种方式可行,只是标题显示会有问题。
示例代码:
const articleUrl = 'https://mp.weixin.qq.com/s/cqlePshM3n2AczSxx9tZAg';
window.open(articleUrl, '_blank');
算是解决了吧。