Chrome 扩展通知 - 如何向通知发送值并重新使用它

Chrome Extensions Notifications - How do I send a value to the notification and re-use it

我正在编写一个非常简单的 chrome 扩展。我需要能够根据传递给通知的数据触发特定的 url。使用 ajax 或其他方式,我将动态创建一个通知调用,用户将单击该通知调用以启动 url。该扩展程序还读取多功能框数据以启动类似的 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 - 需要一种方法将参数传递给通知并使用该数据打开 url。

您可以使用独特的自注销侦听器进行点击。

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();
  });
}