Sfoglia il codice sorgente

Display three.js version in the toolbar icon.

Mr.doob 9 mesi fa
parent
commit
b25cbdc800
3 ha cambiato i file con 37 aggiunte e 2 eliminazioni
  1. 34 0
      devtools/background.js
  2. 2 2
      devtools/bridge.js
  3. 1 0
      devtools/manifest.json

+ 34 - 0
devtools/background.js

@@ -45,6 +45,18 @@ chrome.runtime.onMessage.addListener( ( message, sender, sendResponse ) => {
 	if ( sender.tab ) {
 
 		const tabId = sender.tab.id;
+
+		// If three.js is detected, show a badge
+		if ( message.name === 'register' && message.detail && message.detail.revision ) {
+
+			const revision = String( parseInt( message.detail.revision, 10 ) );
+
+			chrome.action.setBadgeText( { tabId: tabId, text: revision } ).catch( () => { /* Tab might be gone */ } );
+			chrome.action.setBadgeTextColor( { tabId: tabId, color: '#ffffff' } ).catch( () => { /* Tab might be gone */ } );
+			chrome.action.setBadgeBackgroundColor( { tabId: tabId, color: '#049ef4' } ).catch( () => { /* Tab might be gone */ } );
+
+		}
+
 		const port = connections.get( tabId );
 		if ( port ) {
 
@@ -75,6 +87,14 @@ chrome.runtime.onMessage.addListener( ( message, sender, sendResponse ) => {
 chrome.webNavigation.onCommitted.addListener( details => {
 
 	const { tabId, frameId } = details;
+
+	// Clear badge on navigation, only for top-level navigation
+	if ( frameId === 0 ) {
+
+		chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => { /* Tab might be gone */ } );
+
+	}
+
 	const port = connections.get( tabId );
 
 	if ( port ) {
@@ -88,3 +108,17 @@ chrome.webNavigation.onCommitted.addListener( details => {
 	}
 
 } );
+
+// Clear badge when a tab is closed
+chrome.tabs.onRemoved.addListener( ( tabId, removeInfo ) => {
+
+	chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => { /* Tab might be gone */ } );
+
+	// Clean up connection if it exists for the closed tab
+	if ( connections.has( tabId ) ) {
+
+		connections.delete( tabId );
+
+	}
+
+} );

+ 2 - 2
devtools/bridge.js

@@ -393,13 +393,13 @@
 
 		}
 
-		function dispatchEvent( type, detail ) {
+		function dispatchEvent( name, detail ) {
 
 			try {
 
 				window.postMessage( {
 					id: 'three-devtools',
-					type: type,
+					name: name,
 					detail: detail
 				}, '*' );
 

+ 1 - 0
devtools/manifest.json

@@ -9,6 +9,7 @@
 		"48": "icons/48.png",
 		"128": "icons/128.png"
 	},
+	"action": {},
 	"devtools_page": "index.html",
 	"background": {
 		"service_worker": "background.js",

粤ICP备19079148号