Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Commit 69f06c2

Browse files
committed
Show script size and alert if reach chrome sync limit
1 parent ae6590c commit 69f06c2

6 files changed

Lines changed: 92 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"material-ui": "^1.0.0-beta.18",
2020
"mobx": "^3.3.3",
2121
"mobx-react": "^4.3.5",
22+
"object-sizeof": "^1.2.0",
2223
"query-string": "^5.0.1",
2324
"react": "^16.2.0",
2425
"react-ace": "^5.8.0",

src/js/background.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ const methodMap = {
2020
sendResponse({ customjs, host, protocol, tab, matchedHost })
2121
}
2222
},
23-
setData: (message) => {
23+
setData: async (message, _, sendResponse) => {
2424
const { matchedHost, customjs } = message
2525
const hostKey = getHostKey(matchedHost)
26-
chrome.storage.sync.set({ [hostKey]: customjs })
26+
try {
27+
await chrome.storage.sync.set({ [hostKey]: customjs })
28+
sendResponse()
29+
} catch (err) {
30+
sendResponse(err)
31+
}
2732
},
2833
removeData: (message, { url }) => {
2934
const { isRegex, pattern } = message

src/js/components/Page.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Toggle from 'components/Toggle'
1111
import Include from 'components/Include'
1212
import Reset from 'components/Reset'
1313
import Save from 'components/Save'
14+
import Size from 'components/Size'
1415
import NewPattern from 'components/NewPattern'
1516
import queryString from 'query-string'
1617
import NewTabLink from './NewTabLink'
@@ -32,7 +33,6 @@ export default class Page extends Component {
3233

3334
onSave = () => {
3435
this.props.AppStore.save()
35-
this.closePopup()
3636
}
3737

3838
onSelectHost = (e) => {
@@ -100,8 +100,11 @@ export default class Page extends Component {
100100
</div>
101101
<Editor />
102102
<div style={toolbarStyle}>
103-
<RemoveDraft />
104-
<AutoSave />
103+
<span>
104+
<RemoveDraft />
105+
<AutoSave />
106+
</span>
107+
<Size />
105108
<DonateLink />
106109
</div>
107110
<div id='error' className='error is-hidden'>

src/js/components/Size.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, { Component } from 'react'
2+
import { inject, observer } from 'mobx-react'
3+
import Button from 'material-ui/Button'
4+
import Dialog, {
5+
DialogActions,
6+
DialogContent,
7+
DialogContentText,
8+
DialogTitle
9+
} from 'material-ui/Dialog'
10+
11+
const issueLink = 'https://github.com/xcv58/Custom-JavaScript-for-Websites-2/issues/32'
12+
const chromeDocLink = 'https://developer.chrome.com/apps/storage#property-sync'
13+
14+
@inject('AppStore')
15+
@observer
16+
export default class Size extends Component {
17+
handleClose = () => {
18+
window.location.reload()
19+
}
20+
21+
render () {
22+
const { saveError, size } = this.props.AppStore
23+
const alert = saveError && (
24+
<Dialog open onClose={this.handleClose}>
25+
<DialogTitle>Script Save Failure</DialogTitle>
26+
<DialogContent>
27+
<DialogContentText>
28+
Failed to save your script, it's usually because your script is
29+
too large to store in <a href={chromeDocLink} target='_blank' rel='noopener noreferrer'>
30+
chrome.storage.sync
31+
</a>. Please reduce your script size and try again!
32+
</DialogContentText>
33+
<DialogContentText>
34+
We know this problem and try to fix it, please follow <a
35+
href={issueLink} target='_blank' rel='noopener noreferrer'>
36+
this issue
37+
</a> if you want.
38+
</DialogContentText>
39+
</DialogContent>
40+
<DialogActions>
41+
<Button onClick={this.handleClose} color='primary' autoFocus>
42+
Refresh
43+
</Button>
44+
</DialogActions>
45+
</Dialog>
46+
)
47+
return (
48+
<span>{size} bytes{alert}</span>
49+
)
50+
}
51+
}

src/js/stores/AppStore.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { action, computed, observable } from 'mobx'
22
import { encodeSource, decodeSource, getHosts, setHosts } from 'libs'
33
import isEqual from 'lodash.isequal'
4+
import sizeof from 'object-sizeof'
45

56
const key = 'popup'
67
const defaultSource = '// Here You can type your custom JavaScript...'
@@ -29,6 +30,7 @@ export default class AppStore {
2930
@observable matchedHost = ''
3031

3132
@observable error = null
33+
@observable saveError = null
3234

3335
@computed
3436
get include () {
@@ -67,6 +69,11 @@ export default class AppStore {
6769
}
6870
}
6971

72+
@computed
73+
get size () {
74+
return sizeof(this.source)
75+
}
76+
7077
@computed
7178
get domainKey () {
7279
if (this.matchedHost && this.matchedHost.isRegex) {
@@ -177,14 +184,19 @@ export default class AppStore {
177184

178185
@action
179186
save = () => {
180-
this.removeDraft()
181187
const { domain, customjs, matchedHost } = this
182188
chrome.runtime.sendMessage({
183189
method: 'setData',
184190
domain,
185191
matchedHost,
186192
customjs,
187-
reload: true
193+
reload: !this.tabMode
194+
}, (err) => {
195+
if (err) {
196+
this.saveError = err
197+
} else {
198+
this.removeDraft()
199+
}
188200
})
189201
}
190202

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,13 @@ buffer@^4.3.0:
11481148
ieee754 "^1.1.4"
11491149
isarray "^1.0.0"
11501150

1151+
buffer@^5.0.6:
1152+
version "5.0.8"
1153+
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.0.8.tgz#84daa52e7cf2fa8ce4195bc5cf0f7809e0930b24"
1154+
dependencies:
1155+
base64-js "^1.0.2"
1156+
ieee754 "^1.1.4"
1157+
11511158
builtin-modules@^1.0.0, builtin-modules@^1.1.1:
11521159
version "1.1.1"
11531160
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
@@ -3983,6 +3990,12 @@ object-keys@^1.0.10, object-keys@^1.0.8:
39833990
version "1.0.11"
39843991
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d"
39853992

3993+
object-sizeof@^1.2.0:
3994+
version "1.2.0"
3995+
resolved "https://registry.yarnpkg.com/object-sizeof/-/object-sizeof-1.2.0.tgz#66397c923768939d2fc6d2a3930a981e1c7fa68b"
3996+
dependencies:
3997+
buffer "^5.0.6"
3998+
39863999
object.assign@^4.0.4:
39874000
version "4.0.4"
39884001
resolved "https://registry.yarnpkg.com/object.assign/-/object.assign-4.0.4.tgz#b1c9cc044ef1b9fe63606fc141abbb32e14730cc"

0 commit comments

Comments
 (0)