【问题标题】:Why can I change HTMLMediaElement.prototype.play from console, but not via user script? [duplicate]为什么我可以从控制台更改 HTMLMediaElement.prototype.play,但不能通过用户脚本? [复制]
【发布时间】:2013-06-07 13:33:40
【问题描述】:

如果我打开 Chrome 开发者工具控制台并更改 HTMLMediaElement.prototype.play,它就可以正常工作。

> HTMLMediaElement.prototype.play = function(){debugger;}
function (){debugger;}
> HTMLMediaElement.prototype.play
function (){debugger;}

但是,如果我从用户脚本更改它,该函数似乎总是恢复到本机实现。

> HTMLMediaElement.prototype.play
function play() { [native code] }

我已经验证了用户脚本可以正确加载,我什至尝试了一种丑陋的 setInterval 方法,看看至少它是否有效:

var myFunction = function(){debugger;};
window.setInterval(function(){
  if (window.HTMLMediaElement.prototype.play != myFunction)
    window.HTMLMediaElement.prototype.play = myFunction;
}, 900);

但即便如此,我最终还是会使用本机实现。

【问题讨论】:

    标签: google-chrome html5-video google-chrome-devtools userscripts


    【解决方案1】:

    来自Injecting JS functions into the page from a Greasemonkey script on Chrome

    与页面上运行的代码进行通信的唯一方式 Chrome 是通过 DOM 实现的,所以你必须使用像插入这样的 hack 带有您的代码的标签。请注意,如果您的 脚本需要在页面上的所有其他内容之前运行。

    编辑:Nice Alert 扩展是如何做到这一点的:

    function main () {
      // ...
      window.alert = function() {/* ... */};
      // ...
    }
    
    var script = document.createElement('script');
    script.appendChild(document.createTextNode('('+ main +')();'));
    (document.body || document.head || document.documentElement).appendChild(script);
    

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多