@@ -114,126 +114,7 @@ document
114114 statusMessage . className = "" ;
115115
116116 try {
117- // Get info about the active tab
118- const tabs = await browserAPI . tabs . query ( {
119- active : true ,
120- currentWindow : true ,
121- } ) ;
122- if ( tabs . length === 0 ) {
123- throw new Error ( browserAPI . i18n . getMessage ( "popupErrorNoActiveTab" ) ) ;
124- }
125- const activeTab = tabs [ 0 ] ;
126- const currentUrl = activeTab . url ;
127-
128- // Get browser and platform info
129- const browserInfo = await browserAPI . runtime . getBrowserInfo ?. ( ) || { } ;
130- const platformInfo = await browserAPI . runtime . getPlatformInfo ?. ( ) || { } ;
131-
132- // Create default payload
133- let payload = {
134- tab : {
135- title : activeTab . title ,
136- url : currentUrl ,
137- id : activeTab . id ,
138- windowId : activeTab . windowId ,
139- index : activeTab . index ,
140- pinned : activeTab . pinned ,
141- audible : activeTab . audible ,
142- mutedInfo : activeTab . mutedInfo ,
143- incognito : activeTab . incognito ,
144- status : activeTab . status ,
145- } ,
146- browser : browserInfo ,
147- platform : platformInfo ,
148- triggeredAt : new Date ( ) . toISOString ( ) ,
149- } ;
150-
151- if ( webhook && webhook . identifier ) {
152- payload . identifier = webhook . identifier ;
153- }
154-
155- // Use custom payload if available
156- // The custom payload is a JSON string that can contain placeholders like {{tab.title}}
157- // These placeholders will be replaced with actual values before sending the webhook
158- if ( webhook && webhook . customPayload ) {
159- try {
160- // Create variable replacements map
161- const replacements = {
162- "{{tab.title}}" : activeTab . title ,
163- "{{tab.url}}" : currentUrl ,
164- "{{tab.id}}" : activeTab . id ,
165- "{{tab.windowId}}" : activeTab . windowId ,
166- "{{tab.index}}" : activeTab . index ,
167- "{{tab.pinned}}" : activeTab . pinned ,
168- "{{tab.audible}}" : activeTab . audible ,
169- "{{tab.incognito}}" : activeTab . incognito ,
170- "{{tab.status}}" : activeTab . status ,
171- "{{browser}}" : JSON . stringify ( browserInfo ) ,
172- "{{platform.arch}}" : platformInfo . arch || "unknown" ,
173- "{{platform.os}}" : platformInfo . os || "unknown" ,
174- "{{platform.version}}" : platformInfo . version ,
175- "{{triggeredAt}}" : new Date ( ) . toISOString ( ) ,
176- "{{identifier}}" : webhook . identifier || ""
177- } ;
178-
179- // Replace placeholders in custom payload
180- let customPayloadStr = webhook . customPayload ;
181- Object . entries ( replacements ) . forEach ( ( [ placeholder , value ] ) => {
182- // Handle different types of values
183- // For string values in JSON, we need to handle them differently based on context
184- // If the placeholder is inside quotes in the JSON, we should not add quotes again
185- const isPlaceholderInQuotes = customPayloadStr . match ( new RegExp ( `"[^"]*${ placeholder . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) } [^"]*"` , 'g' ) ) ;
186-
187- const replaceValue = typeof value === 'string'
188- ? ( isPlaceholderInQuotes ? value . replace ( / " / g, '\\"' ) : `"${ value . replace ( / " / g, '\\"' ) } "` )
189- : ( value === undefined ? 'null' : JSON . stringify ( value ) ) ;
190-
191- customPayloadStr = customPayloadStr . replace (
192- new RegExp ( placeholder . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) , 'g' ) ,
193- replaceValue
194- ) ;
195- } ) ;
196-
197- // Parse the resulting JSON
198- const customPayload = JSON . parse ( customPayloadStr ) ;
199-
200- // Use the custom payload instead of the default one
201- payload = customPayload ;
202- } catch ( error ) {
203- throw new Error ( browserAPI . i18n . getMessage ( "popupErrorCustomPayloadJsonParseError" , error . message ) ) ;
204- }
205- }
206- // Prepare headers
207- let headers = { "Content-Type" : "application/json" } ;
208- if ( webhook && Array . isArray ( webhook . headers ) ) {
209- webhook . headers . forEach ( h => {
210- if ( h . key && h . value ) headers [ h . key ] = h . value ;
211- } ) ;
212- }
213- // Determine method
214- const method = webhook && webhook . method ? webhook . method : "POST" ;
215- // Prepare fetch options
216- const fetchOpts = {
217- method,
218- headers,
219- } ;
220- if ( method === "POST" ) {
221- fetchOpts . body = JSON . stringify ( payload ) ;
222- } else if ( method === "GET" ) {
223- // For GET, append payload as query param
224- const urlObj = new URL ( url ) ;
225- urlObj . searchParams . set ( "payload" , encodeURIComponent ( JSON . stringify ( payload ) ) ) ;
226- fetchOpts . body = undefined ;
227- // Overwrite url for fetch
228- fetchOpts . _url = urlObj . toString ( ) ;
229- }
230- // Send the request
231- const fetchUrl = fetchOpts . _url || url ;
232- const response = await fetch ( fetchUrl , fetchOpts ) ;
233-
234- if ( ! response . ok ) {
235- throw new Error ( browserAPI . i18n . getMessage ( "popupErrorHttp" , response . status ) ) ;
236- }
117+ await window . sendWebhook ( webhook , false ) ;
237118
238119 // Success feedback
239120 statusMessage . textContent = browserAPI . i18n . getMessage ( "popupStatusSuccess" ) ;
0 commit comments