【问题标题】:redefine native browsers function in Firefox from userscript space从用户脚本空间重新定义 Firefox 中的原生浏览器功能
【发布时间】:2013-12-02 19:47:43
【问题描述】:

下面的代码可以在 FireFox 原生函数中重新定义(在 Chrome 中你可以只做 document.func = newfunc 或与下面相同但没有注入代码的事情),注入对于小的新函数是可以的,但如果需要的话为了与用户脚本中的其他函数或变量进行通信,需要注入用户脚本的整个代码,

所以我正在寻找一种方法,可以在不注入的情况下从 UserScript 的空间覆盖\重新定义\等 FireFox 中的本机函数。

// ==UserScript==
// ==/UserScript==

function doh4x()
{
    window.history.__proto__.pushState = function(a, b, url) {window.location.href = url;}
}

function inject(func) 
{
    var source = func.toString();
    var script = document.createElement('script');
    script.text = "("+ source +")()";
    document.head.appendChild(script);
}

inject(doh4x);

【问题讨论】:

    标签: javascript firefox greasemonkey redefine


    【解决方案1】:

    使用unsafeWindow


    unsafeWindow.history.__proto__.pushState = function (a, b, url) {
        window.location.href = url;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2021-09-13
      • 1970-01-01
      • 2012-09-12
      • 2012-09-19
      • 2016-10-11
      相关资源
      最近更新 更多