background.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* global chrome */
  2. // Map tab IDs to connections
  3. const connections = new Map();
  4. // Listen for connections from the devtools panel
  5. chrome.runtime.onConnect.addListener( port => {
  6. let tabId;
  7. // Listen for messages from the devtools panel
  8. port.onMessage.addListener( message => {
  9. if ( message.name === 'init' ) {
  10. tabId = message.tabId;
  11. connections.set( tabId, port );
  12. } else if ( message.name === 'request-state' && tabId ) {
  13. chrome.tabs.sendMessage( tabId, message );
  14. } else if ( message.name === 'request-object-details' && tabId ) {
  15. chrome.tabs.sendMessage( tabId, message );
  16. } else if ( message.name === 'scroll-to-canvas' && tabId ) {
  17. chrome.tabs.sendMessage( tabId, message );
  18. } else if ( tabId === undefined ) {
  19. console.warn( 'Background: Message received from panel before init:', message );
  20. }
  21. } );
  22. // Clean up when devtools is closed
  23. port.onDisconnect.addListener( () => {
  24. if ( tabId ) {
  25. connections.delete( tabId );
  26. }
  27. } );
  28. } );
  29. // Listen for messages from the content script
  30. chrome.runtime.onMessage.addListener( ( message, sender, sendResponse ) => {
  31. if ( message.scheme ) {
  32. chrome.action.setIcon( {
  33. path: {
  34. 128: `icons/128-${message.scheme}.png`
  35. }
  36. } );
  37. }
  38. if ( sender.tab ) {
  39. const tabId = sender.tab.id;
  40. // If three.js is detected, show a badge
  41. if ( message.name === 'register' && message.detail && message.detail.revision ) {
  42. const revision = String( message.detail.revision );
  43. const number = revision.replace( /\D+$/, '' );
  44. const isDev = revision.includes( 'dev' );
  45. chrome.action.setBadgeText( { tabId: tabId, text: number } ).catch( () => { /* Tab might be gone */ } );
  46. chrome.action.setBadgeTextColor( { tabId: tabId, color: '#ffffff' } ).catch( () => { /* Tab might be gone */ } );
  47. chrome.action.setBadgeBackgroundColor( { tabId: tabId, color: isDev ? '#ff0098' : '#049ef4' } ).catch( () => { /* Tab might be gone */ } );
  48. }
  49. const port = connections.get( tabId );
  50. if ( port ) {
  51. // Forward the message to the devtools panel
  52. try {
  53. port.postMessage( message );
  54. // Send immediate response to avoid "message channel closed" error
  55. sendResponse( { received: true } );
  56. } catch ( e ) {
  57. console.error( 'Error posting message to devtools:', e );
  58. // If the port is broken, clean up the connection
  59. connections.delete( tabId );
  60. }
  61. }
  62. }
  63. return false; // Return false to indicate synchronous handling
  64. } );
  65. // Listen for page navigation events
  66. chrome.webNavigation.onCommitted.addListener( details => {
  67. const { tabId, frameId } = details;
  68. // Clear badge on navigation, only for top-level navigation
  69. if ( frameId === 0 ) {
  70. chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => { /* Tab might be gone */ } );
  71. }
  72. const port = connections.get( tabId );
  73. if ( port ) {
  74. port.postMessage( {
  75. id: 'three-devtools',
  76. name: 'committed',
  77. frameId: frameId
  78. } );
  79. }
  80. } );
  81. // Clear badge when a tab is closed
  82. chrome.tabs.onRemoved.addListener( ( tabId ) => {
  83. chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => { /* Tab might be gone */ } );
  84. // Clean up connection if it exists for the closed tab
  85. if ( connections.has( tabId ) ) {
  86. connections.delete( tabId );
  87. }
  88. } );
粤ICP备19079148号