如何在iframe中嵌入不存储Cookie的SoundCloud播放器?
嘿,我明白你遇到的困境了——想用sandbox阻止Cookie却导致播放器罢工,确实挺头疼的。下面有两个可行的方案,你可以试试看:
方案1:使用SoundCloud官方的无Cookie嵌入参数
SoundCloud其实自带了一个官方参数,可以直接禁用Cookie存储,不需要额外的sandbox属性。你只需要在播放器的src链接末尾添加&no_cookie=true即可。
修改后的代码示例:
<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/251273255&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true&no_cookie=true"></iframe>
这个参数是SoundCloud官方支持的,能直接让播放器以无Cookie模式运行,完全不会影响播放器的正常功能,是最稳妥的选择。
方案2:正确配置sandbox属性(如果方案1不适用)
如果你还是想通过sandbox来控制Cookie,那需要给sandbox添加足够的权限,让播放器能正常加载和运行。之前只加allow-scripts是不够的,因为播放器还需要其他权限来访问资源、播放音频等。
推荐的sandbox配置:
<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" sandbox="allow-scripts allow-popups allow-autoplay allow-presentation" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/251273255&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
这里不要添加allow-same-origin,因为这个权限会允许iframe内的内容设置Cookie。上面的权限组合既能保证播放器正常工作,又能阻止Cookie存储。
内容的提问来源于stack exchange,提问作者Basj
相关产品推荐
相关产品推荐

