简体   繁体   English

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

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

I have a simple image preview that, when clicked, takes you to a full size 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>

The problem: in addition to the request for the image, the browser will also request the favicon.ico.问题:浏览器除了请求图片外,还会请求favicon.ico。 The request headers for the image is: Referer: https://example.com/ (as expected).图像的请求标头为: Referer: https://example.com/ (如预期)。 However, for the request for the favicon.ico file, this header is the full url of the referring page: Referer: https://example.com/where-I-was .但是,对于 favicon.ico 文件的请求,此标头是引用页面的完整 URL: Referer: https://example.com/where-I-was

How do I set the Referer header for the favicon.ico request to simply the origin?如何将 favicon.ico 请求的 Referer 标头设置为简单的来源? I don't want it the full url to show in my nginx logs.我不希望它在我的 nginx 日志中显示完整的 url。 Thx!谢谢!

Option 1选项1

Use the referrerpolicy HTML attribute on the link tag - see docs on MDN .在链接标签上使用referrerpolicy HTML 属性 - 请参阅 MDN上的文档

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

Option 2选项 2

Use the webRequest API to rewrite the Referer header - see the MDN page on it here .使用webRequest API 重写 Referer 标头 - 请参阅此处的 MDN 页面 Simply set the header to whatever you want it to be - seems like you might be interested in the window.location object.只需将标题设置为您想要的任何内容 - 似乎您可能对window.location对象感兴趣。

Sample code - modified from the MDN page :示例代码 - 从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