阻止来自Facebook js sharer的某些图像 [英] Block certain images from Facebook js sharer

查看:83
本文介绍了阻止来自Facebook js sharer的某些图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,有没有办法阻止Facebook js sharer的某些图像?

Well, as stated in title, is there any way to block certain images from Facebook js sharer?

Facebook js sharer会自动从页面中提取图像,并允许用户选择在共享文章中显示为缩略图的图像。目前,我们有一个情况,它在页面上获取广告图像,没有其他图像。

Facebook js sharer automatically fetches images from the page and allows user to choose the image to show as thumbnail in shared post. At the moment, we've got a situation, that it fetches advertisement images on the pages with no other images presented.

我可以想象,横幅可以显示为背景图像的div,或通过动态添加,但仍然可能有特殊的标签图像被排除或共享init选项...找不到与FB文档相关的任何东西,但它们是非常具体的...

I can imagine that banners could be shown as background images for divs, or by adding them dynamically, but still maybe there is special tag for images to be excluded or sharer init options... Can't find anything related on the FB docs, but they are very "specific"...

要澄清以上内容,请查看标记:

To clarify the above take look at the markup:

<!DOCTYPE html>
<html lang="en" 
      xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:og="http://ogp.me/ns#"
      xmlns:fb="https://www.facebook.com/2008/fbml">
 <head>
  <meta property="og:image" content="/logo.jpg">
 </head>
 <body>
  <article>
   ..plain text content
  </article>
  <aside>
   <img src="/banner.jpg"> <!-- fetched image for share, wrong -->
  </aside>
 </body>
</html>

PS:我读过这个问题:如何从Facebook sharer 中排除图像 - 但实际上动态添加横幅(选择的答案)并不是这样的意思

P.S.: I've read this question: how to exclude an image from facebook sharer - but actually adding banners dynamically (selected answer) is not the way it meant to be.

推荐答案

这是一个C& P的另一个答案我给了另一个问题,虽然它也适合这个:

This is a C&P of another answer I gave to a different question, though it fits here too:

您的问题的解决方案可能是检查真正的用户或Facebook机器人是否正在访问您的页面。如果是机器人,那么只需要提供必要的元数据。您可以通过其用户代理检测机器人,根据 Facebook文档是:
facebookexternalhit / 1.1(+ http://www.facebook.com/externalhit_autxt.php)

A solution for your problem might be to check whether a real user or the Facebook bot is visiting your page. If it is the bot, then render only the necessary meta data for it. You can detect the bot via its user agent which according to the Facebook documentation is:
"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"

代码看起来像这样(在PHP中):

The code would look something like this (in PHP):

function userAgentIsFacebookBot() {
    if ($_SERVER['HTTP_USER_AGENT'] == "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)") {
        return true;
    }
    return false;
}

这篇关于阻止来自Facebook js sharer的某些图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆