fe-notification.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * 桌面提醒
  3. * @author zhaoxianlie
  4. */
  5. baidu.feNotification = (function(){
  6. /**
  7. * html式,可以简单的用查询变量传递参数
  8. * @param {string} notify_file 提醒页面路径:相对路径
  9. */
  10. var notifyHtml = function(notify_file) {
  11. var encode = encodeURIComponent;
  12. var notification = webkitNotifications.createHTMLNotification(
  13. chrome.extension.getURL(notify_file)
  14. );
  15. notification.show();
  16. return notification;
  17. };
  18. /**
  19. * 文本格式,可以设置一个图标和标题
  20. * @param {Object} options
  21. * @config {string} type notification的类型,可选值:html、text
  22. * @config {string} icon 图标
  23. * @config {string} title 标题
  24. * @config {string} message 内容
  25. */
  26. var notifyText = function(options){
  27. if(!options.icon) {
  28. options.icon = "static/img/fe-48.png";
  29. }
  30. if(!options.title) {
  31. options.title = "\u5C0F\u63D0\u793A";
  32. }
  33. //创建提醒
  34. var notification = webkitNotifications.createNotification(
  35. chrome.extension.getURL(options.icon),
  36. options.title,
  37. options.message
  38. );
  39. notification.show();
  40. //是否配置了自动关闭
  41. if(options.autoClose !== false) {
  42. // 显示完之后默认5秒关闭,具体还要看用户是否进行了自定义配置
  43. notification.ondisplay = function(e) {
  44. var userComstomNotificationTime = localStorage.getItem("opt_item_notification");
  45. setTimeout(function() { notification.cancel(); }, userComstomNotificationTime || 5000);
  46. }
  47. }
  48. return notification;
  49. };
  50. return {
  51. notifyHtml : notifyHtml,
  52. notifyText : notifyText
  53. };
  54. })();