简体   繁体   English

在Chrome扩展程序中调用notify函数时,打开定义的其他页面?

[英]Open different page as defined when calling notify function in chrome extension?

In my code, I wish to be able to call the function notify each time with a different link, and the specific notification created opens a new tab with that page when clicked Note: I'm very confused by the documentation, please don't just link me to that, I really want an explanation with an example. 在我的代码中,我希望能够每次使用不同的链接来调用notify函数,并且在单击该通知时创建的特定通知会打开一个新标签页,并带有该页面。只是链接到我,我真的想要一个例子做一个解释。 Thanks 谢谢

This is what I have so far: 这是我到目前为止的内容:

function notify(title, msg, link, callback) {
    //no idea what to do with the link here
    var options = {
        title: title,
        message: msg,
        type: "basic", 
        iconUrl: "Icon.png" 
    };
    return chrome.notifications.create("", options, callback);
}

And to call the function: 并调用该函数:

notify("title", "message", "http://www.example.com", function(notification) { });

which would display 将显示

Title Message 标题讯息

and upon clicking, would open a new tab to http://www.example.com 并点击后,将打开一个新标签页到http://www.example.com

then later, I would call it again with notify("title", "message", "http://www.google.com", function(notification) { }); 然后,我将再次使用notify("title", "message", "http://www.google.com", function(notification) { });来调用它notify("title", "message", "http://www.google.com", function(notification) { });

and that one would open a new tab to http://www.google.com when clicked 并在点击后打开一个新标签到http://www.google.com

Thanks if you can help! 谢谢您的帮助!

There is no direct support for attaching an onclick handler to a notification. 没有直接支持将onclick处理程序附加到通知。 Instead, if you click a notification's body, it will generate a general chrome.notification.onClicked event, providing the ID of the notification. 相反,如果您单击通知的正文,它将生成一般的chrome.notification.onClicked事件,提供通知的ID。

You will have, therefore, to maintain your own matching of IDs to URLs. 因此,您必须维护自己的ID与URL的匹配。

var urls = {};

chrome.notifications.onClicked.addListener(notifyClick);
chrome.notifications.onClosed.addListener(notifyClose);

function notify(title, msg, link, callback) {
  var options = {
    title: title,
    message: msg,
    type: "basic", 
    iconUrl: "Icon.png",
    isClickable: true // New in Chrome 32, alters the appearance?
  };
  return chrome.notifications.create("", options, function(id) {
    // New ID will be generated
    urls[id] = link;
  });
}

function notifyClick(id) {
  // Look up the URL:
  chrome.tabs.create({url: urls[id]});
  // Optionally, close the notification:
  // chrome.notifications.clear(id, function(){});
}

function notifyClose(id, byUser) {
  // Clean up the matching
  delete urls[id];
}

Please note: there is a caveat regarding closing notification programmatically. 请注意:有一些关于以编程方式关闭通知的警告。 If the notification is closed from the message center, it will not immediately disappear . 如果通知是从消息中心关闭的, 则不会立即消失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM