<iframe> 内的恶搞导航器

Spoof navigator inside <iframe>

我正在为隐私编写一个浏览器扩展。我需要做的一件事是欺骗 window.navigator 对象及其属性,我已经为主要的 window 对象成功地做到了这一点。我还需要为页面上的每个 iframe.contentWindow 欺骗 window.navigator 对象。我想我可以使用 self.frames 然后循环遍历每个欺骗,但是在我的代码运行后创建但在我的代码运行时不存在的帧呢? var myFrame = document.createElement("iframe"); ???

我想要欺骗所有导航器对象实例,主要 window 中的那个以及在每个 iframe 中创建的那个。为了让您了解我正在尝试做什么,这无法欺骗每个 iframe 内的导航器。

Object.defineProperty(HTMLIFrameElement.prototype.contentWindow, "navigator", {
  configurable: true,
  enumerable: true,
  value: "some fake navigator object"
});

也许是因为它实际上 window.HTMLIFrameElement.prototype 而这就是我出错的地方?

有什么想法吗? 如果您不明白问题,请询问更多细节。

iframe 中的全局对象 (window) 不是从 HTMLIFrameElement 的原型派生的。原型的 contentWindow 属性 只是一个访问器,它为您提供另一个全局的跨领域代理。

要替换实际的 navigator 属性,您需要 运行 每个 iframe 中的脚本 {all_frames: true, run_at: document-start}