Problems in your code
- chrome.tabs.sendMessage() wrong Invocation pattern
- As complete code is not shared, i assume permissions are given to all pages, because manifest don't generate warnings for some set of values.
notification.onclick(function
createsUncaught TypeError: Property 'onclick' of object #<Notification> is not a function
error
Chrome.tabs.sendMessage
Invocation form of chrome.tabs.sendMessage({data: whatever_data}, tabId); should be chrome.tabs.sendMessage(tabId,{"data": "whatever_data"}); (Tab id followed by message).
notification.onclick(function
Use notification.onclick = (function(
assign handler to onclick property (because it is not a function)
After fixing above problems i got your script running.
manifest.json
Registered background scripts and given all permissions needed.
{
"name": "Message Passing",
"description": "This is used as a message passing",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"notifications",
"tabs"
]
}
background.js
Modified code to eliminate errors
//Created Notification
var notification = webkitNotifications.createNotification("icon.jpg", "title", "content");
//Added onclick property
notification.onclick = (function (e) {
//Created new tab
chrome.tabs.create({
url: "/src/view/notification.html"
}, function (tab) {
//just to make sure the tab is activated..
chrome.tabs.onUpdated.addListener(function (tabId) {
if (tabId == tab.id) {
//Send Mesage
chrome.tabs.sendMessage(tabId, {
"data": "whatever_data"
});
}
});
});
});
notification.show();
notification.html
Ensure there is no inline script
or <script>
tag to comply with CSP.
<html>
<head>
<script src="notification.js">
</script>
</head>
<body>
<p>This is a notification</p>
</body>
</html>
notification.js
Used your script with no changes!
chrome.extension.onMessage.addListener(function (msg, _, sendResponse) {
console.log(JSON.stringify(msg));
});
Output
You can see message(s) being received in your new tab
.