PageSourceSearch

https://img.nttexc.co.jp/v=1782711126/files/user/js/select-option_e21.js

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

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.