JavaScript编写Chrome扩展实现与浏览器的交互及时间(2)

{ // 这个字段将用在安装对话框,扩展管理界面,和store里面,弹出通知的标题 "name": "系统通知", // 扩展的版本用一个到4个数字来表示,中间用点隔开,必须在0到65535之间,非零数字不能0开头 "version": "1", // 描述扩种的一段字符串(不能是html或者其他格式,不能超过132个字符)。这个描述必须对浏览器扩展的管理界面和Chrome Web Store都合适。 "description": "Shows off desktop notifications, which are \"toast\" windows that pop up on the desktop.", // 一个或者多个图标来表示扩展,app,和皮肤 "icons": { "16": "16.png", // 应用的fa网页图标 "48": "48.png", // 应用管理页面需要这个图标 "128": "128.png" // 在webstore安装的时候使用 }, // 扩展或app将使用的一组权限 "permissions": ["tabs", "notifications"], // Manifest V2 用background属性取代了background_page // 这里指定了一个Javascript脚本 "background": { "scripts": ["background.js"] }, // Manifest version 1在Chrome18中已经被弃用了,这里应该指定为2 "manifest_version": 2, // manifest_version 2中,指定扩展程序包内可以在网页中使用的资源路径(相对于扩展程序包的根目录)需要使用该属性把资源列入白名单,插入的content script本身不需要加入白名单 "web_accessible_resources": [ "48.png" ] }

3.2、background.js

/** * 显示一个时间 notification */ function show() { var time = new Date().format('yyyy-MM-dd hh:mm:ss'); // 创建一个notification var notification = window.webkitNotifications.createNotification( '48.png', // 图片,在web_accessible_resources 中添加了 '现在的时间是:', // title time // body. ); // 显示notification notification.show(); } // 格式化时间函数 Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o)if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format; } // 测试浏览器是否支持 webkitNotifications if(window.webkitNotifications) { // 显示通知 show(); var interval = 0; // 弹出10次 var times = 10; // 创建定时器 var timer = setInterval(function() { interval++; // 10秒钟弹出一次 if (10 <= interval) { show(); interval = 0; times--; if(times <- 0) clearInterval(timer); } }, 1000); }

源代码

https://github.com/arthinking/google-plugins/tree/master/example/notifications

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzpfwd.html