fix: wrap stealth navigator overrides in try-catch, add pixelRatio to screen override
This commit is contained in:
@@ -7,44 +7,38 @@ export async function injectNavigatorStealth(
|
|||||||
profile: FingerprintProfile
|
profile: FingerprintProfile
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await context.addInitScript((opts) => {
|
await context.addInitScript((opts) => {
|
||||||
Object.defineProperty(navigator, 'webdriver', {
|
// Override navigator properties; wrap in try-catch because stealth plugin may have already
|
||||||
get: () => false,
|
// defined some as non-configurable / 用 try-catch 包裹,因为 stealth 插件可能已将某些属性设为不可重定义
|
||||||
});
|
function safeDefine(obj: any, prop: string, getter: () => any) {
|
||||||
|
try { Object.defineProperty(obj, prop, { get: getter, configurable: true }); } catch { /* already set */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
safeDefine(navigator, 'webdriver', () => false);
|
||||||
|
|
||||||
if (navigator.plugins.length === 0) {
|
if (navigator.plugins.length === 0) {
|
||||||
Object.defineProperty(navigator, 'plugins', {
|
safeDefine(navigator, 'plugins', () => {
|
||||||
get: () => {
|
|
||||||
const arr = Object.create(PluginArray.prototype);
|
const arr = Object.create(PluginArray.prototype);
|
||||||
arr.length = 0;
|
arr.length = 0;
|
||||||
arr.item = () => null;
|
arr.item = () => null;
|
||||||
arr.namedItem = () => null;
|
arr.namedItem = () => null;
|
||||||
arr.refresh = () => {};
|
arr.refresh = () => {};
|
||||||
return arr;
|
return arr;
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(navigator, 'languages', {
|
safeDefine(navigator, 'languages', () => opts.languages);
|
||||||
get: () => opts.languages,
|
safeDefine(navigator, 'platform', () => opts.platform);
|
||||||
});
|
safeDefine(navigator, 'vendor', () => 'Google Inc.');
|
||||||
|
safeDefine(navigator, 'productSub', () => '20030107');
|
||||||
Object.defineProperty(navigator, 'platform', {
|
|
||||||
get: () => opts.platform,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!('connection' in navigator)) {
|
if (!('connection' in navigator)) {
|
||||||
Object.defineProperty(navigator, 'connection', {
|
safeDefine(navigator, 'connection', () => ({
|
||||||
get: () => ({
|
|
||||||
downlink: 10,
|
downlink: 10,
|
||||||
effectiveType: '4g',
|
effectiveType: '4g',
|
||||||
rtt: 50,
|
rtt: 50,
|
||||||
saveData: false,
|
saveData: false,
|
||||||
}),
|
}));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(navigator, 'vendor', { get: () => 'Google Inc.' });
|
|
||||||
Object.defineProperty(navigator, 'productSub', { get: () => '20030107' });
|
|
||||||
}, {
|
}, {
|
||||||
languages: profile.languages,
|
languages: profile.languages,
|
||||||
platform: profile.platform,
|
platform: profile.platform,
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ export async function injectScreenStealth(
|
|||||||
get: () => opts.colorDepth,
|
get: () => opts.colorDepth,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Override devicePixelRatio / 覆盖设备像素比
|
||||||
|
Object.defineProperty(window, 'devicePixelRatio', {
|
||||||
|
get: () => opts.pixelRatio,
|
||||||
|
});
|
||||||
|
|
||||||
// Override window.outerWidth/Height > inner for window decorations / 覆盖 outerWidth/Height 使其 > inner 模拟窗口装饰
|
// Override window.outerWidth/Height > inner for window decorations / 覆盖 outerWidth/Height 使其 > inner 模拟窗口装饰
|
||||||
Object.defineProperty(window, 'outerWidth', {
|
Object.defineProperty(window, 'outerWidth', {
|
||||||
get: () => opts.outerWidth,
|
get: () => opts.outerWidth,
|
||||||
@@ -38,9 +43,10 @@ export async function injectScreenStealth(
|
|||||||
}, {
|
}, {
|
||||||
screenWidth: screenProfile.width,
|
screenWidth: screenProfile.width,
|
||||||
screenHeight: screenProfile.height,
|
screenHeight: screenProfile.height,
|
||||||
availWidth: screenProfile.width,
|
availWidth: screenProfile.width - TASKBAR_HEIGHT, // side taskbar scenario / 侧边任务栏场景
|
||||||
availHeight: screenProfile.height - TASKBAR_HEIGHT,
|
availHeight: screenProfile.height - TASKBAR_HEIGHT,
|
||||||
colorDepth: screenProfile.colorDepth,
|
colorDepth: screenProfile.colorDepth,
|
||||||
|
pixelRatio: screenProfile.pixelRatio,
|
||||||
outerWidth: viewport.width + 16, // window frame chrome / 窗口边框
|
outerWidth: viewport.width + 16, // window frame chrome / 窗口边框
|
||||||
outerHeight: viewport.height + 72, // title bar + window frame / 标题栏 + 窗口边框
|
outerHeight: viewport.height + 72, // title bar + window frame / 标题栏 + 窗口边框
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user