我正在尝试更改默认的javascript参数。我想创建扩展名以防止获取网站的屏幕尺寸。当网站获取客户的屏幕尺寸时,它将进行通知并予以阻止。可以这样做吗?
>> window.screen.width
1366
>> window.screen.width = 800
800
>> window.screen.width
1366
答案 0 :(得分:1)
window.screen
是内置的getter / setter。如果要在访问时通知,可以使用自己的getter覆盖该属性:
Object.defineProperty(window, 'screen', {
get() {
console.log('Tried to get window.screen, preventing');
throw new Error();
},
set() {
}
});
console.log(window.screen.availHeight);
不过,您可以考虑返回伪数据,以便页面的JS仍有运行的机会,例如:
Object.defineProperty(window, 'screen', {
get() {
console.log('Tried to get window.screen, preventing and returning false data');
return {
availHeight: 800,
availLeft: 0,
availTop: 0,
availWidth: 800,
colorDepth: 24,
height: 800,
orientation: {angle: 0, type: "landscape-primary", onchange: null},
pixelDepth: 24,
width: 800,
};
},
set() {
}
});
console.log(window.screen.availHeight);