Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
color: #756e6e;
}
</style>
<script src="popup.js"></script>
<script src="popup.js" type="module"></script>
</head>
<body>
<span class="versionLabel">v2.0.1</span>
Expand All @@ -152,6 +152,7 @@
</div>
<div class="buttonWrapper">
<button id="copyButton">Copy</button>
<button id="copyToClipboard">To Clipboard</button>
</div>
</div>
<div class="formWrapper2">
Expand Down
53 changes: 51 additions & 2 deletions popup.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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();

Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down