You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在iframe中阻止来自badsite.org的请求(无需Chrome广告拦截扩展)

如何在iframe中阻止特定域名的请求(无需扩展)

如果你无法控制嵌入的example.org页面,要阻止它向badsite.org发起请求,有两种无需扩展的可行方案:

1. 浏览器原生站点拦截(手动操作)

Chrome内置了站点请求控制功能,可直接拦截特定域名的请求:

  • 进入Chrome设置 → 隐私和安全 → 站点设置
  • 找到「网络」或「权限」相关选项,添加badsite.org到阻止列表,禁止该域名的所有请求
    这种方法不需要编写代码,但需要用户手动配置浏览器。

2. 主页面设置Content Security Policy(CSP)

如果你能控制嵌入iframe的主页面,可以通过设置CSP尝试限制请求,但需注意跨域iframe的请求默认不受主页面CSP约束,仅当example.org自身的CSP规则不冲突时生效:

  • 方式一:通过HTTP响应头添加CSP规则
    Content-Security-Policy: frame-src https://example.org; connect-src 'self' https://example.org; default-src 'self' https://example.org
    
  • 方式二:在主页面的<head>标签中添加meta标签
    <meta http-equiv="Content-Security-Policy" content="frame-src https://example.org; connect-src 'self' https://example.org; default-src 'self' https://example.org">
    

如果example.org自身的CSP允许向badsite.org发起请求,主页面的规则无法覆盖该设置。

关键说明

受浏览器同源策略限制,主页面无法直接干预跨域iframe内的网络请求,除非使用扩展工具。最彻底的解决方案是联系example.org的管理员,让他们在自身页面中添加CSP规则,从源头上禁止向badsite.org发起请求。

内容的提问来源于stack exchange,提问作者Brian Walczak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:07:22