PageSourceSearch

https://img.nttexc.co.jp/v=1782463847/files/user/js/select-option_e16.js

js nttexc.co.jp collected 2026-10-04 02:57:05 UTC 5,170 bytes, 149 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: '戦略的データ思考', optionText: '戦略的データ思考' },
14 { keyword: 'デザイン思考の理論と実践', optionText: 'デザイン思考の理論と実践' },
15 { keyword: 'データサイエンスの基本とビジネス動向', optionText: 'データサイエンスの基本とビジネス動向' },
16 { keyword: 'データサイエンスプロジェクトの勘所', optionText: 'データサイエンスプロジェクトの勘所' },
17 { keyword: '統計学Ⅰ:データ分析の基礎', optionText: '統計学Ⅰ:データ分析の基礎' },
18 { keyword: '統計学Ⅱ:推測統計の方法', optionText: '統計学Ⅱ:推測統計の方法' },
19 { keyword: '統計学Ⅲ:多変量データ解析法', optionText: '統計学Ⅲ:多変量データ解析法' }
20  ];
21
22  // 【要変更】フォーム項目のラベル名
23  const FIELD_LABEL_TEXT = 'お問い合わせサービス';
24
25  function normalize(str) {
26    return (str || '').replace(/\s+/g, '').trim();
27  }
28
29  function getOptionTextFromStoredTitle() {
30    const prevTitle = localStorage.getItem(STORAGE_KEY) || '';
31    const hit = TITLE_TO_OPTION.find(item =>
32      prevTitle.includes(item.keyword) || prevTitle.includes(item.optionText)
33    );
34    return hit ? hit.optionText : null;
35  }
36
37  function findSelectByLabelText(labelText) {
38    const labels = Array.from(document.querySelectorAll('label'));
39
40    for (const label of labels) {
41      if (!normalize(label.textContent).includes(normalize(labelText))) continue;
42
43      if (label.htmlFor) {
44        const el = document.getElementById(label.htmlFor);
45        if (el && el.tagName === 'SELECT') return el;
46      }
47
48      // 【環境により調整候補】
49      const group = label.closest('.satori__input_group, .form-group, .field, div, li');
50      const selectInGroup = group ? group.querySelector('select') : null;
51      if (selectInGroup) return selectInGroup;
52
53      const selectInParent = label.parentElement
54        ? label.parentElement.querySelector('select')
55        : null;
56      if (selectInParent) return selectInParent;
57    }
58
59    return null;
60  }
61
62  function setSelectByVisibleText(select, optionText) {
63    const options = Array.from(select.options);
64    const target = options.find(opt => {
65      const text = normalize(opt.textContent);
66
67      // 現在は「完全一致 or 部分一致」
68      return text === normalize(optionText) || text.includes(normalize(optionText));
69    });
70
71    if (!target) return false;
72
73    select.value = target.value;
74    target.selected = true;
75
76    select.dispatchEvent(new Event('input', { bubbles: true }));
77    select.dispatchEvent(new Event('change', { bubbles: true }));
78
79    return true;
80  }
81
82  function lockSelect(select, lockedValue) {
83    if (select.dataset.autoLocked === '1') return;
84    select.dataset.autoLocked = '1';
85
86    const restoreValue = () => {
87      if (select.value !== lockedValue) {
88        select.value = lockedValue;
89        select.dispatchEvent(new Event('input', { bubbles: true }));
90        select.dispatchEvent(new Event('change', { bubbles: true }));
91      }
92    };
93
94    select.style.pointerEvents = 'none';
95    select.style.backgroundColor = '#f5f5f5';
96    select.style.cursor = 'not-allowed';
97    select.setAttribute('tabindex', '-1');
98    select.setAttribute('aria-readonly', 'true');
99
100    select.addEventListener('change', restoreValue);
101    select.addEventListener('input', restoreValue);
102    select.addEventListener('keydown', function (e) { e.preventDefault(); });
103    select.addEventListener('mousedown', function (e) { e.preventDefault(); });
104    select.addEventListener('focus', function () { select.blur(); });
105
106    restoreValue();
107  }
108
109  function applyAutoSelect() {
110    const targetOptionText = getOptionTextFromStoredTitle();
111    if (!targetOptionText) return false;
112
113    const select = findSelectByLabelText(FIELD_LABEL_TEXT);
114    if (!select) return false;
115
116    const setOk = setSelectByVisibleText(select, targetOptionText);
117    if (!setOk) return false;
118
119    lockSelect(select, select.value);
120    return true;
121  }
122
123  let applied = false;
124
125  function tryApply() {
126    if (applied) return;
127    applied = applyAutoSelect();
128  }
129
130  if (document.readyState === 'loading') {
131    document.addEventListener('DOMContentLoaded', tryApply);
132  } else {
133    tryApply();
134  }
135
136  const observer = new MutationObserver(function () {
137    tryApply();
138    if (applied) observer.disconnect();
139  });
140
141  observer.observe(document.body, {
142    childList: true,
143    subtree: true
144  });
145
146  setTimeout(tryApply, 500);
147  setTimeout(tryApply, 1500);
148  setTimeout(tryApply, 3000);
149})();

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.