PageSourceSearch

https://img.nttexc.co.jp/v=1782711140/files/user/js/select-option_e22.js

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

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.