是否可以通过扩展名更改核心javascript函数

时间:2019-07-12 07:09:35

标签: javascript google-chrome

我正在尝试更改默认的javascript参数。我想创建扩展名以防止获取网站的屏幕尺寸。当网站获取客户的屏幕尺寸时,它将进行通知并予以阻止。可以这样做吗?

enter image description here

>> window.screen.width
1366
>> window.screen.width = 800
800
>> window.screen.width
1366

1 个答案:

答案 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);