CookieCloud icon indicating copy to clipboard operation
CookieCloud copied to clipboard

Sweep: add webhook when uploading cookie #69

Open easychen opened this issue 1 year ago • 2 comments

add webhook to extension/popup.tsx webhook is a input for a url, and will post uploaded cookie to it when sync finished

easychen avatar May 18 '24 05:05 easychen

🚀 Here's the PR! #70

💎 Sweep Pro: You have unlimited Sweep issues

Actions

  • [ ] ↻ Restart Sweep

Step 1: 🔎 Searching

Here are the code search results. I'm now analyzing these search results to write the PR.

Relevant files (click to expand). Mentioned files will always appear here.

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/popup.tsx#L1-L181

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/function.js#L1-L413

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/background/index.ts#L1-L133

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/tsconfig.json#L1-L18

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/style.scss#L1-L5

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/popup.html#L1-L8

https://github.com/easychen/CookieCloud/blob/09cffd528867544b9c18c5abcf822c3fe1cafc11/extension/background/messages/config.ts#L1-L23

Step 2: ⌨️ Coding

extension/popup.tsx

Add a new input field for the webhook URL in the configuration form.
--- 
+++ 
@@ -1,7 +1,7 @@
         {data['type'] && data['type'] == 'up' && <>
         <div className="">{browser.i18n.getMessage('syncLocalStorageOrNot')}</div>
         <div className="my-2 flex flex-row items-center">
-        {/*  
+        {/*
         <Radio.Group onChange={e=>onChange('with_storage',e)} value={data['with_storage']}>
           <Radio value={1}>是</Radio>
           <Radio value={0}>否</Radio>
@@ -12,4 +12,7 @@
         </div>
 
         <div className="">{browser.i18n.getMessage('requestHeader')}</div>
-        <textarea className="border-1  my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('requestHeaderPlaceholder')}  onChange={e=>onChange('headers',e)} value={data['headers']}/>
+        <textarea className="border-1  my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('requestHeaderPlaceholder')}  onChange={e=>onChange('headers',e)} value={data['headers']}/>
+        
+        <div className="">{browser.i18n.getMessage('webhookUrl')}</div>
+        <input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('webhookUrlPlaceholder')} value={data['webhook_url']} onChange={e=>onChange('webhook_url',e)} />

extension/popup.tsx

Update the `data` state object to include the new `webhook_url` property.
--- 
+++ 
@@ -1,3 +1,3 @@
 function IndexPopup() {
-  let init: Object={"endpoint":"http://127.0.0.1:8088","password":"","interval":10,"domains":"","uuid":String(short_uid.generate()),"type":"up","keep_live":"","with_storage":1,"blacklist":"google.com", "headers": "","expire_minutes":60*24*365};
+  let init: Object={"endpoint":"http://127.0.0.1:8088","password":"","interval":10,"domains":"","uuid":String(short_uid.generate()),"type":"up","keep_live":"","with_storage":1,"blacklist":"google.com", "headers": "","expire_minutes":60*24*365,"webhook_url":""};
   const [data, setData] = useState(init);

extension/function.js

Modify the `upload_cookie` function to send the uploaded data to the specified webhook URL.
--- 
+++ 
@@ -7,12 +7,26 @@
         });
         const result = await response.json();
 
-        if( result && result.action === 'done' ) 
+        if( result && result.action === 'done' ) {
             await save_data( 'LAST_UPLOADED_COOKIE', {"timestamp": new Date().getTime(), "sha256":sha256 } );    
+            
+            if(payload['webhook_url']) {
+                try {
+                    const webhook_response = await fetch(payload['webhook_url'], {
+                        method: 'POST',
+                        headers: {'Content-Type': 'application/json'},
+                        body: JSON.stringify({"cookie_data":cookies,"local_storage_data":local_storages})
+                    });
+                    console.log("Webhook response:", webhook_response);
+                } catch (error) {
+                    console.log("Webhook error:", error);
+                }
+            }
+        }
 
         return result;
     } catch (error) {
         console.log("error", error);
         showBadge("err");
         return false;
-    }  
+    }

Step 3: 🔄️ Validating

Your changes have been successfully made to the branch sweep/add_webhook_when_uploading_cookie_69. I have validated these changes using a syntax checker and a linter.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.

add webhook to extension/popup.tsx webhook is a input for a url, and will post uploaded cookie to it when sync finished

easychen avatar May 18 '24 05:05 easychen