JS - 在 Firefox 中窗口焦点并置于最前面

JS - Window Focus and Bring to front in Firefox

本文关键字:于最 前面 焦点 窗口 Firefox JS      更新时间:2023-09-26

我正在尝试为Firefox设置一个小脚本,该脚本在javascript插件(Greasemonkey)中运行。我们有一个队列来监控到达的票证,我编写了一些应该每 2 分钟刷新一次页面并执行以下操作:

如果通过此条件找到票证

if (isEmpty($('#incident_table > tbody'))&isEmpty($('#task_table > tbody')))

那么我希望发生以下情况:
- 任务栏闪烁并显示一条消息,因此可见
- 如果窗口聚焦,它将立即显示"排队中的门票!"警报- 如果窗口没有聚焦,
它将等待10秒,如果仍然没有聚焦 - 显示"排队中的门票!"警报并将窗口放在前面。

我有刷新和闪烁部分,但我无法让焦点部分工作......我一直在环顾四周,我看到 Firefox 在 window.focus() 和所有"带到前面"方面存在一些"问题",下面的大部分代码都受到我在这个网站上找到的东西的启发。

任何意见不胜感激!我也对替代方案持开放态度 - 最后,这需要做的是刷新,检查条件并通知我是否已经在看它,或者如果它没有聚焦,请等待 10 秒钟"软通知"(闪烁),然后如果我没有注意到它,请将其放在前面。

问候

{ 
    newExcitingAlerts = (function () {
        var oldTitle = document.title;
        var msg = "***NEW***";
        var timeoutId;
        var blink = function() { document.title = document.title == msg ? 'Tickets in queue!' : msg; };
        var clear = function() {
            clearInterval(timeoutId);
            document.title = oldTitle;
            window.onmousemove = null;
            timeoutId = null;
        };
        return function () {
            if (!timeoutId) {
                timeoutId = setInterval(blink, 1000);
                window.onmousemove = clear;
            }
        };
    }());

    $(document).ready(function(){
        function isEmpty( el ){
            return !$.trim(el.html());
        }
        if (isEmpty($('#incident_table > tbody'))&isEmpty($('#task_table > tbody'))) {
        }

        else{
            newExcitingAlerts();
        }
        setTimeout(function() {
            location.reload();
        }, 120000);
    });
}

这是我使用的替代方案,就像一个魅力。网络接口通知。

document.addEventListener('DOMContentLoaded', function () {
    if (Notification.permission !== "granted")
        Notification.requestPermission();
});
function notifyMe() {
    if (!Notification) {
        alert('Desktop notifications not available in your browser. Try Chromium.');
        return;
    }
    if (Notification.permission !== "granted")
        Notification.requestPermission();
    else {
        var notification = new Notification('Tickets in queue!', {
            icon: 'http://siliconangle.com/files/2014/05/servicenow-icon.png',
            body: "There are new tickets in queue, please acknowledge!",
        });
        notification.onclick = function () {
            window.open("https://cchprod.service-now.com/task_list.do?sysparm_query=assignment_group%3D3139519437b7f1009654261953990e1f^ORassignment_group%3D31de85e337818a00ef8898a543990e99^ORassignment_group%3Da40029e937ec420065aa261953990eb5^ORassignment_group%3De903ad2d37ec420065aa261953990ecb^ORassignment_group%3Dd25fe5323779c24065aa261953990e54^ORassignment_group%3D508639363779c24065aa261953990e29^ORassignment_group%3D51fe5a37379e0a00ef8898a543990ea2^ORassignment_group%3D3d8171b23779c24065aa261953990e21^ORassignment_group%3Decfe5a37379e0a00ef8898a543990e6c^ORassignment_group%3D48c0b9723779c24065aa261953990e5d^ORassignment_group%3De5fde9fe3739c24065aa261953990e75^ORassignment_group%3D15fe5a37379e0a00ef8898a543990e99^ORassignment_group%3D15fe5a37379e0a00ef8898a543990ea7^ORassignment_group%3D1ed3f1f23779c24065aa261953990e47^active%3Dtrue^sys_class_name%3Dincident^ORsys_class_name%3Dsc_req_item^assigned_toISEMPTY&sysparm_first_row=1&sysparm_view=");
        };
    }
}
{
    newExcitingAlerts = (function () {
        var oldTitle = document.title;
        var msg = "***NEW***";
        var timeoutId;
        var blink = function() {
            document.title = document.title == msg ? 'Tickets in queue!' : msg;
        };
        var clear = function() {
            clearInterval(timeoutId);
            document.title = oldTitle;
            window.onmousemove = null;
            timeoutId = null;
        };
        return function () {
            if (!timeoutId) {
                timeoutId = setInterval(blink, 1000);
                window.onmousemove = clear;
            }
        };
    }
                         ());
    $(document).ready(function () {
        function isEmpty(el) {
            return !$.trim(el.html());
        }
        var x = document.getElementById("task_table").getAttribute("grand_total_rows");
        if( x != "0" ) {
            newExcitingAlerts();
            notifyMe();
        }
        else
        {
        }
        setTimeout(function() {
            location.reload();
        }
                   , 120000);
    });
}

在 Windows 操作系统中,当另一个进程的窗口聚焦时,您无法聚焦窗口。你必须使用 js-ctypes 来解决这个问题。

在Mac OS X和Linux上,我不确定你是否可以让你的进程从另一个具有正常功能的过程中窃取焦点。但是如果你不能,你肯定可以使用js-ctypes来完成工作。

以下是在Windows中执行此操作的方法 - https://stackoverflow.com/a/32038880/1828637

在Windows上比在OS X和Linux上更难。我已经将窗口集中在所有使用 js-ctypes 的系统上。因此,如果您找不到如何使用可用的功能,请告诉我。