如何在Chrome扩展程序中实现带有声音的通知弹出窗口。

就像Checker Plus for Gmail

最佳答案

您可以使用以下代码作为在桌面通知中播放声音的参考,它结合使用<audio>标记和Desktop Notifications

示范

manifest.json

已注册的通知权限和带有清单文件的后台页面。

{
    "name": "Notification with Audio",
    "description": "http://stackoverflow.com/questions/14917531/how-to-implement-a-notification-popup-with-sound-in-chrome-extension",
    "manifest_version": 2,
    "version": "1",
    "permissions": [
        "notifications"
    ],
    "background": {
        "scripts": [
            "background.js"
        ]
    }
}


background.js

从后台应用程序创建了一个通知页面。

// create a HTML notification:
var notification = webkitNotifications.createHTMLNotification(
    'notification.html' // html url - can be relative
);

// Then show the notification.
notification.show();


notification.html

播放一些随机的歌曲

<html>
    <body>
        <p>Some Nice Text While Playing Song.. </p>
        <audio autoplay>
        <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3" type="audio/mpeg" />
        <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg" type="audio/ogg" />
        </audio>
    </body>
</html>


参考资料


Notifications

10-08 08:38
查看更多