为什么浏览器会限制 :visited 选择器? [英] Why did browsers limit :visited selector?

查看:30
本文介绍了为什么浏览器会限制 :visited 选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解隐私问题,但在 这篇文章 Mozilla 声明他们对 querySelector()getComputedStyle() 撒谎.

I understand the privacy concerns, but in this article Mozilla states that they are lying to querySelector() and getComputedStyle().

如果他们已经在对网站撒谎,那么为什么要将 :visited 限制为简单的颜色?不能使用相同的方法对网站隐藏完整的样式吗?

If they are already lying to sites, than why limit :visited to just simple colors? Couldn't full styling still be hidden from sites using the same method?

推荐答案

限制可以应用于访问链接的样式,防止它们以可以通过 getComputedStyle() 查询的方式影响不相关元素的布局——如果不秘密计算整个页面的布局,就无法进行欺骗,就好像所有链接都未访问一样,这在性能方面将是极其昂贵的.这与 :visited + span 之类的东西不再被应用(甚至 :visited 中仍然允许的属性)是一样的.

Limiting the styles that can be applied to visited links prevents them from affecting the layout of unrelated elements in a way that can be queried by getComputedStyle() — something that cannot be spoofed without secretly computing the layout of the entire page as if all links were unvisited, which would be extremely expensive performance-wise. This is in the same vein as things like :visited + span no longer being applied (not even the properties still allowed in :visited).

考虑一下这个概念验证,您可以在其中单击一个链接来切换模拟其访问性的类名,并查看如何在 :link:visited 会影响布局:

Consider this proof-of-concept, in which you can click a link to toggle a class name that simulates its visitedness, and see how toggling between :link and :visited can affect layout:

var a = document.querySelector('a'),
    p = document.querySelector('p + p');

a.addEventListener('click', function(e) {
  a.className = a.className == 'unvisited' ? 'visited' : 'unvisited';
  console.log('a is now ' + a.className + '; top pos of following p is now ' + p.getBoundingClientRect().top);
}, false);

a.unvisited {
  font-size: 1em;
}

a.visited {
  font-size: 2em; /* A property not normally allowed on :visited */
}

<p><a class="unvisited" href="#">Toggle visitedness</a>
<p>Another paragraph

这篇关于为什么浏览器会限制 :visited 选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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