
本文介绍使用 ViolentMonkey 脚本在每次浏览器启动时动态随机伪造 screen、window 等关键分辨率属性,以增强隐私防护;同时强调其局限性与更优实践建议。
本文介绍使用 violentmonkey 脚本在每次浏览器启动时动态随机伪造 `screen`、`window` 等关键分辨率属性,以增强隐私防护;同时强调其局限性与更优实践建议。
在反指纹(anti-fingerprinting)实践中,屏幕分辨率是极易被采集的高熵特征之一。若每次访问都返回固定值(如 1920×1080),反而会强化设备唯一性;而真正有效的策略不是“完全随机”,而是模拟真实用户分布中的常见分辨率组合。以下是在 ViolentMonkey 中实现该目标的专业级方案。
✅ 推荐实现:预设常见分辨率池 + 伪随机选择
避免运行时生成任意宽高比(如 1377×623),这本身即构成异常指纹。应从真实主流分辨率中选取,并保持各属性间逻辑一致(例如 innerWidth 应略小于 outerWidth,devicePixelRatio 需匹配设备类型):
// ==UserScript==
// @name Random Screen Resolution Spoof (Anti-FP)
// @namespace https://github.com/yourname
// @version 1.2
// @description Spoof screen/window dimensions with realistic, non-randomized values on every page load
// @author You
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
// ✅ 精心筛选的真实常见分辨率组合(兼顾桌面/笔记本/高分屏)
const RESOLUTION_SETS = [
// 1920×1080 (Full HD) — 最常见,含合理边框与缩放
{
availWidth: 1920, width: 1920,
availHeight: 1080, height: 1080,
innerWidth: 1920, innerHeight: 974,
outerWidth: 1920, outerHeight: 1040,
devicePixelRatio: 1
},
// 2560×1440 (QHD) — 高端显示器主流
{
availWidth: 2560, width: 2560,
availHeight: 1440, height: 1440,
innerWidth: 2560, innerHeight: 1334,
outerWidth: 2560, outerHeight: 1400,
devicePixelRatio: 1
},
// 1280×720 (HD) — 旧笔记本/小屏设备
{
availWidth: 1280, width: 1280,
availHeight: 720, height: 720,
innerWidth: 1280, innerHeight: 624,
outerWidth: 1280, outerHeight: 690,
devicePixelRatio: 1
},
// 1440×900 — 经典宽屏笔记本(仍有一定存量)
{
availWidth: 1440, width: 1440,
availHeight: 900, height: 900,
innerWidth: 1440, innerHeight: 794,
outerWidth: 1440, outerHeight: 860,
devicePixelRatio: 1
}
];
// ? 每次脚本执行时「伪随机」选择一个(实际使用 Math.random() 即可,无需加密安全)
const selected = RESOLUTION_SETS[Math.floor(Math.random() * RESOLUTION_SETS.length)];
// ?️ 安全覆写:使用 bind(null) 隐藏 getter 源码(关键!否则易被检测)
const defineProp = (obj, prop, value) => {
Object.defineProperty(obj, prop, {
get: function () { return value; }.bind(null),
configurable: true,
enumerable: true
});
};
// ⚙️ 批量注入所有属性
defineProp(window.screen, 'availWidth', selected.availWidth);
defineProp(window.screen, 'width', selected.width);
defineProp(window.screen, 'availHeight', selected.availHeight);
defineProp(window.screen, 'height', selected.height);
defineProp(window, 'innerWidth', selected.innerWidth);
defineProp(window, 'innerHeight', selected.innerHeight);
defineProp(window, 'outerWidth', selected.outerWidth);
defineProp(window, 'outerHeight', selected.outerHeight);
defineProp(window, 'devicePixelRatio', selected.devicePixelRatio);
// ? 可选:覆盖 screen.orientation(部分网站会读取)
if (window.screen.orientation) {
defineProp(window.screen.orientation, 'type', 'landscape-primary');
}
})();
⚠️ 重要注意事项与进阶建议
- @run-at document-start 是必须的:确保在页面 JS 执行前完成覆写,否则原始值可能已被缓存。
- 不要用 Math.random() 生成任意数值:1921×1081 或 1600×901 这类非标准尺寸会立即暴露脚本行为,显著提升指纹唯一性。
- bind(null) 不可省略:未绑定的 getter 函数 toString() 会暴露源码路径(如 "function(){return 1920;}"),成为明确的自动化脚本指纹(参考 Palant 的经典分析)。
- 单一值 > 完全随机:权威研究(如 Tor Project、Brave 浏览器文档)指出,长期稳定使用一个高频率真实分辨率(如 1920×1080)比每次切换更难被区分——因为真实用户极少改变物理分辨率。
- 警惕「过度防御」陷阱:单独伪造分辨率效果有限。现代指纹识别依赖多维度交叉验证(Canvas、WebGL、AudioContext、字体枚举等)。建议优先启用浏览器原生防护(Firefox 的 Total Cookie Protection + Resist Fingerprinting;Brave 的 Aggressive Mode),再辅以脚本微调。
- ViolentMonkey 兼容性提示:确保脚本权限设置为 @grant none(仅需 DOM 访问),避免因 @grant unsafeWindow 引发跨域或沙箱限制问题。
✅ 总结:本方案不追求“不可预测”,而追求“不可区分”——通过模拟真实硬件分布,让您的指纹融入海量普通用户中。真正的隐私保护,始于克制与理性,而非堆砌技术。











