@@ -77,6 +77,25 @@ export default async function ({ addon, console }) {
7777 }
7878 return addon . settings . get ( id ) ;
7979 } ;
80+ const isSupportedMainButton = ( mainButton ) => {
81+ if ( ! mainButton ) {
82+ return false ;
83+ }
84+ const assetPanelWrapper = mainButton . closest ( "[class*=asset-panel_wrapper_]" ) ;
85+ const stageSelector = mainButton . closest ( '[class*="target-pane_stage-selector-wrapper"]' ) ;
86+ if ( assetPanelWrapper || stageSelector ) {
87+ return true ;
88+ }
89+
90+ const spriteSelector = mainButton . closest ( '[class*="sprite-selector"]' ) ;
91+ if ( ! spriteSelector ) {
92+ return false ;
93+ }
94+
95+ const moreButtonsElement = mainButton . parentElement &&
96+ mainButton . parentElement . querySelector ( '[class*="action-menu_more-buttons_"]' ) ;
97+ return Boolean ( moreButtonsElement && moreButtonsElement . children && moreButtonsElement . children . length >= 4 ) ;
98+ } ;
8099 const getButtonToClick = ( mainButton ) => {
81100 const assetPanelWrapper = mainButton . closest ( "[class*=asset-panel_wrapper_]" ) ;
82101 if ( assetPanelWrapper ) {
@@ -101,13 +120,25 @@ export default async function ({ addon, console }) {
101120 if ( ! mainButton ) {
102121 return ;
103122 }
123+ if ( ! isSupportedMainButton ( mainButton ) ) {
124+ return ;
125+ }
104126 e . stopPropagation ( ) ;
105127 const moreButtonsElement = mainButton . parentElement . querySelector ( '[class*="action-menu_more-buttons_"]' ) ;
128+ if ( ! moreButtonsElement || ! moreButtonsElement . children || moreButtonsElement . children . length < 4 ) {
129+ return ;
130+ }
106131 const moreButtons = moreButtonsElement . children ;
107132 const { index } = getButtonToClick ( mainButton ) ;
108133 // better-img-uploads can add a button at the start, so search "from the end" for compatibility
109134 const buttonToClick = moreButtons [ moreButtons . length - ( 4 - index ) ] ;
135+ if ( ! buttonToClick ) {
136+ return ;
137+ }
110138 const elementToClick = buttonToClick . querySelector ( "button" ) ;
139+ if ( ! elementToClick ) {
140+ return ;
141+ }
111142 elementToClick . click ( ) ;
112143 } ,
113144 {
@@ -121,7 +152,13 @@ export default async function ({ addon, console }) {
121152 if ( ! mainButton ) {
122153 return ;
123154 }
155+ if ( ! isSupportedMainButton ( mainButton ) ) {
156+ return ;
157+ }
124158 const tooltipElement = mainButton . parentElement . querySelector ( ".__react_component_tooltip" ) ;
159+ if ( ! tooltipElement ) {
160+ return ;
161+ }
125162 const { tooltip } = getButtonToClick ( mainButton ) ;
126163 const translatedTooltip = addon . tab . redux . state . locales . messages [ tooltip ] ;
127164 const needToFixTooltipText = translatedTooltip && tooltipElement . textContent !== translatedTooltip ;
0 commit comments