尝试检测字符以确定页面是否应该重新加载。 (Safari 扩展)

发布于 2024-10-02 18:38:46 字数 853 浏览 1 评论 0原文

大家都知道,我对 JavaScript 比较陌生(而且我一直在尝试用纯 JS 做事,而不是使用 jQuery 等)。在我签约的地方,我们有一个 Exchange 服务器,我使用网络邮件客户端。

为了防止它在没有任何活动时超时,我开始组装一个 Safari 扩展,因为我可以使用一个扩展将 JS 注入到页面中。我认为这将是一个很好的练习,因为我正在从头开始学习 JS。

好吧,这部分是有效的,除了当你回复或发送新电子邮件时,所以我尝试添加第二位代码来防止这种情况,但它仍然会刷新。我不明白为什么。我将不胜感激任何帮助!如果我要回复或回复全部,我还必须添加规则,但我认为如果我能让这个工作正常,我就能让这些工作顺利进行。

这是我的代码:

if (location.host === "mail.exmx.net") {
    /* The Domain is https://mail.exmx.net/owa/?ae=Folder&t=IPF.Note */

    var timer = setTimeout(function() {
        location.reload( );
    }, 30000 /* 30 Seconds used just as a test to speed up waiting time*/ );
}

else (location.href.indexOf("?ae=Item&t=IPM.Note&a=New") != -1) {
    /* my attempt at using a static part of the string that's in the location bar when a new email is created */
    clearTimeout(timer);
}

Just so everyone knows, I'm relatively new to JavaScript, (and I've been trying to do things in pure JS rather than using jQuery, etc.). At a place I contract for, we have an Exchange server, I use the webmail client.

To prevent it from timing me out when there isn't any activity, I started putting together a Safari Extension since I could inject JS into a page using one. And I thought it would be a good exercise since I'm learning JS from scratch.

Well, that part of it works, except for when you're replying, or sending a new email, so I tried adding in the second bit of code to prevent that, but it still refreshes. I can't figure out why. I would appreciate any help! I am also going to have to add in rules for if I'm replying, or replying all, but I think if I can get this working, I can get those going to.

So here's my code:

if (location.host === "mail.exmx.net") {
    /* The Domain is https://mail.exmx.net/owa/?ae=Folder&t=IPF.Note */

    var timer = setTimeout(function() {
        location.reload( );
    }, 30000 /* 30 Seconds used just as a test to speed up waiting time*/ );
}

else (location.href.indexOf("?ae=Item&t=IPM.Note&a=New") != -1) {
    /* my attempt at using a static part of the string that's in the location bar when a new email is created */
    clearTimeout(timer);
}

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

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

发布评论

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

评论(1

み零 2024-10-09 18:38:46

你的代码很奇怪。您需要 else if,而不是 else

当然,您可以仅在两个条件都设置的情况下设置计时器(而不是设置计时器,然后清除它):

if (location.host == "mail.exmx.net" && location.href.indexOf("?ae=Item&t=IPM.Note&a=New") == -1) {

    /* The Domain is https://mail.exmx.net/owa/?ae=Folder&t=IPF.Note */

    var timer = setTimeout(function() {
        location.reload( );
    }, 30000 /* 30 Seconds used just as a test to speed up waiting time*/ );
}

Your code is wonky. Instead of else, you need else if.

Of course, you could set the timer only if both conditions are set (instead of setting the timer, then clearing it):

if (location.host == "mail.exmx.net" && location.href.indexOf("?ae=Item&t=IPM.Note&a=New") == -1) {

    /* The Domain is https://mail.exmx.net/owa/?ae=Folder&t=IPF.Note */

    var timer = setTimeout(function() {
        location.reload( );
    }, 30000 /* 30 Seconds used just as a test to speed up waiting time*/ );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文