From 360c61dc6572e6cc5ec2c6ef2b658a4f7fba057d Mon Sep 17 00:00:00 2001 From: shepherdwin Date: Thu, 29 Feb 2024 14:36:30 +0800 Subject: [PATCH] feat: Improve cookie handling in popup window - Update popup functionality to allow for copying cookies to clipboard - Initialize the default domain value to the current tab URL - Update cookie setting to include secure and httpOnly flags --- popup.html | 3 ++- popup.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/popup.html b/popup.html index d790cae..ea29a3a 100644 --- a/popup.html +++ b/popup.html @@ -132,7 +132,7 @@ color: #756e6e; } - + v2.0.1 @@ -152,6 +152,7 @@
+
diff --git a/popup.js b/popup.js index 0127537..2f335b0 100644 --- a/popup.js +++ b/popup.js @@ -1,3 +1,21 @@ +const input = document.getElementById('domainInput'); + +(async function initPopupWindow() { + let [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); + + if (tab?.url) { + try { + let url = new URL(tab.url); + input.value = url.hostname; + } catch(e) { + alert(e); + // ignore + } + } + + input.focus(); +})(); + // When the popup Paste Button is clicked const onCopyButtonClick = () => { chrome.tabs.query( @@ -15,6 +33,32 @@ const onCopyButtonClick = () => { ); }; +const onCopyClipboardButtonClick = (e) => { + chrome.tabs.query( + { + status: 'complete', + windowId: chrome.windows.WINDOW_ID_CURRENT, + active: true, + }, + tab => { + chrome.cookies.getAll({ url: tab[0].url }, cookie => { + const cookieString = cookie.map(o => `${o.name}=${o.value}`).join(';'); + const el = document.createElement('textarea') + el.value = cookieString; + document.body.append(el); + // Select the text and copy to clipboard + el.select(); + document.execCommand('copy', true); + el.remove(); + + const text = e.target.innerText; + e.target.innerText = 'Success'; + setTimeout(() => e.target.innerText = text, 1000); + }); + }, + ); +}; + const removeOldCookies = (cookies, index, url, callback) => { if (!cookies[index]) return callback(); @@ -42,7 +86,7 @@ const onPasteButtonClick = async () => { if (!copyCookieData) return alert('Uh-Oh! You need to copy the cookies first.'); - let domain = document.getElementById('domainInput').value.trim(); + let domain = input.value.trim(); if (!domain) domain = 'localhost'; chrome.tabs.query( @@ -58,13 +102,15 @@ const onPasteButtonClick = async () => { chrome.cookies.getAll({ url: tab[0].url }, cookies => { removeOldCookies(cookies, 0, tab[0].url, () => { - copyCookieData.forEach(({ name, value, path }) => { + copyCookieData.forEach(({ name, value, path, httpOnly, secure }) => { try { chrome.cookies.set({ url: tab[0].url, + secure, name, value, path, + httpOnly, domain, }); } catch (error) { @@ -116,6 +162,9 @@ window.addEventListener('load', () => { document .getElementById('copyButton') .addEventListener('click', onCopyButtonClick); + document + .getElementById('copyToClipboard') + .addEventListener('click', onCopyClipboardButtonClick); document .getElementById('pasteButton') .addEventListener('click', onPasteButtonClick);