soiz1 commited on
Commit
96d48d5
·
verified ·
1 Parent(s): cefa10d

Update dev-tools.js

Browse files
Files changed (1) hide show
  1. dev-tools.js +45 -9
dev-tools.js CHANGED
@@ -566,9 +566,9 @@
566
  LCP: null,
567
  TTFB: null
568
  };
569
-
570
  // DOM変更を監視するMutationObserver
571
- const observer = new MutationObserver((mutations) => {
572
  mutations.forEach((mutation) => {
573
  if (mutation.type === 'childList') {
574
  mutation.addedNodes.forEach((node) => {
@@ -580,7 +580,40 @@
580
  });
581
  refreshElementsPanel();
582
  });
583
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
  // ノードをハイライト表示
585
  function highlightNode(node) {
586
  const elementId = node.id || Math.random().toString(36).substr(2, 9);
@@ -598,7 +631,7 @@
598
  }
599
 
600
  // 開発者ツールのメイン関数
601
- function createDevTools() {
602
  const container = document.createElement('div');
603
  container.className = 'devtools-container';
604
  container.id = 'devtools-container';
@@ -758,16 +791,19 @@
758
  container.appendChild(ttfbCard);
759
 
760
  // Web Vitalsの計測を開始
761
- webVitalsScript.onload = function() {
762
- if (window.webVitals) {
 
763
  window.webVitals.getCLS(updateCLS);
764
  window.webVitals.getFCP(updateFCP);
765
  window.webVitals.getFID(updateFID);
766
  window.webVitals.getLCP(updateLCP);
767
  window.webVitals.getTTFB(updateTTFB);
768
- }
769
- };
770
-
 
 
771
  function updateCLS(metric) {
772
  vitalsData.CLS = metric.value;
773
  updateVitalDisplay('cls', metric.value, 0.1, 0.25);
 
566
  LCP: null,
567
  TTFB: null
568
  };
569
+ let observer;
570
  // DOM変更を監視するMutationObserver
571
+ let observer = new MutationObserver((mutations) => {
572
  mutations.forEach((mutation) => {
573
  if (mutation.type === 'childList') {
574
  mutation.addedNodes.forEach((node) => {
 
580
  });
581
  refreshElementsPanel();
582
  });
583
+ const loadWebVitals = () => {
584
+ return new Promise((resolve) => {
585
+ if (window.webVitals) {
586
+ resolve();
587
+ return;
588
+ }
589
+
590
+ const webVitalsScript = document.createElement('script');
591
+ webVitalsScript.src = 'https://unpkg.com/[email protected]/dist/web-vitals.iife.js';
592
+ webVitalsScript.onload = resolve;
593
+ document.head.appendChild(webVitalsScript);
594
+ });
595
+ };
596
+
597
+ // DOMContentLoaded内に追加
598
+ observer = new MutationObserver((mutations) => {
599
+ mutations.forEach((mutation) => {
600
+ if (mutation.type === 'childList') {
601
+ mutation.addedNodes.forEach((node) => {
602
+ if (node.nodeType === Node.ELEMENT_NODE) {
603
+ highlightNode(node);
604
+ }
605
+ });
606
+ }
607
+ });
608
+ refreshElementsPanel();
609
+ });
610
+
611
+ observer.observe(document.documentElement, {
612
+ childList: true,
613
+ subtree: true,
614
+ attributes: true,
615
+ characterData: true
616
+ });
617
  // ノードをハイライト表示
618
  function highlightNode(node) {
619
  const elementId = node.id || Math.random().toString(36).substr(2, 9);
 
631
  }
632
 
633
  // 開発者ツールのメイン関数
634
+ const createDevTools = () => {
635
  const container = document.createElement('div');
636
  container.className = 'devtools-container';
637
  container.id = 'devtools-container';
 
791
  container.appendChild(ttfbCard);
792
 
793
  // Web Vitalsの計測を開始
794
+ loadWebVitals().then(() => {
795
+ if (window.webVitals) {
796
+ try {
797
  window.webVitals.getCLS(updateCLS);
798
  window.webVitals.getFCP(updateFCP);
799
  window.webVitals.getFID(updateFID);
800
  window.webVitals.getLCP(updateLCP);
801
  window.webVitals.getTTFB(updateTTFB);
802
+ } catch (e) {
803
+ console.error('Web Vitals error:', e);
804
+ }
805
+ }
806
+ });
807
  function updateCLS(metric) {
808
  vitalsData.CLS = metric.value;
809
  updateVitalDisplay('cls', metric.value, 0.1, 0.25);