简体   繁体   English

javascript中的通知,无需用户输入

[英]Notifications in javascript without user input

I've been trying to figure out a way to use notifications on a background process and couldnt find anything online about it. 我一直在尝试找出一种在后台进程中使用通知的方法,并且无法在线找到有关它的任何信息。 So, I figured out one way around it and wanted to share (Not sure if this is the best way to go about doing this but here goes:) 因此,我想出了一种解决方法,并希望与他人分享(不知道这是否是实现此目的的最佳方法,但是可以:)

Problem: I want to notify the user of new info when the page is running but in the background (blurred). 问题:当页面正在运行但在后台(模糊)时,我想通知用户新信息。 I could use alert('new info!'); 我可以使用alert('new info!'); to get the taskbar icon to flash, but then you have to manually dismiss it (tried it and it's hella annoying). 要使任务栏图标闪烁,但是您必须手动将其关闭(尝试过后,它会令人讨厌)。 I really liked the notifications, but they only work if the user performs an action, so not helpful... 我真的很喜欢通知,但是通知仅在用户执行操作时才起作用,因此无济于事...

My solution: I made a chrome extension that runs in the background and triggers the notifications. 我的解决方案:我制作了一个Chrome扩展程序,该程序在后台运行并触发通知。 It's a little limited in scope as you need to have chrome to do it, but it does what i need it to, and for the purposes of the problem i'm working on, i can just make my user group use chrome ;D 由于您需要使用chrome来做,所以范围上有一点限制,但是它确实满足了我的需要,出于我要解决的问题,我可以让我的用户组使用chrome; D

The specifics: The extension only has two components, the manifest and a script. 细节:扩展名只有两个部分,清单和脚本。 Currently, i setup the manifest so that it only works on my site using the match identifier... and i set the permissions to include notifications. 目前,我设置了清单,以便仅使用匹配标识符在我的网站上工作...并且我将权限设置为包括通知。

The JS script has a window.setinterval that looks for an element in the page with the id NOTIFIER . JS脚本具有一个window.setinterval,用于在页面中查找ID为NOTIFIER的元素。 If it's empty, it does nothing, otherwise it creates a notification based on the content and then clears the content to prevent showing the same notification multiple times... (I tried using .onchange for that element, but couldn't get the event to trigger... I'd prefer to do this on an event rather then setInterval) 如果为空,则不执行任何操作,否则将基于内容创建一个通知,然后清除内容以防止多次显示相同的通知...(我尝试对该元素使用.onchange ,但无法获取该事件触发...我更愿意在事件上执行此操作,而不是setInterval)

Notify.js Notify.js

function onExtLoad() {
  var timer = setInterval(refresh,1000);
}

document.addEventListener('DOMContentLoaded', onExtLoad());

function refresh() {
if (document.getElementById('NOTIFIER').innerHTML == "") {
    //do nothing?
} else {
    var notification = webkitNotifications.createNotification("",
        "You got a new message",
        document.getElementById('NOTIFIER').innerHTML);
    notification.show();
    document.getElementById('NOTIFIER').innerHTML = "";
}

}

Then, all i need to do is have the JS on the page control when it adds info the the NOTIFIER and voila! 然后,我需要做的就是在页面控件上添加JS时添加NOTIFIER和voila信息! notifications! 通知!

Hope this helps someone else. 希望这对其他人有帮助。

@ExpertSystem: I messed around with the MutationObserver but I can only get it to trigger once. @ExpertSystem:我把MutationObserver弄乱了,但是我只能使其触发一次。 Here's a JSFiddle: http://jsfiddle.net/BTX8x/1/ Am I missing something? 这是一个JSFiddle: http : //jsfiddle.net/BTX8x/1/我缺少什么吗? Is there a way to reset it? 有办法重设吗?

EDIT: Figured it out, i needed subtree:true 编辑:弄清楚了,我需要subtree:true

I hope I won't be telling something stupid, but from where I see it (and remember from school) that's basically how http works : a request is sent to the server, which issues a response eventually after executing some server-side code. 我希望我不会讲愚蠢的东西,但是从我看到的地方(并记得在学校里),这基本上是http的工作原理:请求发送到服务器,服务器在执行一些服务器端代码后最终发出响应。 Basically you're asking for a "PUSH" functionality from server to client, and in that case you can't make use of HTTP. 基本上,您是在要求从服务器到客户端的“推送”功能,在这种情况下,您将无法使用HTTP。 Some tricks exist to work around this limitation, but basically they're all issuing requests at a certain frequency (Dave's answer does exactly that). 有一些技巧可以解决此限制,但基本上,它们都是以一定的频率发出请求的(Dave的答案正是这样)。 If your site doesn't change that much, that means a lot of requests are issued for no reason (nothing has changed), consuming bandwith for nothing. 如果您的网站变化不大,则意味着无缘无故地发出了很多请求(什么都没有改变),浪费了带宽。

From what I know, the answer to this is called Websockets, which are supported by recent browsers only. 据我所知,答案是Websockets,仅最近的浏览器支持。 I never had the chance to use it though so I couldn't tell much more about it. 我从来没有机会使用它,所以我无法透露更多有关它的信息。 This allows full duplex communication, thus allowing server to "push" data to the client. 这允许全双工通信,从而允许服务器将数据“推送”到客户端。 I guess that's what SO uses for "new message" notifications (top left of the screen - you see immediately when you receive a new message) 我想这就是SO用于“新消息”通知的方式(屏幕左上方-收到新消息时您会立即看到)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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