github.com/oinume/lekcije@v0.0.0-20231017100347-5b4c5eb6ab24/backend/infrastructure/dmm_eikaiwa/testdata/49393_files/pigeon.js (about) 1 var NaviApiPigeon = {}; 2 3 /** 4 * send data to pigeon system 5 * 6 * @param {string} endpoint endpoint for pigeon 7 * @param {object} params POST payload 8 * @param {object|null} options option for XHR (pass `null` if unnecessary) 9 * 10 * contents of `options`: 11 * {number} timeout: time to timeout[ms] 12 * {function} callbackOnTimeout: callback for timeout 13 * {function} callbackOnUpload: callback for completion of uploading data 14 */ 15 NaviApiPigeon.request = function (endpoint, params, options) { 16 if (navigator.sendBeacon) { 17 navigator.sendBeacon( 18 endpoint, 19 new Blob([JSON.stringify(params)], { type: 'application/json' }) 20 ); 21 return; 22 } 23 // use XHR if sendBeacon is not supported 24 var request = new XMLHttpRequest(); // eslint-disable-line vars-on-top 25 request.open('POST', endpoint, true); 26 request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); 27 if (options) { 28 if (options.timeout) { 29 request.timeout = options.timeout; 30 } 31 if (options.callbackOnTimeout) { 32 request.ontimeout = options.callbackOnTimeout; 33 } 34 if (options.callbackOnUpload) { 35 request.upload.onloadend = options.callbackOnUpload; 36 } 37 } 38 request.send(JSON.stringify(params)); 39 }; 40 41 /** 42 * judge if the browser is iOS Safari (from BD department code) 43 * 44 * @method checkIOS 45 * @private 46 * @returns {boolean} 47 */ 48 NaviApiPigeon.checkIOS = function () { 49 var ua = navigator.userAgent.toLowerCase(); 50 var isiPhone = ua.indexOf('iphone') > -1; 51 var isiPod = ua.indexOf('ipod') > -1; 52 var isiPad = ua.indexOf('ipad') > -1; 53 var isSafari = ua.indexOf('safari') > -1; 54 var isChrome = ua.indexOf('chrome') > -1; 55 return (isiPhone || isiPod || isiPad) && isSafari && !isChrome; 56 }; 57 58 /** 59 * generate domain for Pigeon API endpoint 60 * 61 * @return {string} 62 */ 63 NaviApiPigeon.getDomain = function () { 64 var env = document.getElementById('naviapi-pigeon').getAttribute('data-env'); 65 66 // ios 67 if (/dmm\.co\.jp$/.test(window.location.hostname) // isR18 68 && NaviApiPigeon.checkIOS()) { 69 if (env === 'stg') { 70 return 'stg-pigeon.i3.dmm.co.jp'; 71 } 72 return 'pigeon.i3.dmm.co.jp'; 73 } 74 75 // others 76 if (env === 'stg') { 77 return 'stg-pigeon.i3.dmm.com'; 78 } 79 return 'pigeon.i3.dmm.com'; 80 }; 81 82 /** 83 * generate Pigeon API endpoint 84 * 85 * @param {string} various path parameter 86 * @param {string} action path parameter 87 * @param {string} option path parameter 88 * @return {string} 89 */ 90 NaviApiPigeon.getEndPoint = function (various, action, option) { 91 var version = document.getElementById('naviapi-pigeon').getAttribute('data-version'); 92 return 'https://' + NaviApiPigeon.getDomain() + '/' + version + '/' + various + '/' + action + '/' + option; 93 }; 94 95 /** 96 * get cookie value for given `key` 97 * 98 * @param {string} key key for Cookie 99 * @return {string} value of Cookie (empty string if not exist) 100 */ 101 NaviApiPigeon.getCookie = function (key) { 102 var name = key + '='; 103 var cookies; 104 var cookie; 105 var index; 106 try { 107 cookies = document.cookie.split(/;\s*/); 108 } catch (e) { 109 // sometimes accessing `document.cookie` raises `SecurityError` on Android Chrome 110 return ''; 111 } 112 for (index = 0; index < cookies.length; index += 1) { 113 cookie = cookies[index]; 114 if (cookie.indexOf(name) === 0) { 115 return cookie.substring(name.length, cookie.length); 116 } 117 } 118 return ''; 119 }; 120 121 /** 122 * generate common parameter for pigeon event 123 * 124 * @return {Object} 125 */ 126 NaviApiPigeon.createCommonParameters = function () { 127 return { 128 site_type: document.getElementById('naviapi-pigeon').getAttribute('data-site-type'), 129 view_type: document.getElementById('naviapi-pigeon').getAttribute('data-view-type'), 130 url: document.location.href, 131 referer: document.referrer, 132 segment: NaviApiPigeon.getCookie('i3_ab'), 133 open_id: NaviApiPigeon.getCookie('i3_opnd'), 134 cdp_id: NaviApiPigeon.getCookie('cdp_id') 135 }; 136 }; 137 138 /** 139 * callback for click event 140 */ 141 NaviApiPigeon.click = function (event) { 142 var currentTarget = event.currentTarget; 143 var options = null; 144 var params = NaviApiPigeon.createCommonParameters(); 145 146 var dataExtraMap = currentTarget.getAttribute('data-extra-map'); 147 var map = null; 148 if (dataExtraMap) { 149 // create parameter from `data-extra-map` attribute (in JSON format) 150 map = JSON.parse(dataExtraMap); 151 Object.keys(map).forEach(function (key) { 152 params[key] = map[key]; 153 }); 154 } 155 156 try { 157 NaviApiPigeon.request( 158 NaviApiPigeon.getEndPoint( 159 currentTarget.getAttribute('data-various'), 160 'click', 161 currentTarget.getAttribute('data-option') 162 ), 163 params, 164 options 165 ); 166 } catch (e) { 167 // continue regardless of error 168 } 169 }; 170 171 /** 172 * callback for opening navi 173 */ 174 NaviApiPigeon.open = function (event) { 175 var currentTarget = event.currentTarget; 176 var options = null; 177 var params = NaviApiPigeon.createCommonParameters(); 178 179 try { 180 NaviApiPigeon.request( 181 NaviApiPigeon.getEndPoint( 182 currentTarget.getAttribute('data-various'), 183 'open', 184 currentTarget.getAttribute('data-option') 185 ), 186 params, 187 options 188 ); 189 } catch (e) { 190 // continue regardless of error 191 } 192 }; 193 194 /** 195 * add event listeners 196 */ 197 NaviApiPigeon.clickElements = document.querySelectorAll('[data-navi-pigeon-click]'); 198 Array.prototype.forEach.call(NaviApiPigeon.clickElements, function (element) { 199 element.addEventListener('click', NaviApiPigeon.click); 200 }); 201 202 NaviApiPigeon.openElements = document.querySelectorAll('[data-navi-pigeon-open]'); 203 Array.prototype.forEach.call(NaviApiPigeon.openElements, function (element) { 204 element.addEventListener('click', NaviApiPigeon.open); 205 });