Chrome插件开发API笔记
chrome.runtime.* 通信
chrome.runtime.sendMessage(extensionId, message, options, callback);
1
2
3
4chrome.runtime.sendMessage('Hello', function(response) {
console.log(response);
document.write(response);
});chrome.runtime.onMessage.addListener(callback)
1
2
3
4
5chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
if(message == 'Hello') {
sendResponse('I recive it...');
}
});
chrome.storage.StorageArea.* 存储API, “permissions”声明 “storage”
chrome.storage.StorageArea.get(keys, function(result) { console.log(result) });
keys
可以是字符串/数组/对象/
chrome.storage.StorageArea.getByetsInUse(keys, function(bytes){....});
- 该方法获取一个数据或者多个数据所占的总空间,返回结果的单位是字节
chrome.storage.StorageArea.set(items, function(){...});
- 该方法为写入数据
items
为对象类型,形式为 键/值 对.
chrome.storage.StorageArea.remove(keys, function(){....});
- 该方法为删除数据
- keys 可以是字符串也可以是包含多个字符串的数组
chrome.storage.StorageArea.clear(keys, function(){....});
- 清空所有数据
chrome.storage.onChanged 当存储区的数据发生改变时, 此方法会被激发
chrome.storage.onChanged.addListener(function(changs, areaName) {...});
changes
字典对象, 包含oldValue
/newValue
两个属性StorageArea
为local
/sync
chrome.context.Menus.* 将扩展加入到右键菜单
chrome.context.Menus.create({type: "", title: "", id:"",...., parentId: "..."})
type
可以是normal
,radio
,checkbox
(带checked属性, 值为false/true), ….parentId
指定父级contexts
显示条件, [“…”], 数组中可以是all, page, frame, selection, link, editable, image, video, audio, launcher
. eg: contexts: [“link”] //右键链接到时候有 . 默认为page
, 即所有页面onclick
绑定函数- 函数接收两个数据,
info
/tab
info
接收数据, 主要属性,id
值为当前contextMenu的id,selectionText
数据
- 函数接收两个数据,
*.update
*.remove
chrome.browserAction.setIcon(details, callback)
...setIcon({path:{'19': 'imagepth'}})
chrome.browserAction.setBadge**
1 | chrome.browserAction.setBadgeBackgroundColor({color: "green"}); |
chrome.tabs.*
chrome.tabs.onCreated.addListener(function(tab) {...})
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {...})
chrome.tabs.onMoved.addListener(function(tab, moveInfo) {...})
chrome.tabs.onActivated.addListener(function(activeInfo) {...})
chrome.tabs.onHighlighted.addListener(function(highlightInfo) {...})
active
指标签在当前窗口中正被显示highlight
只是标签的颜色被显示成了白色–如果此标签没有被选中, 正常情况是灰色
chrome.tabs.onDetached.addListener(function(tabId, detachInfo)){...}
onDetached
是标签脱离窗口时,所触发的事件chrome.tabs.onAttached.addListener(function(tabId,attachInfo)){...}
onAttached
是标签附着到窗口上时触发的事件
chrome.downloads.*
chrome.downloads.download(options, callback);
options 结构
1
2
3
4
5
6
7
8
9{
url: 下载文件的url
filename: 保存的文件名,
conflictAction: 重命名文件的处理方式,
saveAs: 是否弹出另存为窗口,
method: 请求方式(GET或者POST),
headers: 自定义headers数组
body: POST的数据
}conflictAction
的值只能是uniquify
在文件后面添加带括号的序号,以保证文件名唯一overwrite
覆盖prompt
给出提示, 让用户决定是覆盖还是重命名