PageSourceSearch

https://www.kohsin-home.co.jp/wp-content/themes/kohshinhome/assets/js/zip-to-address.js?ver=1777020926

js kohsin-home.co.jp collected 2026-10-08 05:27:09 UTC 3,549 bytes, 110 lines download raw bytes

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(/[0-9]/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.