PageSourceSearch

https://www.sportshunter.gr/wp-content/plugins/sh-web-push/assets/js/shwp-client.js?ver=1761529752

js sportshunter.gr collected 2026-10-09 17:29:22 UTC 5,588 bytes, 132 lines download raw bytes

1(function(){
2  // Temporary safety: require both ServiceWorker and Push APIs
3  if (!('serviceWorker' in navigator) || !('PushManager' in window) || (typeof Notification === 'undefined')) { return; }
4
5  const cfg = (typeof SHWP_SETTINGS !== 'undefined') ? SHWP_SETTINGS : {};
6
7  // TEMPORARY DISABLE SWITCH
8  // When SHWP_SETTINGS.enabled === false, we fully stop any SW register or permission prompt.
9  // To re-enable later: set SHWP_SETTINGS.enabled = true from PHP (wp_localize_script)
10  // and ensure production sw.js + valid VAPID public key are configured.
11  // Treat any non-true value as disabled (handles '', '0', 0, false, undefined)
12  if (cfg && cfg.enabled !== true) {
13    try {
14      window.SHWP = {
15        init: async () => false,
16        enable: async () => ({ ok:false, reason:'disabled' }),
17        disable: async () => ({ ok:true }),
18        test: async () => ({ ok:false, reason:'disabled' })
19      };
20    } catch(e){}
21    return;
22  }
23  const state = { registration: null, subscription: null, ready: false };
24
25  function base64ToUint8Array(base64String) {
26    const padding = '='.repeat((4 - base64String.length % 4) % 4);
27    const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
28    const rawData = atob(base64);
29    const outputArray = new Uint8Array(rawData.length);
30    for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); }
31    return outputArray;
32  }
33
34  async function registerSW(){
35    try {
36      const reg = await navigator.serviceWorker.register(cfg.swUrl, { scope: '/' });
37      state.registration = reg;
38      state.ready = true;
39      return reg;
40    } catch (e) { console.warn('[SHWP] SW register error', e); return null; }
41  }
42
43  async function getOrCreateSubscription(){
44    if (!state.registration) return null;
45    const existing = await state.registration.pushManager.getSubscription();
46    if (existing) { state.subscription = existing; return existing; }
47    const vapid = (cfg.vapidPublic || '').trim();
48    if (!vapid) { console.warn('[SHWP] Missing VAPID public key'); return null; }
49    try {
50      const sub = await state.registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: base64ToUint8Array(vapid) });
51      state.subscription = sub;
52      return sub;
53    } catch (e) { console.warn('[SHWP] subscribe error', e); return null; }
54  }
55
56  async function sendToServer(sub){
57    try {
58      const res = await fetch(cfg.restUrl + 'subscribe', {
59        method: 'POST',
60        headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': cfg.nonce },
61        body: JSON.stringify(sub)
62      });
63      return await res.json();
64    } catch (e) { console.warn('[SHWP] sendToServer error', e); return null; }
65  }
66
67  async function removeFromServer(sub){
68    try {
69      const res = await fetch(cfg.restUrl + 'unsubscribe', {
70        method: 'POST',
71        headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': cfg.nonce },
72        body: JSON.stringify({ endpoint: sub && sub.endpoint })
73      });
74      return await res.json();
75    } catch (e) { console.warn('[SHWP] removeFromServer error', e); return null; }
76  }
77
78  // Public API
79  const SHWP = {
80    async init(){ if (!state.registration) await registerSW(); return state.ready; },
81    async enable(){
82      if (Notification && Notification.permission === 'default') {
83        // must be called from a user gesture ideally
84        try { await Notification.requestPermission(); } catch(e){}
85      }
86      if (Notification.permission !== 'granted') {
87        return { ok:false, reason: 'permission_' + Notification.permission };
88      }
89      await SHWP.init();
90      const sub = await getOrCreateSubscription();
91      if (!sub) return { ok:false, reason:'subscribe_failed' };
92      const saved = await sendToServer(sub.toJSON());
93      return { ok:true, saved };
94    },
95    async disable(){
96      await SHWP.init();
97      const existing = state.registration ? await state.registration.pushManager.getSubscription() : null;
98      if (existing) { await removeFromServer(existing); try { await existing.unsubscribe(); } catch(e){} }
99      return { ok:true };
100    },
101    async test(){
102      try {
103        const res = await fetch(cfg.restUrl + 'test', { method:'POST', headers:{ 'X-WP-Nonce': cfg.nonce } });
104        return await res.json();
105      } catch(e){ return { ok:false, error: e.message }; }
106    }
107  };
108
109  // Expose
110  window.SHWP = SHWP;
111
112  // Soft auto-init SW (χωρίς prompt). Το enable() θα ζητήσει άδεια όταν το καλέσουμε εμείς.
113  registerSW().then(() => {
114    // Default ON for tests: Προσπάθησε να ενεργοποιήσεις αυτόματα
115    // Μικρή καθυστέρηση ώστε να είναι έτοιμο το registration
116    setTimeout(async () => {
117      try {
118        if (typeof Notification !== 'undefined') {
119          // Αν ήδη granted, διασφάλισε ότι έχουμε subscription στον server
120          if (Notification.permission === 'granted') {
121            const sub = await state.registration.pushManager.getSubscription();
122            if (!sub) { await SHWP.enable(); }
123            else { await fetch(cfg.restUrl + 'subscribe', { method:'POST', headers:{ 'Content-Type':'application/json','X-WP-Nonce':cfg.nonce }, body: JSON.stringify(sub.toJSON()) }); }
124          } else if (Notification.permission === 'default') {
125            // Ζήτα άδεια αυτόματα (test mode)
126            await SHWP.enable();
127          }
128        }
129      } catch(e){ /* no-op */ }
130    }, 500);
131  });
132})();

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.