简体   繁体   中英

JS - Window Focus and Bring to front in Firefox

I am trying to set up a tiny script for Firefox that runs in a javascript add-on (Greasemonkey). We have a queue that we monitor for arriving tickets, I coded something that is supposed to refresh the page every 2 minutes and do the following:

if tickets are found by this condition

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

then I want the following to happen:
- task-bar blinks with a message so it is visible
- if the window is focused it will display "Tickets in the queue!" alert right away
- if the window is not focused, it will wait for 10 seconds, if still not focused - display "Tickets in the queue!" alert and bring the window to front.

I've got the refresh and blinking part, but I cannot get the focus part to work... I've been looking around and I see that Firefox is having some "issues" with window.focus() and all the "bring to front", most of the code below is inspired by stuff I've found on this site.

Any input is appreciated! I am also opened to alternatives - in the end, what this needs to do is refresh, check the condition and notify if I am already looking at it or if it is not focused wait 10 seconds with a "soft notify" (blink) then bring it to the front if I don't notice it.

Regards,
Dan

{ 
    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);
    });

}

Here is the alternative I've used, works like a charm. Web API notifications.

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);
    });

}

In Windows operating system you cannot focus the window while the window of another process is focused. You have to use js-ctypes to get around this.

On Mac OS X and Linux, I'm not sure if you can make your process steal focus from another with the normal functions. But if you can't you can for sure use js-ctypes to get the job done.

Here is how to do it in Windows - https://stackoverflow.com/a/32038880/1828637

It is harder on Windows then on OS X and Linux. I have focused windows on all systems using js-ctypes. So if you can't find out how to do with the functions available to you, let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM