@@ -2,7 +2,7 @@ import Plugin from "src/plugin-system/plugin.class";
22
33export default class BuyButtonStateSynchronizer extends Plugin {
44 static options = {
5- showDialogInsteadOfDisableButton : null ,
5+ showDialogInsteadOfDisableButton : false ,
66 } ;
77
88 /**
@@ -47,7 +47,7 @@ export default class BuyButtonStateSynchronizer extends Plugin {
4747 * is left.
4848 */
4949 static disableBuyButton ( id , actionLabel = null , actionCallback = null ) {
50- const buyButton = document . getElementsByClassName ( " btn-buy") . item ( 0 ) ;
50+ const buyButton = document . querySelector ( ". btn-buy") ;
5151 if ( buyButton == null ) {
5252 console . warn ( "Cannot disable buy button: No element with class btn-buy" ) ;
5353 return ;
@@ -75,7 +75,7 @@ export default class BuyButtonStateSynchronizer extends Plugin {
7575 * @param {string } id The ID of the vote that is withdrawn.
7676 */
7777 static enableBuyButton ( id ) {
78- const buyButton = document . getElementsByClassName ( " btn-buy") . item ( 0 ) ;
78+ const buyButton = document . querySelector ( ". btn-buy") ;
7979 if ( buyButton == null ) {
8080 console . warn ( "Cannot enable buy button: No element with class btn-buy" ) ;
8181 return ;
@@ -88,6 +88,15 @@ export default class BuyButtonStateSynchronizer extends Plugin {
8888 }
8989
9090 init ( ) {
91+ const buyForm = document . getElementById ( "productDetailPageBuyProductForm" ) ;
92+ if ( buyForm == null ) {
93+ return ; // We are not on the product details page
94+ }
95+ this . buyButton = document . querySelector ( ".btn-buy" ) ;
96+ if ( this . buyButton == null ) {
97+ console . warn ( "Cannot initialize: No element with class btn-buy" ) ;
98+ return ;
99+ }
91100 if ( ! window . buyButtonStateSynchronizerActions ) {
92101 window . buyButtonStateSynchronizerActions = { } ;
93102 }
@@ -107,12 +116,14 @@ export default class BuyButtonStateSynchronizer extends Plugin {
107116 setTimeout ( this . buyButtonNodeChanged . bind ( this ) ) ;
108117
109118 if ( this . options . showDialogInsteadOfDisableButton ) {
110- this . el . addEventListener ( "click" , this . onBuyButtonClicked . bind ( this ) ) ;
111- this . el . disabled = false ;
119+ this . buyButton . addEventListener (
120+ "click" ,
121+ this . onBuyButtonClicked . bind ( this )
122+ ) ;
123+ this . buyButton . disabled = false ;
112124 }
113125
114126 // Prevent form submit by pressing enter if buy button is disabled
115- const buyForm = document . getElementById ( "productDetailPageBuyProductForm" ) ;
116127 buyForm . addEventListener ( "keydown" , ( event ) => {
117128 if ( event . code == "Enter" || event . code == "NumpadEnter" ) {
118129 return this . onBuyButtonClicked ( event ) ;
@@ -126,23 +137,22 @@ export default class BuyButtonStateSynchronizer extends Plugin {
126137 // stop observing attribute changes...
127138 this . stopObservingBuyButtonAttributeChanges ( ) ;
128139 // ...otherwise this line would cause an infinite loop...
129- this . el . disabled = this . isDisabled ;
140+ this . buyButton . disabled = this . isDisabled ;
130141 // ...start observing again
131142 this . startObservingBuyButtonAttributeChanges ( ) ;
132143 }
133144 }
134145
135146 startObservingBuyButtonAttributeChanges ( ) {
136- // this.el is the element on which this plugin was initialized -> the buy button
137- this . buyButtonObserver . observe ( this . el , { attributes : true } ) ;
147+ this . buyButtonObserver . observe ( this . buyButton , { attributes : true } ) ;
138148 }
139149
140150 stopObservingBuyButtonAttributeChanges ( ) {
141151 this . buyButtonObserver . disconnect ( ) ;
142152 }
143153
144154 isBuyButtonDisabled ( ) {
145- for ( let attribute of this . el . attributes ) {
155+ for ( let attribute of this . buyButton . attributes ) {
146156 if ( attribute . name . endsWith ( "-buy-button-disabled" ) ) {
147157 return true ;
148158 }
0 commit comments