拦截浏览器的图片加载请求

发布于 2024-11-24 07:07:32 字数 128 浏览 6 评论 0 原文

我想知道是否有办法拦截浏览器的图像加载请求并添加一些服务器期望的请求头。

实际场景是这样的:Web应用程序向服务器发送XHR并完成身份验证握手。所有后续请求都必须包含 auth 标头。由于浏览器不发送图像请求的标头,图像已损坏。

I would like to know if there is a way to intercept the image loading requests of a browser and add some request headers expected by the server.

The actual scenario is this: the web app sends an XHR to the server and completes an authentication handshake. All the subsequent requests have to include the auth header. The images are broken because the browser does not send the headers for the image requests.

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

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

发布评论

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

评论(4

独孤求败 2024-12-01 07:07:32

这个问题本身很奇怪,我觉得你总体上使用了错误的方法。

但如果有人仍然好奇,现在这可以通过 Service Workers 来完成。

下面是我们如何做到这一点的

// Register the service worker
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register("/sw.js").then((registration) => {
    // Registration was successful
  }).catch((error) => {
    // Registration failed
  });
}

这是我们的 Service Worker

self.addEventListener('fetch',(event) => {
  if (/\.jpg$|.png$/.test(event.request.url)) {
    console.log("Image request: ", event.request.url);
  }
});

基本上,这种方法允许拦截任何请求。

Service Worker 无法直接与页面脚本通信,因为它们单独运行且相互隔离。但您可以使用 postMessage 进行通信。

The question itself is strange and I feel you're using the wrong approach in general.

But if anyone is still curious, nowadays this could be accomplished with Service Workers.

Below is how we did this

// Register the service worker
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register("/sw.js").then((registration) => {
    // Registration was successful
  }).catch((error) => {
    // Registration failed
  });
}

This is our Service Worker

self.addEventListener('fetch',(event) => {
  if (/\.jpg$|.png$/.test(event.request.url)) {
    console.log("Image request: ", event.request.url);
  }
});

Basically, this approach allows for intercepting any requests.

Service Workers can't communicate with your page scripts directly as they run separately and are isolated. But you can use postMessage for communication.

荒人说梦 2024-12-01 07:07:32

您可以使用带有适当标头的 AJAX 来请求图像。然后你必须对图像二进制进行base64编码,并通过设置将其插入到DOM中

<img src="data:image/png;base64,[base64 encoded image]" />

You can request the image using AJAX with the appropriate headers. Then you must base64 encode the image binary and insert it into the DOM by setting

<img src="data:image/png;base64,[base64 encoded image]" />
め七分饶幸 2024-12-01 07:07:32

有一种方法可以在浏览器中拦截图像请求:查看 Mobify.js 中的 Capturing API:https://hacks.mozilla.org/2013/03/capturing-improving-performance-of-the-adaptive-web/

There is a way to intercept image requests in the browser: checkout the Capturing API in Mobify.js: https://hacks.mozilla.org/2013/03/capturing-improving-performance-of-the-adaptive-web/

疯狂的代价 2024-12-01 07:07:32

不,没有办法做到这一点,但这也是一件非常好的事情。

(嗯,没有办法从你的代码中做到这一点。当然,浏览器所有者可以安装一个工具来改变请求,如果他们愿意的话。)

浏览器发出脚本和图像的 HTTP 请求的事实以自己严格的方式意味着使用 XHR 的站点可以通过让服务器拒绝某些不包含站点自己的 XHR 代码添加的特殊标头的请求来防止某些类型的 CSRF 攻击(跨站点请求伪造)。

您也无法准确控制浏览器对表单帖子标题的操作。

No, there is not a way to do that, and it's a very good thing too.

(Well, there's no way to do it from your code. The browser owner can install a tool that alters requests if they so desire, of course.)

The fact that browsers issue HTTP requests for scripts and images in their own strict ways means that a site using XHR can prevent some kinds of CSRF attacks (cross-site request forgery) by having the server refuse certain requests if they don't include a special header that the site's own XHR code adds.

You can't control exactly what a browser does to the header with form posts, either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文