PageSourceSearch

https://img.nttexc.co.jp/v=1782366640/files/user/js/select-option_e05.js

js nttexc.co.jp collected 2026-10-04 02:56:50 UTC 4,567 bytes, 144 lines download raw bytes

1(function () {
2  /**
3   * ▼実務で変更しやすい設定
4   */
5
6  // 【要変更】遷移元ページ側と同じキーにする
7  const STORAGE_KEY = 'prev_page_title';
8
9  const TITLE_TO_OPTION = [
10    // 【要変更】
11    // keyword: 遷移元ページの<title>に含まれていたら反応させたい文字列
12    // optionText: フォームのプルダウンで選ばせたい表示文字列
13 { keyword: '行政、自治体向けDX入門(概要編)', optionText: '行政、自治体向けDX入門(概要編)' },
14 { keyword: 'DX入門 自治体編', optionText: 'DX入門 自治体編' }
15  ];
16
17  // 【要変更】フォーム項目のラベル名
18  const FIELD_LABEL_TEXT = 'お問い合わせサービス';
19
20  function normalize(str) {
21    return (str || '').replace(/\s+/g, '').trim();
22  }
23
24  function getOptionTextFromStoredTitle() {
25    const prevTitle = localStorage.getItem(STORAGE_KEY) || '';
26    const hit = TITLE_TO_OPTION.find(item =>
27      prevTitle.includes(item.keyword) || prevTitle.includes(item.optionText)
28    );
29    return hit ? hit.optionText : null;
30  }
31
32  function findSelectByLabelText(labelText) {
33    const labels = Array.from(document.querySelectorAll('label'));
34
35    for (const label of labels) {
36      if (!normalize(label.textContent).includes(normalize(labelText))) continue;
37
38      if (label.htmlFor) {
39        const el = document.getElementById(label.htmlFor);
40        if (el && el.tagName === 'SELECT') return el;
41      }
42
43      // 【環境により調整候補】
44      const group = label.closest('.satori__input_group, .form-group, .field, div, li');
45      const selectInGroup = group ? group.querySelector('select') : null;
46      if (selectInGroup) return selectInGroup;
47
48      const selectInParent = label.parentElement
49        ? label.parentElement.querySelector('select')
50        : null;
51      if (selectInParent) return selectInParent;
52    }
53
54    return null;
55  }
56
57  function setSelectByVisibleText(select, optionText) {
58    const options = Array.from(select.options);
59    const target = options.find(opt => {
60      const text = normalize(opt.textContent);
61
62      // 現在は「完全一致 or 部分一致」
63      return text === normalize(optionText) || text.includes(normalize(optionText));
64    });
65
66    if (!target) return false;
67
68    select.value = target.value;
69    target.selected = true;
70
71    select.dispatchEvent(new Event('input', { bubbles: true }));
72    select.dispatchEvent(new Event('change', { bubbles: true }));
73
74    return true;
75  }
76
77  function lockSelect(select, lockedValue) {
78    if (select.dataset.autoLocked === '1') return;
79    select.dataset.autoLocked = '1';
80
81    const restoreValue = () => {
82      if (select.value !== lockedValue) {
83        select.value = lockedValue;
84        select.dispatchEvent(new Event('input', { bubbles: true }));
85        select.dispatchEvent(new Event('change', { bubbles: true }));
86      }
87    };
88
89    select.style.pointerEvents = 'none';
90    select.style.backgroundColor = '#f5f5f5';
91    select.style.cursor = 'not-allowed';
92    select.setAttribute('tabindex', '-1');
93    select.setAttribute('aria-readonly', 'true');
94
95    select.addEventListener('change', restoreValue);
96    select.addEventListener('input', restoreValue);
97    select.addEventListener('keydown', function (e) { e.preventDefault(); });
98    select.addEventListener('mousedown', function (e) { e.preventDefault(); });
99    select.addEventListener('focus', function () { select.blur(); });
100
101    restoreValue();
102  }
103
104  function applyAutoSelect() {
105    const targetOptionText = getOptionTextFromStoredTitle();
106    if (!targetOptionText) return false;
107
108    const select = findSelectByLabelText(FIELD_LABEL_TEXT);
109    if (!select) return false;
110
111    const setOk = setSelectByVisibleText(select, targetOptionText);
112    if (!setOk) return false;
113
114    lockSelect(select, select.value);
115    return true;
116  }
117
118  let applied = false;
119
120  function tryApply() {
121    if (applied) return;
122    applied = applyAutoSelect();
123  }
124
125  if (document.readyState === 'loading') {
126    document.addEventListener('DOMContentLoaded', tryApply);
127  } else {
128    tryApply();
129  }
130
131  const observer = new MutationObserver(function () {
132    tryApply();
133    if (applied) observer.disconnect();
134  });
135
136  observer.observe(document.body, {
137    childList: true,
138    subtree: true
139  });
140
141  setTimeout(tryApply, 500);
142  setTimeout(tryApply, 1500);
143  setTimeout(tryApply, 3000);
144})();

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.