繁体   English   中英

单击图像时,如何将“来源”作为 favicon.ico 文件的引荐来源发送?

[英]How do I send "origin" as the referrer for the favicon.ico file when clicking on an image?

我有一个简单的图像预览,点击后,你会看到一个全尺寸的图像:

<html>
  <head>
    <meta name="referrer" content="origin">
  </head>
  <body>
    <a href="/myimage.jpg">
      <img src="/myimage.jpg" title="my image" border="0" />
    </a>
  </body>
</html>

问题:浏览器除了请求图片外,还会请求favicon.ico。 图像的请求标头为: Referer: https://example.com/ (如预期)。 但是,对于 favicon.ico 文件的请求,此标头是引用页面的完整 URL: Referer: https://example.com/where-I-was

如何将 favicon.ico 请求的 Referer 标头设置为简单的来源? 我不希望它在我的 nginx 日志中显示完整的 url。 谢谢!

选项1

在链接标签上使用referrerpolicy HTML 属性 - 请参阅 MDN上的文档

<a href="/myimage.jpg" referrerpolicy="origin">

选项 2

使用webRequest API 重写 Referer 标头 - 请参阅此处的 MDN 页面 只需将标题设置为您想要的任何内容 - 似乎您可能对window.location对象感兴趣。

示例代码 - 从MDN 页面修改:

var targetURL = "http://example.com/favicon.ico";

var protocol = window.location.protocol;
var hostname = window.location.hostname;
var port = window.location.port;

var origin = protocol + '//' + hostname + (port ? ':' + port : '');

function rewriteUserAgentHeader(e) {
  e.requestHeaders.forEach(function(header){
    if (header.name.toLowerCase() == "referer") {
      header.value = origin;
    }
  });
  return {requestHeaders: e.requestHeaders};
}

browser.webRequest.onBeforeSendHeaders.addListener(
  rewriteUserAgentHeader,
  {urls: [targetURL]},
  ["blocking", "requestHeaders"]
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM