为什么 facebook 有随机元素 ID?
我注意到 facebook 的每个元素都有随机元素 ID - 包括没有唯一元素 ID 的元素。
<div id="__w2_YvdN1r2_loading">blah</div>
任何想法为什么他们在每个元素上都这样做?他们是怎么做到的?
I have noticed that facebook has random element IDs for every element - including elements that have no unique element id.
<div id="__w2_YvdN1r2_loading">blah</div>
Any ideas why they do this on every element & how they do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我能想到的“为什么”的一个原因是防止或至少减少屏幕抓取 - 如果 ID 确实是随机的。至于如何生成伪随机值,有无数种方法,其中任何一种都可以被强制生成类似于
__w2_YvdN1r2
的字符串。考虑到 Facebook 的 Javascript 量有多大,如果 ID 和您想象的一样随机,我会感到非常惊讶,除非这些值也输出到 javascript 数组/对象/存储中,以供 javascript 页面使用。
One reason I can think of for "why" would be to prevent, or at least reduce the incidence of, screen scraping - if the IDs are indeed random. As to how, there are inumerable ways to generate pseudo-random values and any one of them could be coerced to produce a string that resembles
__w2_YvdN1r2
.Given how Javascript heavy Facebook is, I'd be quite surprised if the IDs are as random as you think, unless the values are also output into a javascript array/object/store of some description for the pages javascript to use.
因此更难有意义地抓取,可能只是一串随机字符。
So that it's harder to scrape meaningfully, probably just a string of random chars.
我假设这是引用它们可能关联的 Base64 编码的 UUID——可能代表用户或令牌。
I am assuming this is to reference the base64 encoded UUIDs that they may be associated with -- perhaps representing a user or token.
为每个元素提供唯一的 ID 可保证 FB 可以使用脚本引用特定元素。例如,FB 可能有一个将页面滚动到更新元素的脚本,并且他们希望脚本适用于所有元素。此外,这样每个元素都可以用作 URL 中的片段标识符。它还允许您永久链接到页面中的任何元素。因此,举例来说,如果我想将您链接到维基百科片段标识符页面的“示例”部分,那么我将使用该元素的 id 作为带有
#
的锚点:< ;a href="http://en.wikipedia.org/wiki/Fragment_identifier#examples">我的链接文本
哇哦。元。
至于如何实现,我们不知道,因为它是闭源的。但 FB 是用 PHP 编写的,因此他们可能会使用 uniqid 之类的东西。
Giving every element a unique id guarantees that the FB can reference a particular element using scripts. For example, FB might have a script that scrolls the page to an updated element, and they want script to work for all elements. Also, this way every element can be used as a fragment identifier in a URL. It also allows you to permalink to any element in the page. So, for example, if I want to link you to the "examples" section of wikipedia's Fragment Identifier page, then I'll use the id of that element as an anchor with
#
:<a href="http://en.wikipedia.org/wiki/Fragment_identifier#examples">my link text</a>
Woah. Meta.
As for the how, we don't know because it's closed source. But FB is written in PHP so they might use something like uniqid.