我必须重新加载chrome扩展以显示通知

I have to reload chrome extension to show notification

本文关键字:chrome 扩展 通知 显示 加载 新加载      更新时间:2023-09-26

我正在开发我的第一个chrome扩展。我已经实现了Signal R2来显示chrome通知。问题是,如果扩展是idol一段时间,它就会停止显示通知,同时如果我调试background.js(通过打开扩展的后台页面),它就会开始显示通知。

这是我的信号R实现

 [HttpGet]
    public void SendNotificationYealink(string mac, string ip, string model, string active_url, string active_user, string active_host, string local, string remote, string display_local, string display_remote, string call_id, string action)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        context.Clients.All.addNotificationToExtension("Arslan",  "Active_User =" + active_user + 
             ", Local =" + local + ", Remote =" + remote + ", Display_Local =" + display_local + ", Display_Remote =" + display_remote + ", Call Id=" + call_id);
    }

和我的背景.js

$(function () {
var connection = $.hubConnection();
connection.url = 'http://localhost:8089/signalr';
var notificationHubProxy = connection.createHubProxy('notificationHub');
notificationHubProxy.on('addNotificationToExtension', function (callFrom, phoneNumber) {
    var firstRun =
        {
            type: "basic",
            title: callFrom,
            message: phoneNumber,
            iconUrl: "icon.png"
        }
    chrome.notifications.create(firstRun);
});
connection.start().done(function () {
    console.log('Connection established.');
});
})

显然,一旦执行了一次闭包,闭包中的变量以及notificationHubProxy及其事件侦听器就会被销毁。

删除代码周围的($(function () { ... })()包装,它在扩展的后台页面中没有任何作用,因为它在自己的隔离环境中运行,所以不必向任何人隐藏变量。

在不太可能的情况下,这是一种确保后台页面的DOM初始化的方法,并且您实际上在其中有DOM元素,只需在关闭</body>标记之前将<script src="background.js"></script>移动到background.html的末尾。