与Chrome中运行在页面上的代码进行通信的唯一方法是通过DOM,因此您必须使用黑客手段,例如
<script>在代码中插入标签。请注意,如果您的脚本需要先运行页面上的所有其他内容,则这可能会引起问题。
编辑: 这是Nicealert扩展程序执行此操作的方式:
function main () { // ... window.alert = function() {}; // ...}var script = document.createElement('script');script.appendChild(document.createTextNode('('+ main +')();'));(document.body || document.head || document.documentElement).appendChild(script);


