File size: 4,276 Bytes
e7e019f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// dev-tools.js
document.addEventListener('DOMContentLoaded', () => {
  // 開発者ツール起動ボタン
  const openButton = document.createElement('button');
  openButton.textContent = '開発者ツールを開く';
  openButton.style.position = 'fixed';
  openButton.style.top = '10px';
  openButton.style.right = '10px';
  document.body.appendChild(openButton);

  // 開発者ツールコンテナ
  const devToolsContainer = document.createElement('div');
  devToolsContainer.style.cssText = `
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 400px;
    background: #242424;
    color: #fff;
    font-family: monospace;
    display: none;
    border-top: 2px solid #888;
    flex-direction: column;
  `;

  // タブバー
  const tabBar = document.createElement('div');
  tabBar.style.display = 'flex';
  tabBar.style.padding = '5px';
  tabBar.style.gap = '10px';
  tabBar.style.background = '#1a1a1a';

  // タブボタン
  const tabs = ['Console', 'Elements', 'Storage'];
  const contentAreas = {};

  tabs.forEach(tabName => {
    const tabButton = document.createElement('button');
    tabButton.textContent = tabName;
    tabButton.addEventListener('click', () => {
      Object.values(contentAreas).forEach(area => area.style.display = 'none');
      contentAreas[tabName].style.display = 'block';
      tabButton.classList.add('active');
    });
    tabBar.appendChild(tabButton);

    const content = document.createElement('div');
    content.style.display = 'none';
    content.style.flexGrow = '1';
    content.style.overflow = 'auto';
    contentAreas[tabName] = content;
    devToolsContainer.appendChild(content);
  });

  // Consoleタブ
  const consoleContent = contentAreas['Console'];
  consoleContent.innerHTML = `
    <input type="text" id="console-input" placeholder="JavaScriptを入力" 
      style="width: 100%; padding: 5px; background: #333; color: #fff; border: none;">
    <pre id="console-output" style="padding: 10px; margin: 0;"></pre>
  `;

  // Consoleログのキャプチャ
  const originalConsoleLog = console.log;
  const consoleOutput = consoleContent.querySelector('#console-output');
  
  console.log = (...args) => {
    consoleOutput.textContent += args.join(' ') + '\n';
    originalConsoleLog(...args);
  };

  // JavaScript実行
  consoleContent.querySelector('#console-input').addEventListener('keypress', e => {
    if (e.key === 'Enter') {
      try {
        eval(e.target.value);
      } catch (err) {
        console.log('Error:', err.message);
      }
      e.target.value = '';
    }
  });

  // Elementsタブ
  const elementsContent = contentAreas['Elements'];
  elementsContent.innerHTML = '<div class="dom-tree"></div>';
  
  function buildDOMTree(element, parent) {
    const div = document.createElement('div');
    div.textContent = '<' + element.tagName.toLowerCase() + '>';
    div.style.paddingLeft = '20px';
    
    Array.from(element.children).forEach(child => {
      buildDOMTree(child, div);
    });
    
    parent.appendChild(div);
  }
  
  buildDOMTree(document.documentElement, elementsContent.querySelector('.dom-tree'));

  // Storageタブ
  const storageContent = contentAreas['Storage'];
  storageContent.innerHTML = `
    <div style="padding: 10px;">
      <h3>Local Storage</h3>
      <pre id="local-storage"></pre>
      <button id="clear-storage">Clear Local Storage</button>
      
      <h3>Cookies</h3>
      <pre id="cookies"></pre>
    </div>
  `;

  // ストレージ表示更新
  function updateStorageDisplay() {
    storageContent.querySelector('#local-storage').textContent = 
      JSON.stringify(localStorage, null, 2);
    storageContent.querySelector('#cookies').textContent = 
      document.cookie;
  }

  storageContent.querySelector('#clear-storage').addEventListener('click', () => {
    localStorage.clear();
    updateStorageDisplay();
  });

  updateStorageDisplay();

  // 全体の組み立て
  devToolsContainer.insertBefore(tabBar, devToolsContainer.firstChild);
  document.body.appendChild(devToolsContainer);

  // 開閉制御
  openButton.addEventListener('click', () => {
    devToolsContainer.style.display = 'flex';
    contentAreas['Console'].style.display = 'block';
    tabs[0].classList.add('active');
  });
});