我如何仅在浏览器API调用的域中仅拦截一个域的一个端点?

发布于 2025-01-21 11:19:59 字数 896 浏览 2 评论 0 原文

假设我输入一个(公共)网站,该网站在3个不同的端点上进行3个XHR/fetch呼叫:

我想实现的目标是拦截到 https://api.example.com/path2 仅,将其重定向到本地服务(Local -Host:8000)和让PATH1和PATH3贯穿原始域。

我在这里有什么样的选择?我已经研究了很多解决这个问题的方法:

  • DNS重写 - 此解决方案不合适,因为我仍然必须拦截Path1和Path3,只有将它们重定向到原始IPS并尝试尽可能地模仿标头 - 这意味着我将必须为每个截断的域进行特定的代理配置 - 这是不可行的
  • 镀铬扩展 - 发现没有一个专门处理单个端点,
  • 截取了page loding fetter和xmlhttprequest oferpage off page load off page load oft page load-仍然没有涵盖所有场景fetch和xmlhttprequest的值之前(?)

Suppose I enter a (public) website that makes 3 XHR/fetch calls on 3 different endpoints:

What I want to achieve is intercept the call to https://api.example.com/path2 only, redirect it to a local service (localhost:8000) and let path1 and path3 through to the original domain.

What kind of options do I have here? I have studied a lot of approaches to this issue:

  • DNS rewriting - this solution is not suitable as I still have to intercept path1 and path3, only redirect them to the original IPs and try to mimic the headers as much as possible - which means I would have to do a specific proxy configuration for each intercepted domain - this is unfeasible
  • Chrome extensions - found none to deal specifically with single endpoint intercepting
  • Overwriting both fetch and XmlHttpRequest after page load - still doesn't cover all scenarios, maybe some websites cache the values of fetch and XmlHttpRequest before page load (?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-01-28 11:20:00

结合镀铬扩展名和获取覆盖物将起作用。

  1. 下载webextension,让您在给定页面加载之前加载JavaScript代码,例如用户javaScript和css
  2. 在页面加载之前添加以下脚本以运行:拦截JavaScript提取API请求和响应
    const { fetch: originalFetch } = window;
    window.fetch = async (...args) => {
      let [resource, config ] = args;
    
      // request interceptor starts
      resource = resource === "https://api.example.com/path2" ? "http://localhost:8000/path2" : resource
      // request interceptor ends
    
      const response = await originalFetch(resource, config);
    
      // response interceptor here
      return response;
    };

Combining the chrome extension and fetch overwrite will work.

  1. download an webextension that let you load javascript code before a given page loads, e.g. User JavaScript and CSS
  2. Add the following script to run before your page loads, base on: Intercepting JavaScript Fetch API requests and responses
    const { fetch: originalFetch } = window;
    window.fetch = async (...args) => {
      let [resource, config ] = args;
    
      // request interceptor starts
      resource = resource === "https://api.example.com/path2" ? "http://localhost:8000/path2" : resource
      // request interceptor ends
    
      const response = await originalFetch(resource, config);
    
      // response interceptor here
      return response;
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文