PageSourceSearch

https://folk24.pl/templates/independent/js/PushRegistration.js

js folk24.pl collected 2026-10-02 12:16:56 UTC 3,067 bytes, 113 lines download raw bytes

1(function($) {
2	$.fn.pushRegistration = function(options) {
3		var defaults = {
4				keyId: 0,
5				publicKey: [],
6				addUrl: '',
7				checkUrl: '',
8				callback: function(state) {}
9		};
10		
11		var options = $.extend(defaults, options);
12		var swRegistration = null;
13		var subscription = null;
14		var subscribed = false;
15		var messageChannel = new MessageChannel();
16		messageChannel.port1.onmessage = function(event) {
17			if($('response/errors', $(event.data)).length)
18			{
19				updateButton(buttons, 'WYSTĄPIŁ BŁĄD', false);
20				return;
21			}
22			
23			updateButton(buttons, 'DODALIŚMY CIĘ DO LISTY SUBSKRYBENTÓW', false);
24			options.callback('subscribed');
25		};
26		var updateButton = function(button, text, enabled)
27		{
28			button.html(text);
29			button.attr('disabled', !enabled);
30			if (enabled)
31				button.removeClass('disabled');
32			else
33				button.addClass('disabled');
34		}
35		var buttons = $(this);
36
37		if ('serviceWorker' in navigator && 'PushManager' in window) {
38			navigator.serviceWorker.register('/templates/independent/js/PushWorker.js')
39			.then(function(swReg) {
40				swRegistration = swReg;
41				
42				swRegistration.pushManager.getSubscription()
43				.then(function(s) {
44					subscription = s;
45					if (!subscription)
46					{
47						options.callback('notSubscribed');
48						return;
49					}
50					
51					var subscriptionJSON = subscription.toJSON();
52					$.phpartAjax(options.checkUrl, {endpoint: subscriptionJSON.endpoint,
53						auth: subscriptionJSON.keys.auth})
54					.done(function(response) {
55						if(response.errors)
56						{
57							updateButton(button, 'WYSTĄPIŁ BŁĄD', false);
58							return;
59						}
60						
61						if($('exists', response.data).text() === 'true')
62						{
63							subscribed = true;
64							updateButton(buttons, 'JUŻ JESTEŚ NA LIŚCIE SUBSKRYBENTÓW', false);
65							options.callback('subscribed');
66						}
67						else
68							options.callback('notSubscribed');
69					});
70				});
71			})
72			.catch(function(error) {
73				updateButton(buttons, 'WYSTĄPIŁ BŁĄD', false);
74			});
75		} else {
76			updateButton(buttons, 'TWOJA PRZEGLĄDARKA NIE OBSŁUGUJE POWIADOMIEŃ', false);
77			options.callback('notSupported');
78		}
79		
80		return this.click(function() {
81			if (!swRegistration || subscribed)
82				return false;
83			
84			updateButton(buttons, 'ZATWIERDŹ ZGODĘ NA OTRZYMYWANIE POWIADOMIEŃ', false);
85
86			swRegistration.pushManager.subscribe({
87			    userVisibleOnly: true,
88			    applicationServerKey: new Uint8Array(options.publicKey)
89			})
90			.then(function(s) {
91				subscription = s;
92				if (!subscription)
93					return;
94				
95				updateButton(buttons, 'TRWA PRZETWARZANIE', false);
96				
97				var subscriptionJSON = subscription.toJSON();
98
99				swRegistration.active.postMessage({addUrl: options.addUrl,
100					keyId: options.keyId,
101					publicKey: options.publicKey,
102					endpoint: subscriptionJSON.endpoint,
103					auth: subscriptionJSON.keys.auth,
104					p256dh: subscriptionJSON.keys.p256dh}, [messageChannel.port2]);
105			})
106			.catch(function(err) {
107				updateButton(buttons, 'WYSTĄPIŁ BŁĄD', false);
108			});			
109			
110			return false;
111		});
112	};
113})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.