【问题标题】:JS - Window Focus and Bring to front in FirefoxJS - 窗口焦点并在 Firefox 中突出显示
【发布时间】:2016-03-22 17:46:12
【问题描述】:

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

如果在这个条件下找到票

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

那么我希望发生以下情况:
- 任务栏闪烁并显示一条消息,因此可见
- 如果窗口被聚焦,它将显示“Tickets in the queue!”立即报警
- 如果窗口没有聚焦,它将等待 10 秒,如果仍然没有聚焦 - 显示“Tickets in the queue!”提醒并把窗户放在前面。

我有刷新和闪烁部分,但我无法让焦点部分工作......我一直在环顾四周,我发现 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);
    });

}

【问题讨论】:

    标签: javascript jquery firefox firefox-addon


    【解决方案1】:

    这是我用过的替代方案,就像一个魅力。 Web API 通知。

    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 上的另一个应用程序,这是否会将 Firefox 带到最前面?
    【解决方案2】:

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

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

    这是在 Windows 中的操作方法 - https://stackoverflow.com/a/32038880/1828637

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

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多