【问题标题】:Hooking document.write function挂钩 document.write 函数
【发布时间】:2013-01-06 10:54:04
【问题描述】:

我正在开发一个需要我拦截 document.write 函数的 Chrome 扩展程序(注意:我正在使用内容脚本)。我在这里使用的方法:http://sitr.us/2012/09/04/monkey-patching-document-write.html 但它不能正常工作。这就是我现在拥有的:

(function() {
    var originalWrite = document.write;
    alert("checkpoint 1");
    document.write = function() {
        alert("checkpoint 2");

        //secret stuff here         

        return Function.prototype.apply.call(
                        originalWrite, document, arguments);    
    }
})();

但是,当我在网页上调用 document.write 时,我的钩子中的“检查点 2”警报永远不会被调用。我究竟做错了什么?

【问题讨论】:

  • 不确定扩展的工作流程...但是您尝试过return originalWrite.apply(this, arguments); 吗?

标签: javascript google-chrome


【解决方案1】:

您的扩展程序在其自己的沙箱中运行,has no access to the web page's JavaScript environment。在您的扩展程序中覆盖document.write 不会影响网页本身的document.write 功能。

这是the docs的引述:

但是,内容脚本有一些限制。他们不能:

  • 使用 chrome.* API(chrome.extension 的部分除外)
  • 使用由其扩展页面定义的变量或函数
  • 使用网页定义的变量或函数或其他内容脚本

要更改网页的document.write 功能,您必须insert your script into the DOM

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2020-12-11
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多