background.js 3.0 KB

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