在 Chrome 中通过用户脚本替换页面功能?

发布于 2024-09-16 04:13:52 字数 1070 浏览 6 评论 0原文

我创建了一个 Greasemonkey 脚本来替换页面函数:

(function() {
    var oldFunc = func;
    func = function() {
        oldFunc();
        // other stuff
    };
)();

我一直想让这个脚本在 Gresemonkey 和 Chrome 中工作,并且了解到 Chrome 不支持 unsafeWindow,因此一直在注入该脚本通过将函数转换为字符串并使用 setTimeout 进入页面:

setTimeout("(function(){var old=func;func=function(){old();/*other stuff*/}}())", 0);

这在 Greasemonkey 中工作得很好,但在 Chrome 中却什么也没有提供。它显然没有触发,但日志中也没有出现任何内容。我尝试过许多其他注射方式,但没有任何效果。 (为了测试,我还在函数顶部添加了一个 alert() 。)

  1. setTimeout(..., 0) — 无
  2. 在作用域内运行函数,没有注入到页面中 - 没有警报,但控制台中“func未定义”
  3. document.href = "javascript:..." - 没有
  4. document.body.addEventListener("load" , ...) — 无所有

这些都与 @run-at document-end 一起使用。

不幸的是,这是我的第一个 Chrome 用户脚本,即使经过一个小时左右的谷歌搜索和探索,我也不知道如何调试它。有什么帮助吗?

实际脚本,当前在 Greasemonkey 中运行: http://pastebin.com/HtLVjYHg

I've created a Greasemonkey script which replaces a page function:

(function() {
    var oldFunc = func;
    func = function() {
        oldFunc();
        // other stuff
    };
)();

I had always intended this script to work in both Gresemonkey and Chrome and had read that Chrome doesn't support unsafeWindow, so have been injecting the script into the page by converting the function to a string and using setTimeout:

setTimeout("(function(){var old=func;func=function(){old();/*other stuff*/}}())", 0);

This works perfectly well in Greasemonkey, but gives me absolutely nothing in Chrome. It clearly isn't firing, but nothing appears in the log, either. I have tried a number of other injection styles, but can't get anything to work. (For testing, I also added an alert() to the top of the function.)

  1. setTimeout(..., 0) — nothing
  2. Running the function in-scope, without injection into the page — no alert, but "func is not defined" in console
  3. document.href = "javascript:..." — nothing
  4. document.body.addEventListener("load", ...) — nothing

All of these are with @run-at document-end.

Unfortunately, this being my first Chrome userscript, I'm at a loss as to how to debug it, even after an hour or so of Googling and poking around SO. Any help?

Actual script, currently working in Greasemonkey: http://pastebin.com/HtLVjYHg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-09-23 04:13:52

最后,我发现在 Chrome 中有效的唯一技术是使用 DOM 方法创建

function embed() {
   var oldFunc = window.func;

    window.func = function() {
        oldFunc();

        // other stuff
    };
}

var inject = document.createElement("script");

inject.setAttribute("type", "text/javascript");
inject.appendChild(document.createTextNode("(" + embed + ")()"));

document.body.appendChild(inject);

最终脚本:http://userscripts.org/scripts/review/84394

In the end, the only technique I found which worked in Chrome was to create a <script> node using DOM methods. Fortunately, this seems to work equally well in Firefox and Opera and is made fairly painless by <func>.toString():

function embed() {
   var oldFunc = window.func;

    window.func = function() {
        oldFunc();

        // other stuff
    };
}

var inject = document.createElement("script");

inject.setAttribute("type", "text/javascript");
inject.appendChild(document.createTextNode("(" + embed + ")()"));

document.body.appendChild(inject);

Final script: http://userscripts.org/scripts/review/84394

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文