Skip to content

Commit 2dc7e93

Browse files
committed
move export_json to popup due to manifest v3 limit
1 parent 44beec5 commit 2dc7e93

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

src/assets/download.svg

Lines changed: 2 additions & 0 deletions
Loading

src/background/index.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import colors from 'tailwindcss/colors'
22
import { setBadge, setBadgeBackground } from '../utils/badge'
3-
import { save2Json } from '../utils/file'
43
import { getValue, setValue } from '../utils/storage'
54
import { createTab, getCurrentWindowTabsInfo } from '../utils/tabs'
65
import devDB from './devdb'
@@ -99,20 +98,8 @@ if (!isProd) {
9998
})
10099
}
101100

102-
chrome.contextMenus.create(
103-
{
104-
id: 'export_json',
105-
title: 'Export JSON',
106-
contexts: ['action'],
107-
}
108-
)
109-
110101
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
111102
switch (info.menuItemId) {
112-
case 'export_json':
113-
const data = await getValue()
114-
save2Json(data)
115-
break
116103
case 'open_debug_tab':
117104
const b = import.meta.env.VITE_BROWSER
118105
const url = chrome.runtime.getURL(

src/popup/Popup.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useState } from 'react'
22
import colors from 'tailwindcss/colors'
3+
import downloadIcon from '../assets/download.svg'
34
import emptyIcon from '../assets/empty.svg'
45
import groupsIcon from '../assets/workspaces.svg'
56
import { messages } from '../background/message'
@@ -8,6 +9,7 @@ import Tab from '../components/Tab'
89
import Groups from '../groups/Groups'
910
import '../tailwind.css'
1011
import { setBadge, setBadgeBackground } from '../utils/badge'
12+
import { save2Json } from '../utils/file'
1113
import { getValue, setValue } from '../utils/storage'
1214

1315
const setStorageAndUpdateBadge = (newTabs) => {
@@ -23,6 +25,10 @@ const setStorageAndUpdateBadge = (newTabs) => {
2325
})
2426
}
2527

28+
const exportJson = async () => {
29+
save2Json(await getValue(null, {}))
30+
}
31+
2632
const getReadLaterDatabase = async () => {
2733
const storage = await getValue('read_later', [])
2834
return storage
@@ -83,6 +89,14 @@ export function Popup() {
8389
<div className="flex flex-row items-center h-8 gap-1">
8490
<SearchBar {...{ query, setQuery }} />
8591
<button
92+
title="Export Data"
93+
onClick={exportJson}
94+
className="flex items-center justify-center w-8 h-8 p-1 border-2 border-blue-500 rounded hover:bg-blue-200"
95+
>
96+
<img src={downloadIcon} alt="Download" />
97+
</button>
98+
<button
99+
title="Groups"
86100
onClick={() => setShowsGroups(!showsGroups)}
87101
className="flex items-center justify-center w-8 h-8 p-1 border-2 border-blue-500 rounded hover:bg-blue-200"
88102
>

src/utils/file.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
*/
44
export function save2Json(data) {
55
if (data) {
6-
// not working for v3
7-
// const blob = new Blob([JSON.stringify(data)], { type: 'text/plain' })
8-
// const url = URL.createObjectURL(blob)
6+
// for v3, this is not working for service worker context
7+
const blob = new Blob([JSON.stringify(data)], { type: 'text/plain' })
8+
const url = URL.createObjectURL(blob)
99

10-
const url = `data:,${JSON.stringify(data, null, 2)}`
11-
chrome.downloads.download({
12-
url: url,
13-
filename: `read_later_${new Date().toDateString().replaceAll(' ', '_')}.json`,
14-
saveAs: true,
15-
})
10+
const link = document.createElement('a')
11+
link.href = url
12+
link.download = `read_later_${new Date().toDateString().replaceAll(' ', '_')}.json`
13+
link.click()
14+
link.remove()
1615
}
1716
}

0 commit comments

Comments
 (0)