gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:03:02 24 4
gpt4 key购买 nike

我正在编写一个非常简单的 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
}

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

关于javascript - Chrome 扩展程序通知 - 如何向通知发送值并重新使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32191547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com