24. / 创建一个简单的文字通知:
/
var notification = webkitNotifications.createNotification(
'48.png', / icon url - can be relative
/
'Hello!', / notification title
/
'Lorem ipsum...' / notification body text
/
);
/ 或者创建一个 HTML 通知:
/
var notification =
webkitNotifications.createHTMLNotification(
'notification.html' / html url - can be relative
/
);
/ 显示通知
/
notification.show();
57. 从content script建立长连接通道,发送和接受消息
contentscript.js
================
var port = chrome.extension.connect({name: "knockknock"});
port.postMessage({joke: "Knock knock"});
port.onMessage.addListener(function(msg) {
if (msg.question == "Who's there?")
port.postMessage({answer: "Madame"});
else if (msg.question == "Madame who?")
port.postMessage({answer: "Madame... Bovary"});
});
58. 处理长连接通道
chrome.extension.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.onMessage.addListener(function(msg) {
if (msg.joke == "Knock knock")
port.postMessage({question: "Who's there?"});
else if (msg.answer == "Madame")
port.postMessage({question: "Madame who?"});
else if (msg.answer == "Madame... Bovary")
port.postMessage({question: "I don't get it."});
});
});
59. 扩展间的消息传递
/ For simple requests:
/
chrome.extension.onRequestExternal.addListener(
function(request, sender, sendResponse) {
if (sender.id == blacklistedExtension)
sendResponse({}); / don't allow this extension access
/
else if (request.getTargetData)
sendResponse({targetData: targetData});
else if (request.activateLasers) {
var success = activateLasers();
sendResponse({activateLasers: success});
}
});
60. 扩展间的消息传递
/ For long-lived connections:
/
chrome.extension.onConnectExternal.addListener(
function(port) {
port.onMessage.addListener(function(msg) {
/ See other examples for
/
/ sample onMessage handlers.
/
});
});
61. 扩展间的消息传递
/ The ID of the extension we want to talk to.
/
var laserExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
/ Make a simple request:
/
chrome.extension.sendRequest(laserExtensionId, {getTargetData: true},
function(response) {
if (targetInRange(response.targetData))
chrome.extension.sendRequest(laserExtensionId, {activateLasers: true});
});
/ Start a long-running conversation:
/
var port = chrome.extension.connect(laserExtensionId);
port.postMessage(...);