background.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* global chrome */
  2. // Constants
  3. const MESSAGE_ID = 'three-devtools';
  4. const MESSAGE_INIT = 'init';
  5. const MESSAGE_REQUEST_STATE = 'request-state';
  6. const MESSAGE_REQUEST_OBJECT_DETAILS = 'request-object-details';
  7. const MESSAGE_SCROLL_TO_CANVAS = 'scroll-to-canvas';
  8. const MESSAGE_REGISTER = 'register';
  9. const MESSAGE_COMMITTED = 'committed';
  10. // Map tab IDs to connections
  11. const connections = new Map();
  12. // Handle extension icon clicks in the toolbar
  13. chrome.action.onClicked.addListener( ( tab ) => {
  14. // Send scroll-to-canvas message to the content script (no UUID = scroll to first canvas)
  15. chrome.tabs.sendMessage( tab.id, {
  16. name: MESSAGE_SCROLL_TO_CANVAS,
  17. tabId: tab.id
  18. } ).catch( () => {
  19. // Ignore error - tab might not have the content script injected
  20. console.log( 'Could not send scroll-to-canvas message to tab', tab.id );
  21. } );
  22. } );
  23. // Listen for connections from the devtools panel
  24. chrome.runtime.onConnect.addListener( port => {
  25. let tabId;
  26. // Messages that should be forwarded to content script
  27. const forwardableMessages = new Set( [ MESSAGE_REQUEST_STATE, MESSAGE_REQUEST_OBJECT_DETAILS, MESSAGE_SCROLL_TO_CANVAS ] );
  28. // Listen for messages from the devtools panel
  29. port.onMessage.addListener( message => {
  30. if ( message.name === MESSAGE_INIT ) {
  31. tabId = message.tabId;
  32. connections.set( tabId, port );
  33. } else if ( forwardableMessages.has( message.name ) && tabId ) {
  34. chrome.tabs.sendMessage( tabId, message );
  35. } else if ( tabId === undefined ) {
  36. console.warn( 'Background: Message received from panel before init:', message );
  37. }
  38. } );
  39. // Clean up when devtools is closed
  40. port.onDisconnect.addListener( () => {
  41. if ( tabId ) {
  42. connections.delete( tabId );
  43. }
  44. } );
  45. } );
  46. // Listen for messages from the content script
  47. chrome.runtime.onMessage.addListener( ( message, sender, sendResponse ) => {
  48. if ( message.scheme ) {
  49. chrome.action.setIcon( {
  50. path: {
  51. 128: `icons/128-${message.scheme}.png`
  52. }
  53. } );
  54. }
  55. if ( sender.tab ) {
  56. const tabId = sender.tab.id;
  57. // If three.js is detected, show a badge
  58. if ( message.name === MESSAGE_REGISTER && message.detail && message.detail.revision ) {
  59. const revision = String( message.detail.revision );
  60. const number = revision.replace( /\D+$/, '' );
  61. const isDev = revision.includes( 'dev' );
  62. chrome.action.setBadgeText( { tabId: tabId, text: number } ).catch( () => {
  63. // Ignore error - tab might have been closed
  64. } );
  65. chrome.action.setBadgeTextColor( { tabId: tabId, color: '#ffffff' } ).catch( () => {
  66. // Ignore error - tab might have been closed
  67. } );
  68. chrome.action.setBadgeBackgroundColor( { tabId: tabId, color: isDev ? '#ff0098' : '#049ef4' } ).catch( () => {
  69. // Ignore error - tab might have been closed
  70. } );
  71. }
  72. const port = connections.get( tabId );
  73. if ( port ) {
  74. // Forward the message to the devtools panel
  75. try {
  76. port.postMessage( message );
  77. // Send immediate response to avoid "message channel closed" error
  78. sendResponse( { received: true } );
  79. } catch ( e ) {
  80. console.error( 'Error posting message to devtools:', e );
  81. // If the port is broken, clean up the connection
  82. connections.delete( tabId );
  83. }
  84. }
  85. }
  86. return false; // Return false to indicate synchronous handling
  87. } );
  88. // Listen for page navigation events
  89. chrome.webNavigation.onCommitted.addListener( details => {
  90. const { tabId, frameId } = details;
  91. // Clear badge on navigation, only for top-level navigation
  92. if ( frameId === 0 ) {
  93. chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => {
  94. // Ignore error - tab might have been closed
  95. } );
  96. }
  97. const port = connections.get( tabId );
  98. if ( port ) {
  99. port.postMessage( {
  100. id: MESSAGE_ID,
  101. name: MESSAGE_COMMITTED,
  102. frameId: frameId
  103. } );
  104. }
  105. } );
  106. // Clear badge when a tab is closed
  107. chrome.tabs.onRemoved.addListener( ( tabId ) => {
  108. chrome.action.setBadgeText( { tabId: tabId, text: '' } ).catch( () => {
  109. // Ignore error - tab is already gone
  110. } );
  111. // Clean up connection if it exists for the closed tab
  112. if ( connections.has( tabId ) ) {
  113. connections.delete( tabId );
  114. }
  115. } );
粤ICP备19079148号