JS - 在 Firefox 中窗口焦点并置于最前面
JS - Window Focus and Bring to front in Firefox
我正在尝试为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 的系统上。因此,如果您找不到如何使用可用的功能,请告诉我。
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 如何在下面的ES6循环中获得前面的文本
- 我可以获得相对于被点击元素的确切点击位置吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 序列化数据属性中对象的最可靠方法
- FabricJS-组选择使对象在选择时处于最前面
- JS - 在 Firefox 中窗口焦点并置于最前面
- 如何创建一组从属选择列表,其中连续列表依赖于前面列表选项的选择
- Kadane 的最大子数组算法是否适用于所有正整数数组?
- 将过滤条件应用于地图的最干净方法
- AngularJS 1.3-为什么表单在加载时有效,适用于长度最小的输入
- 将弹出窗口置于最前面
- 最干净的方式,使模态依赖于一个功能?AngularJS
- 如何设置Chrome应用's的最大宽度窄于最小化,最大化,关闭按钮的宽度
- 在MongoDB中,将函数应用于集合中的所有对象的最有效方法是什么?
- 确定子元素相对于其父元素的索引的最简单方法
- 嵌套的悬停事件,仅适用于最直接的悬停元素
- 将JQuery对话框置于最前面
- 我如何使这个JavaScript只适用于最小宽度为1036px的浏览器上的用户