1/*! 2 * éµä¾¿çªå· â 使 èªåå ¥åï¼yubinbango 代æ¿ï¼ 3 * 4 * zipcloud API (https://zipcloud.ibsnet.co.jp/api/search) ãå©ç¨ã 5 * yubinbango ã¨åã microformat æ§é ï¼form.h-adr / .p-country-name / 6 * .p-postal-code / .p-region / .p-locality / .p-street-address / 7 * .p-extended-addressï¼ã対象ã¨ããã 8 * 9 * zipcloud 㯠JSON ãè¿ããããä¸ä¸ API å´ãæ¹ããããã¦ã 10 * æååã¨ã㦠input.value ã«ä»£å ¥ãããã ãã§ãä»»æã³ã¼ãå®è¡ã¯èµ·ããªãã 11 */ 12(function () { 13 'use strict'; 14 15 var ENDPOINT = 'https://zipcloud.ibsnet.co.jp/api/search'; 16 var COUNTRIES = ['Japan', 'JP', 'JPN', 'JAPAN']; 17 var TARGET_CLASSES = ['p-region', 'p-locality', 'p-street-address', 'p-extended-address']; 18 19 function normalizeZip(value) { 20 var half = (value || '').replace(/[ï¼-ï¼]/g, function (c) { 21 return String.fromCharCode(c.charCodeAt(0) - 0xFEE0); 22 }); 23 return half.replace(/\D/g, ''); 24 } 25 26 function isJapanForm(form) { 27 var countryEl = form.querySelector('.p-country-name'); 28 if (!countryEl) return false; 29 var value = countryEl.value || countryEl.textContent || countryEl.innerHTML || ''; 30 value = value.trim(); 31 return COUNTRIES.indexOf(value) !== -1; 32 } 33 34 function joinZipFromInputs(form) { 35 var inputs = form.querySelectorAll('.p-postal-code'); 36 var combined = ''; 37 Array.prototype.forEach.call(inputs, function (el) { 38 combined += el.value || ''; 39 }); 40 return normalizeZip(combined); 41 } 42 43 function clearTargets(form) { 44 TARGET_CLASSES.forEach(function (cls) { 45 var nodes = form.querySelectorAll('.' + cls); 46 Array.prototype.forEach.call(nodes, function (node) { 47 node.value = ''; 48 }); 49 }); 50 } 51 52 function setAddress(form, result) { 53 clearTargets(form); 54 55 var parts = { 56 'p-region': result.address1 || '', 57 'p-locality': result.address2 || '', 58 'p-street-address': result.address3 || '', 59 'p-extended-address': '' 60 }; 61 62 TARGET_CLASSES.forEach(function (cls) { 63 var nodes = form.querySelectorAll('.' + cls); 64 Array.prototype.forEach.call(nodes, function (node) { 65 node.value += parts[cls] || ''; 66 }); 67 }); 68 } 69 70 function lookup(form) { 71 var zip = joinZipFromInputs(form); 72 if (zip.length !== 7) return; 73 74 var url = ENDPOINT + '?zipcode=' + encodeURIComponent(zip); 75 fetch(url, { method: 'GET', credentials: 'omit', cache: 'no-cache' }) 76 .then(function (res) { 77 if (!res.ok) throw new Error('network'); 78 return res.json(); 79 }) 80 .then(function (data) { 81 if (!data || data.status !== 200 || !data.results || !data.results.length) { 82 return; 83 } 84 setAddress(form, data.results[0]); 85 }) 86 .catch(function () { 87 // 失ææã¯é»ã£ã¦ã¹ãããï¼ã¦ã¼ã¶ã¼ã¯æå ¥åã§ããï¼ 88 }); 89 } 90 91 function init() { 92 var forms = document.querySelectorAll('form.h-adr'); 93 Array.prototype.forEach.call(forms, function (form) { 94 if (!isJapanForm(form)) return; 95 96 var zipInputs = form.querySelectorAll('.p-postal-code'); 97 if (!zipInputs.length) return; 98 99 var lastZip = zipInputs[zipInputs.length - 1]; 100 lastZip.addEventListener('keyup', function () { lookup(form); }, false); 101 lastZip.addEventListener('blur', function () { lookup(form); }, false); 102 }); 103 } 104 105 if (document.readyState === 'loading') { 106 document.addEventListener('DOMContentLoaded', init, false); 107 } else { 108 init(); 109 } 110})();
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.