Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

master #31 to firefox (Fix: Improve Firefox compat in save/setClipboard) #32

Merged
merged 3 commits into from
Apr 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Get cookies.txt LOCALLY",
"description": "Get cookies.txt, NEVER send information outside with open-source",
"version": "0.4.2",
"version": "0.4.3",
"manifest_version": 3,
"permissions": [
"activeTab",
Expand Down
25 changes: 15 additions & 10 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,29 @@ const FormatMap = {
* @param {Format} format
* @param {boolean} saveAs
*/
const save = async (text, name, { ext, mimeType }, saveAs=false) => {
const url = `data:${mimeType},${encodeURIComponent(text)}`;
const save = async (text, name, { ext, mimeType }, saveAs = false) => {
const blob = new Blob([text], { 'type': mimeType });
const filename = name + ext;
await chrome.downloads.download({url, filename, saveAs});
const url = URL.createObjectURL(blob);
const downloadId = await chrome.downloads.download({ url, filename, saveAs });
/** @type {(downloadDelta: chrome.downloads.DownloadDelta) => void} */
const callback = delta => {
const { id, state, error } = delta;
if (id === downloadId && (state?.current === 'complete' || error)) {
URL.revokeObjectURL(url);
chrome.downloads.onChanged.removeListener(callback);
}
}
chrome.downloads.onChanged.addListener(callback);
}

/**
* Copy text data to the clipboard
* @param {string} text
*/
const setClipboard = async (text) => {
const type = 'text/plain';
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data)
.then(() => {
document.getElementById('copy').innerText = 'Copied!';
})
await navigator.clipboard.writeText(text);
document.getElementById('copy').innerText = 'Copied!';
}

/**
Expand Down