从用户脚本空间重新定义Firefox中的原生浏览器功能

redefine native browsers function in Firefox from userscript space

本文关键字:Firefox 原生 功能 浏览器 定义 脚本 用户 空间 新定义      更新时间:2024-02-19

在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);

使用unsafeWindow


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