Reputation: 85
I am writing a very simple chrome extension. I need the ability to fire off a specific url based on the data that is passed to the notification. Using ajax, or whatever, I would dynamically create a notification call that the user would click to launch the url. The extension also reads in omnibox data to launch a similar URL.
Manifest.json
{
"name": "Quick Search ",
"description": "Quickly search",
"omnibox": {
"keyword": "#"
},
"permissions": [
"contextMenus",
"notifications",
"http://url.ineed.com/"
],
"icons": {
"16": "rugbyball.png",
"32": "rugbyball32.png",
"128": "rugbyball128.png"
},
"background": {
"scripts": ["background.js"]
},
"browser_action":{
"default_icon":"rugbyball.png"
},
"version": "1.0",
"minimum_chrome_version": "9",
"manifest_version": 2
}
background.js
function resetDefaultSuggestion() {
chrome.omnibox.setDefaultSuggestion({
description: '#: Search for %s'
});
}
resetDefaultSuggestion();
chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
// Suggestion code will end up here.
});
chrome.omnibox.onInputCancelled.addListener(function() {
resetDefaultSuggestion();
});
function navigate(url) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: url});
});
}
chrome.omnibox.onInputEntered.addListener(function(text) {
navigate("https://url.ineed.com?param=" + text);
});
chrome.notifications.onClicked.addListener(closeNotification);
function closeNotification(evt) {
console.log('click event ' + evt);
navigate("https://url.ineed.com/");
}
var Notification=(function (){
var notification= null ;
return {
display: function (opt){
notification=chrome.notifications.create(opt);
},
hide: function (){
notification.close();
}
};
})();
chrome.browserAction.onClicked.addListener( function (windowId){
var opt = {
type: "basic" ,
title: "Message" ,
message: "New " ,
iconUrl: "rugbyball128.png"
};
Notification.display(opt);
});
tl;dr - Need a way to pass a param to the notification and open a url with that data.
Upvotes: 0
Views: 1011
Reputation: 77502
You can use unique self-deregistering listeners for clicks.
function createNotification(url, createdCallback) { // callback is optional
var opt = { /* ... */ };
chrome.notifications.create(opt, function(createdId) {
var handler = function(id) {
if(id == createdId) {
navigate(url);
chrome.notifications.clear(id);
chrome.notifications.onClicked.removeListener(handler);
}
};
chrome.notifications.onClicked.addListener(handler);
if(typeof createdCallback == "function") createdCallback();
});
}
Upvotes: 3