覆盖浏览器 API 的



我正在为Firefox,Chrome等在JavaScript中开发Webextension。

它旨在防止用户浏览器被指纹。

由于用于构建浏览器指纹的大多数信息来自浏览器本身的JavaScript API,因此是否可以更改/欺骗普通API可能会从Webexension/addon中返回的值?

如果这是不可直接的,那么是否有任何方法可以控制这些API返回网站以保护用户隐私的值的值?

我正在谈论的API的示例是:

user agent
screen print
color depth
current resolution
available resolution
device XDPI
device YDPI
plugin list
font list
local storage
session storage
timezone
language
system language
cookies
canvas print

您可以尝试使用Object.defineProperty()

object.defineproperty()方法直接在对象上定义新属性,或修改对象上的现有属性,然后返回对象。

console.log(window.screen.colorDepth); // 24
Object.defineProperty(window.screen, 'colorDepth', {
  value: 'hello world',
  configurable: true 
});
console.log(window.screen.colorDepth); // hello world

在上面,我们使用Object.defineProperty来更改属性window.screen.colorDepth的值。在这里,您将使用任何想要的方法来欺骗值。您可以使用相同的逻辑来修改要欺骗的任何属性(例如navigator.userAgent

但是页面的全局对象与插件全局对象之间存在分离。您应该能够通过将脚本注入文档来克服:

var code = function() {
    console.log(window.screen.colorDepth); // 24
    Object.defineProperty(window.screen, 'colorDepth', {
      value: 'hello world',
      configurable: true 
    });
    console.log(window.screen.colorDepth); // hello world
};
var script = document.createElement('script');
script.textContent = '(' + code + ')()';
(document.head||document.documentElement).appendChild(script);

有关更多信息,请参见此处和此处。您可以在此处使用上述代码下载工作的Chrome扩展名(解压缩文件夹,导航到Chrome://Chrome中的扩展名,然后将文件夹放入窗口中)

最新更新