V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Get Google Chrome
Vimium · 在 Chrome 里使用 vim 快捷键
AmItheRobot
V2EX  ›  Chrome

请教 Chrome 扩展下怎么拿到类似 Firefox 里的 detail.originUrl?

  •  
  •   AmItheRobot · Oct 25, 2020 · 2690 views
    This topic created in 2013 days ago, the information mentioned may be changed or developed.

    几年前开始有这个需求:

    拦截https://example.com/author/.* 源页面上,发出的对 https://vod.cdn.cn/img/.* 的资源(例如图片)请求
    但是放行 https://example.com/others/.* 等其它页面上,发出的对 https://vod.cdn.cn/img/.* 的资源请求

    这个需求 uBlock 满足不了(恳求仔细、慢一点读需求),全网也没找到能做到的扩展。

    后来找到一个偏方歪法(当时用的 Firefox ),用 Header Editor 来做,重定向请求+自定义函数

    if(detail.type.endsWith('_frame'))
      return null;
    if(/https:\/\/example\.com\/author\/.*/.test(detail.originUrl))
      return '_header_editor_cancel_';
    

    通过 Firefox Addon API 提供的 details.originUrl 来判断源页面。这个方法非常好用!

    但是最近换成 Chrome 了,虽然 Chrome 也有扩展 Header Editor,但是上面的自定义函数在 Chrome 下用不了。所以请教一下开发大佬:

    • Chrome 下要拿到类似 FF 的参数 detail.originUrl,要怎么拿?偶查了API 文档,没有这个参数
    • 或者 Chrome 有无新解决方案,满足最开始那个需求?
    4 replies    2020-10-25 17:38:46 +08:00
    AngryPanda
        1
    AngryPanda  
       Oct 25, 2020 via Android
    OnBeforeSendHeadersOptions
    sneezry
        2
    sneezry  
       Oct 25, 2020
    感觉可以通过判断 referer ?之前我向 v2ex-plus 贡献过一段正常显示微博图片的代码,是删除掉这个 header 。你可以改为判断这个 header,如果符合你的规则,就进行拦截。https://github.com/sciooga/v2ex-plus/blob/53f8d25f890f8d2b23f890bd4b4be03ae48603b7/background.js#L636
    iNaru
        3
    iNaru  
       Oct 25, 2020
    @sneezry referer 已经不带 path 了。
    AmItheRobot
        4
    AmItheRobot  
    OP
       Oct 25, 2020
    @sneezry #2 谢谢:) 确实 chrome 现在 referer 不带路径了。而且 HE 自定义函数里,要拿 header 只能在响应时拿,请求阶段拿不了,贼尴尬 =_=|
    https://he.firefoxcn.net/zh-CN/custom-function.html#detail 对象
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3811 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 05:17 · PVG 13:17 · LAX 22:17 · JFK 01:17
    ♥ Do have faith in what you're doing.