使用 jQuery 和 GreaseMonkey 检测 Gmail 何时完成加载

发布于 2024-09-14 07:06:37 字数 446 浏览 3 评论 0原文

我正在尝试使用 GreaseMonkey 脚本向 Gmail 添加一些 jQuery 内容。添加 jQuery 功能效果很好,但问题是我无法真正检测到 Gmail 何时完成加载。

这基本上是发生的情况:

  1. 我刷新 Gmail
  2. 加载窗口启动
  3. GM 脚本在加载窗口期间启动 3 次 加载窗口
  4. 中的某些内容发生变化
  5. GM 脚本再次启动
  6. 页面发生变化
  7. GM 脚本最后一次启动
  8. Gmail 视图加载和完成

此时,一堆 JavaScript 已加载到 DOM 中,这些 JavaScript 可能会调用一些使用 AJAX 加载视图其余部分的函数。

我希望 jQuery 在第 8 步之后(当所有内容都完成加载后)执行一些操作。

有谁知道如何做到/检测到这一点?

I'm trying to add some jQuery stuff to Gmail using a GreaseMonkey script. Adding jQuery functionality works fine, but the problem is that I can't really detect when Gmail has finished loading.

This is basically what happens:

  1. I refresh Gmail
  2. The loading window starts
  3. The GM script starts 3 times, during the loading window
  4. Something in the loading window changes
  5. The GM script starts one more time
  6. The page changes
  7. The GM script starts for the last time
  8. Gmail view loads and finishes

At this point a bunch of JavaScript has been loaded into the DOM that probably calls on some functions that use AJAX to load the rest of the view.

I'd like for jQuery to do stuff after step 8, when everything has finished loading.

Does anyone know how to do/detect this?

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

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

发布评论

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

评论(1

瑾兮 2024-09-21 07:06:38

首先,您可以要求 Google 修复其 gmail-greasemonkey API - - 似乎每天都会打破更多。具体来说,registerViewChangeCallback() 将有助于解决方案,但它似乎已停止正常工作。

解决方法是延迟触发主要文档更改。以下代码似乎对我来说在 Firefox 上有效。它可能需要调整 iFrame 内容。

//
// ==UserScript==
// @name            Fire on page finished (with AJAX mods)
// @namespace       Gmail
// @description     This script shows one way to wait for an AJAX-heavy page to load.
// @include         http://mail.google.com/*
// @include         https://mail.google.com/*
// ==/UserScript==
//

if (window.top != window.self)  //don't run on frames or iframes
    return;


var zGbl_PageChangedByAJAX_Timer = '';


window.addEventListener ("load", LocalMain, false);


function LocalMain ()
{
    if (typeof zGbl_PageChangedByAJAX_Timer == "number")
    {
        clearTimeout (zGbl_PageChangedByAJAX_Timer);
        zGbl_PageChangedByAJAX_Timer  = '';
    }

    document.body.addEventListener ("DOMNodeInserted", PageBitHasLoaded, false);
}


function PageBitHasLoaded (zEvent)
{
    if (typeof zGbl_PageChangedByAJAX_Timer == "number")
    {
        clearTimeout (zGbl_PageChangedByAJAX_Timer);
        zGbl_PageChangedByAJAX_Timer  = '';
    }
    zGbl_PageChangedByAJAX_Timer      = setTimeout (function() {HandlePageChange (); }, 666);
}


function HandlePageChange ()
{
    removeEventListener ("DOMNodeInserted", PageBitHasLoaded, false);

    alert ('Page has finished loading.');
}

First, you can yell at Google to fix their gmail-greasemonkey API -- which seems to break more each day. Specifically, registerViewChangeCallback() would facilitate a solution but it seems to have stopped working correctly.

A workaround would be to delay-fire off main document changes. The following code seems to work for me, on Firefox. It might need tweaking for iFrame content.

//
// ==UserScript==
// @name            Fire on page finished (with AJAX mods)
// @namespace       Gmail
// @description     This script shows one way to wait for an AJAX-heavy page to load.
// @include         http://mail.google.com/*
// @include         https://mail.google.com/*
// ==/UserScript==
//

if (window.top != window.self)  //don't run on frames or iframes
    return;


var zGbl_PageChangedByAJAX_Timer = '';


window.addEventListener ("load", LocalMain, false);


function LocalMain ()
{
    if (typeof zGbl_PageChangedByAJAX_Timer == "number")
    {
        clearTimeout (zGbl_PageChangedByAJAX_Timer);
        zGbl_PageChangedByAJAX_Timer  = '';
    }

    document.body.addEventListener ("DOMNodeInserted", PageBitHasLoaded, false);
}


function PageBitHasLoaded (zEvent)
{
    if (typeof zGbl_PageChangedByAJAX_Timer == "number")
    {
        clearTimeout (zGbl_PageChangedByAJAX_Timer);
        zGbl_PageChangedByAJAX_Timer  = '';
    }
    zGbl_PageChangedByAJAX_Timer      = setTimeout (function() {HandlePageChange (); }, 666);
}


function HandlePageChange ()
{
    removeEventListener ("DOMNodeInserted", PageBitHasLoaded, false);

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