Skip to content

Commit 71bde5d

Browse files
committed
Clean up.
1 parent b2d1739 commit 71bde5d

File tree

5 files changed

+35
-121
lines changed

5 files changed

+35
-121
lines changed

devtools/background.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global chrome */
2+
13
// Map tab IDs to connections
24
const connections = new Map();
35

@@ -122,7 +124,7 @@ chrome.webNavigation.onCommitted.addListener( details => {
122124
} );
123125

124126
// Clear badge when a tab is closed
125-
chrome.tabs.onRemoved.addListener( ( tabId, removeInfo ) => {
127+
chrome.tabs.onRemoved.addListener( ( tabId ) => {
126128

127129
chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => { /* Tab might be gone */ } );
128130

devtools/bridge.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@
347347
// Check if THREE is in the global scope (Old versions)
348348
window.addEventListener( 'load', () => {
349349

350-
if ( window.THREE && window.THREE.REVISION) {
351-
350+
if ( window.THREE && window.THREE.REVISION ) {
351+
352352
dispatchEvent( 'register', { revision: THREE.REVISION } );
353-
353+
354354
}
355-
355+
356356
} );
357357

358358
// Watch for page unload to reset state

devtools/content-script.js

Lines changed: 24 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
/* global chrome */
2+
13
// This script runs in the context of the web page
24
// console.log( 'Three.js DevTools: Content script loaded at document_readyState:', document.readyState ); // Comment out
35

4-
// Function to inject the bridge script
6+
// Inject the bridge script into the main document or a target (e.g., iframe)
57
function injectBridge( target = document ) {
68

79
const script = document.createElement( 'script' );
@@ -13,27 +15,20 @@ function injectBridge( target = document ) {
1315
};
1416

1517
( target.head || target.documentElement ).appendChild( script );
16-
1718
return script;
1819

1920
}
2021

21-
// Also inject into any existing iframes
22+
// Inject bridge into all existing iframes
2223
function injectIntoIframes() {
2324

24-
const iframes = document.querySelectorAll( 'iframe' );
25-
iframes.forEach( iframe => {
25+
document.querySelectorAll( 'iframe' ).forEach( iframe => {
2626

2727
try {
2828

29-
injectBridge( iframe.contentDocument );
30-
31-
} catch ( e ) {
29+
if ( iframe.contentDocument ) injectBridge( iframe.contentDocument );
3230

33-
// Ignore cross-origin iframe errors
34-
// console.log( 'DevTools: Could not inject into iframe:', e ); // Comment out
35-
36-
}
31+
} catch ( e ) { /* Ignore cross-origin errors */ }
3732

3833
} );
3934

@@ -44,27 +39,21 @@ injectBridge();
4439
injectIntoIframes();
4540

4641
// Watch for new iframes being added
47-
const observer = new MutationObserver( mutations => {
42+
new MutationObserver( mutations => {
4843

4944
mutations.forEach( mutation => {
5045

5146
mutation.addedNodes.forEach( node => {
5247

5348
if ( node.tagName === 'IFRAME' ) {
5449

55-
// Wait for iframe to load
5650
node.addEventListener( 'load', () => {
5751

5852
try {
5953

60-
injectBridge( node.contentDocument );
61-
62-
} catch ( e ) {
63-
64-
// Ignore cross-origin iframe errors
65-
// console.log( 'DevTools: Could not inject into iframe:', e ); // Comment out
54+
if ( node.contentDocument ) injectBridge( node.contentDocument );
6655

67-
}
56+
} catch ( e ) { /* Ignore cross-origin errors */ }
6857

6958
} );
7059

@@ -74,19 +63,13 @@ const observer = new MutationObserver( mutations => {
7463

7564
} );
7665

77-
} );
66+
} ).observe( document.documentElement, { childList: true, subtree: true } );
7867

79-
observer.observe( document.documentElement, {
80-
childList: true,
81-
subtree: true
82-
} );
83-
84-
// Helper function to check if extension context is valid
68+
// Helper to check if extension context is valid
8569
function isExtensionContextValid() {
8670

8771
try {
8872

89-
// This will throw if context is invalidated
9073
chrome.runtime.getURL( '' );
9174
return true;
9275

@@ -98,120 +81,47 @@ function isExtensionContextValid() {
9881

9982
}
10083

101-
// Handle messages from the main window
102-
function handleMainWindowMessage( event ) {
103-
104-
// Only accept messages from the same frame
105-
if ( event.source !== window ) {
106-
107-
return;
108-
109-
}
110-
111-
const message = event.data;
112-
if ( ! message || message.id !== 'three-devtools' ) {
113-
114-
return;
115-
116-
}
117-
118-
// Check extension context before sending message
119-
if ( ! isExtensionContextValid() ) {
120-
121-
console.warn( 'Extension context invalidated, cannot send message' );
122-
return;
123-
124-
}
125-
126-
// Add source information
127-
const messageWithSource = {
128-
...event.data,
129-
source: event.source === window ? 'main' : 'iframe'
130-
};
131-
132-
// Forward to background page
133-
chrome.runtime.sendMessage( messageWithSource );
134-
135-
}
136-
137-
// Handle messages from iframes
138-
function handleIframeMessage( event ) {
139-
140-
// Skip messages from main window
141-
if ( event.source === window ) {
142-
143-
return;
144-
145-
}
84+
// Unified message handler for window messages
85+
function handleWindowMessage( event ) {
14686

147-
const message = event.data;
148-
if ( ! message || message.id !== 'three-devtools' ) {
87+
// Only accept messages with the correct id
88+
if ( ! event.data || event.data.id !== 'three-devtools' ) return;
14989

150-
return;
151-
152-
}
90+
// Determine source: 'main' for window, 'iframe' otherwise
91+
const source = event.source === window ? 'main' : 'iframe';
15392

154-
// Check extension context before sending message
15593
if ( ! isExtensionContextValid() ) {
15694

15795
console.warn( 'Extension context invalidated, cannot send message' );
15896
return;
15997

16098
}
16199

162-
// Add source information
163-
const messageWithSource = {
164-
...event.data,
165-
source: 'iframe'
166-
};
167-
168-
// Forward to background page
100+
const messageWithSource = { ...event.data, source };
169101
chrome.runtime.sendMessage( messageWithSource );
170102

171103
}
172104

173-
// Listener for messages forwarded from the background script (originating from panel)
174-
function handleBackgroundMessage( message, sender, sendResponse ) {
105+
// Listener for messages from the background script (originating from panel)
106+
function handleBackgroundMessage( message ) {
175107

176-
// Check if the message is one we need to forward to the bridge
177-
// Only forward request-state now
178108
if ( message.name === 'request-state' ) {
179109

180-
// console.log( 'Content script: Forwarding message to bridge:', message.name );
181-
// Ensure the message has the correct ID before forwarding to the page
182110
message.id = 'three-devtools';
183-
window.postMessage( message, '*' ); // Forward the modified message to the page
184-
185-
// Optional: Forward to iframes too, if needed (might cause duplicates if bridge is in iframe)
186-
/*
187-
const iframes = document.querySelectorAll('iframe');
188-
iframes.forEach(iframe => {
189-
try {
190-
iframe.contentWindow.postMessage(message, '*');
191-
} catch (e) {}
192-
});
193-
*/
111+
window.postMessage( message, '*' );
194112

195113
}
196-
// Keep channel open? No, this listener is synchronous for now.
197-
// return true;
198114

199115
}
200116

201117
// Add event listeners
202-
window.addEventListener( 'message', handleMainWindowMessage, false );
203-
window.addEventListener( 'message', handleIframeMessage, false );
204-
// chrome.runtime.onMessage.addListener( handleDevtoolsMessage ); // This seems redundant/incorrectly placed in original code
205-
206-
// Use a single listener for messages from the background script
118+
window.addEventListener( 'message', handleWindowMessage, false );
207119
chrome.runtime.onMessage.addListener( handleBackgroundMessage );
208120

209121
// Icon color scheme
210122
const isLightTheme = window.matchMedia( '(prefers-color-scheme: light)' ).matches;
211-
212123
chrome.runtime.sendMessage( { scheme: isLightTheme ? 'light' : 'dark' } );
213-
214-
window.matchMedia( '(prefers-color-scheme: light)' ).onchange = ( event ) => {
124+
window.matchMedia( '(prefers-color-scheme: light)' ).onchange = event => {
215125

216126
chrome.runtime.sendMessage( { scheme: event.matches ? 'light' : 'dark' } );
217127

devtools/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "Three.js DevTools",
4-
"version": "1.8",
4+
"version": "1.9",
55
"description": "Developer tools extension for Three.js",
66
"icons": {
77
"128": "icons/128-light.png"

devtools/panel/panel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global chrome */
2+
13
// Store the state of our inspector
24
const state = {
35
revision: null,
@@ -425,7 +427,7 @@ function updateUI() {
425427
header.style.justifyContent = 'space-between'; // Align items left and right
426428

427429
const miscSpan = document.createElement( 'span' );
428-
miscSpan.innerHTML = `<a href="https://docs.google.com/forms/d/e/1FAIpQLSdw1QcgXNiECYiPx6k0vSQRiRe0FmByrrojV4fgeL5zzXIiCw/viewform?usp=preview" target="_blank">+</a>`;
430+
miscSpan.innerHTML = '<a href="https://docs.google.com/forms/d/e/1FAIpQLSdw1QcgXNiECYiPx6k0vSQRiRe0FmByrrojV4fgeL5zzXIiCw/viewform?usp=preview" target="_blank">+</a>';
429431

430432
const manifest = chrome.runtime.getManifest();
431433

0 commit comments

Comments
 (0)