和浏览器的交互
1、书签
使用chrome.bookmarks模块来创建、组织和管理书签。也可参看 Override Pages,来创建一个可定制的书签管理器页面。
1.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"bookmarks"
],
...
}
对象和属性:
签是按照树状结构组织的,每个节点都是一个书签或者一组节点(每个书签夹可包含多个节点)。每个节点都对应一个BookmarkTreeNode 对象。
可以通过 chrome.bookmarks API来使用BookmarkTreeNode的属性。
例子:
创建了一个标题为 "Extension bookmarks"的书签夹。
chrome.bookmarks.create({'parentId': bookmarkBar.id,
'title': 'Extension bookmarks'},
function(newFolder) {
console.log("added folder: " + newFolder.title);
});
创建了一个指向扩展开发文档的书签。
chrome.bookmarks.create({'parentId': extensionsFolderId,
'title': 'Extensions doc',
'url': 'http://code.google.com/chrome/extensions'});
2、cookies
2.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"cookies",
"*:/
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



