async function updateScreen( targetid , formname ) { let d = document.forms[ formname ]; if( d ){ try{ let a ={}; for( let i = 0; i < d.length ; i += 1 ) a[ d[ i ].name ] = d[ i ].value; let r = await fetch( "/api/" , { method: "POST" , body: JSON.stringify( a ) , header: { "Content-Type": "application/json" } }); let t = await r.text(); let id = document.getElementById( targetid ); if( id ) id.innerHTML = t; } catch ( err ){ alert( "Error:" + err ); } } else alert( "フォーム名が見つかりません[" + formname + "]" );} function requestPermission() { Notification.requestPermission().then((permission) => { switch (permission) { case 'granted': // 許可 activatePushServer(); alert( "通知の許可ありがとうございます。" ); break; case 'denied': // ブロック case 'default': // ブロックも許可もなし(xで閉じた) default: alert( "通知の許可をお願いします" ); break; } }); } function checkNotificationPermission() { if( "Notification" in window ) return Notification.permission; return false; } function activatePushServer(){ if ('serviceWorker' in navigator) { navigator.serviceWorker.register('service-worker.js', { scope: '/', }) } else { alert('serviceWorker に対応していません。') } navigator.serviceWorker.ready.then( ( registration ) => { alert( "ServiceManagerが有効になりました" ); registration.pushManager.getSubscription().then((subscription) => { let server_public_key = urlBase64ToUint8Array( "BK4n23ANz4lxduzs0ACsgKVEQX-xuJrkfqMPjh1941CRrzLMYIdY_-g78eXPkko8ZOj0GrG1mWGqd_EVkPeQH_c" ); registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: server_public_key }).then( (subscription ) => { const endpoint = subscription.endpoint; const rawKey = subscription.getKey('p256dh'); const push_public_key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ''; const rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : ''; const push_auth_token = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : ''; let a = { COMMAND: "hn714WVeGCZ7HExFIDQl47nWA_SElk9OyTN_KHfomU8,", FUNCTION: "T1xV20zUs4lZ4PZbQdR-IygW2nxFTbLoMLyowapET9I,", ENDPOINT: endpoint, PUBLICKEY: push_public_key, TOKEN: push_auth_token } fetch( "/api/" , { method: "POST", body: JSON.stringify( a ) , header: { "Content-Type": "application/json" } }); }).catch(e => console.log('pushサーバーへの登録に失敗しました。')); }) }) } /** サーバー公開鍵をpushサーバーが求める方式にフォーマットする */ function urlBase64ToUint8Array(server_public_key) { const padding = '='.repeat((4 - server_public_key.length % 4) % 4); const base64 = (server_public_key + padding).replace(/\-/g, '+').replace(/_/g, '/'); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } return outputArray; }