Skip to content

Commit f071806

Browse files
committed
Start transitioning to manifest v3
1 parent 71b6ffc commit f071806

File tree

9 files changed

+157
-78
lines changed

9 files changed

+157
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
.tern-port
44
/ts-out
55
/build
6+
/build-*
67
/node_modules
78

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"jest.jestCommandLine": "yarn test",
1313
"[javascript]": {
1414
"editor.codeActionsOnSave": {
15-
"source.organizeImports": false
15+
"source.organizeImports": "never"
1616
}
1717
},
1818
"[typescript]": {
1919
"editor.codeActionsOnSave": {
20-
"source.organizeImports": false
20+
"source.organizeImports": "never"
2121
}
2222
},
2323
"[javascript][typescript]": {

README.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1-
JSONView
2-
========
1+
# JSONView
32

43
[JSONView](http://jsonview.com) is a Web extension compatible with Firefox and Google Chrome that helps you view JSON documents in the browser.
54

6-
* [Install for Firefox](https://addons.mozilla.org/en-US/firefox/addon/jsonview/)
7-
* [Install for Chrome](https://chrome.google.com/webstore/detail/jsonview/gmegofmjomhknnokphhckolhcffdaihd)
8-
* [Install for Edge](https://microsoftedge.microsoft.com/addons/detail/jsonview/kmpfgkgaimakokfhgdahhiaaiidiphco)
5+
- [Install for Firefox](https://addons.mozilla.org/en-US/firefox/addon/jsonview/)
6+
- [Install for Chrome](https://chrome.google.com/webstore/detail/jsonview/gmegofmjomhknnokphhckolhcffdaihd)
7+
- [Install for Edge](https://microsoftedge.microsoft.com/addons/detail/jsonview/kmpfgkgaimakokfhgdahhiaaiidiphco)
98

109
Normally, when encountering a [JSON](http://json.org) document (content type `application/json`), Firefox simply prompts you to download the view. With the JSONView extension, JSON documents are shown in the browser similar to how XML documents are shown. The document is formatted, highlighted, and arrays and objects can be collapsed. Even if the JSON document contains errors, JSONView will still show the raw text.
1110

1211
Once you've got JSONView installed, check out [this example JSON file](http://jsonview.com/example.json) to see the extension in action!
1312

1413
If you'd like to contribute to JSONView but don't want to code, consider contributing a translation. Copy the existing localization files from `src/_locale` and fill them in for your own language, then send a pull request. You can do it all from the GitHub interface. There are not many strings to translate!
1514

16-
Keyboard Shortcuts
17-
----------------
15+
## Keyboard Shortcuts
1816

19-
* Left Arrow - Collapses the json on key up
20-
* Right Arrow - Expands the json on key up
17+
- Left Arrow - Collapses the json on key up
18+
- Right Arrow - Expands the json on key up
2119

22-
Reporting Issues
23-
----------------
20+
## Reporting Issues
2421

2522
Use the GitHub [Issue tracker for JSONView](https://github.com/bhollis/jsonview/issues) to file issues. Pull requests are especially welcome.
2623

27-
Developing JSONView
28-
-------------------
24+
## Developing JSONView
2925

3026
Before contributing to JSONView, make sure to read the [Contributing Guidelines](CONTRIBUTING.md). I appreciate contributions people make to JSONView, but the goal of the extension is to be simple and straightforward, so I frequently reject contributions that add complexity or unnecessary features. Please consider filing an issue before doing any work, so you don't waste time on something I won't accept.
3127

32-
* Install [NodeJS](https://nodejs.org/en/) and [Yarn](https://yarnpkg.com/en/docs/install).
33-
* Check out jsonview.
34-
* Run `yarn` inside the jsonview repository.
35-
* Run `yarn start` to build the extension.
36-
* In Firefox, go to `about:debugging#addons` in the address bar, check "Enable add-on debugging", select "Load Temporary Add-on", and choose the `jsonview/build` folder.
37-
* In Chrome, go to `chrome://extensions/` in the address bar, select "Load Unpacked", and choose the `jsonview/build` folder.
28+
- Install [NodeJS](https://nodejs.org/en/) and [Yarn](https://yarnpkg.com/en/docs/install).
29+
- Check out jsonview.
30+
- Run `yarn` inside the jsonview repository.
31+
- Run `yarn start` to build the extension.
32+
- In Firefox, go to `about:debugging#addons` in the address bar, check "Enable add-on debugging", select "Load Temporary Add-on", and choose the `jsonview/build-firefox` folder.
33+
- In Chrome, go to `chrome://extensions/` in the address bar, select "Load Unpacked", and choose the `jsonview/build-chrome` folder.
3834

3935
JSONView makes use of [TypeScript](https://www.typescriptlang.org/). I recommend [VSCode](https://code.visualstudio.com/) for editing the code - it will automatically prompt to install the correct extensions, and will highlight errors. All of the code that makes up the extension itself are in `src/`.
4036

41-
Common Issues
42-
-------------
43-
* **JSONView isn't displaying my file as JSON**: You are probably not serving
37+
## Common Issues
38+
39+
- **JSONView isn't displaying my file as JSON**: You are probably not serving
4440
the JSON with the "application/json" MIME type.
45-
* **Opening a local .json file uses the Firefox default JSON viewer**: You need to disable the built-in JSON viewer to use JSONView. Go to "about:config" and set "devtools.jsonview.enabled" to "false".
41+
- **Opening a local .json file uses the Firefox default JSON viewer**: You need to disable the built-in JSON viewer to use JSONView. Go to "about:config" and set "devtools.jsonview.enabled" to "false".
4642

4743
JSONView is open source software under the MIT license.
4844

49-
Publishing
50-
----------
45+
## Publishing
5146

5247
```
5348
npm start

build.sh

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,39 @@ tsc
66

77
node --test
88

9-
rm -rf build
10-
mkdir -p build
11-
12-
rollup ts-out/background.js --format iife --name 'background' --file build/background.js
13-
rollup ts-out/content.js --format iife --name 'background' --file build/content.js
14-
rollup ts-out/viewer.js --format iife --name 'background' --file build/viewer.js
15-
cp src/viewer.css build/viewer.css
16-
cp src/manifest.json build/manifest.json
17-
cp license.txt build/license.txt
18-
cp -r src/_locales build
19-
cp src/icon*.png build
20-
21-
rm -f jsonview.zip
22-
pushd build
23-
zip -r ../jsonview.zip *
9+
# Chrome
10+
rm -rf build-chrome
11+
mkdir -p build-chrome
12+
13+
rollup ts-out/background.js --format iife --name 'background' --file build-chrome/background.js
14+
rollup ts-out/content.js --format iife --name 'background' --file build-chrome/content.js
15+
rollup ts-out/viewer.js --format iife --name 'background' --file build-chrome/viewer.js
16+
cp src/viewer.css build-chrome/viewer.css
17+
cp src/manifest.chrome.json build-chrome/manifest.json
18+
cp license.txt build-chrome/license.txt
19+
cp -r src/_locales build-chrome
20+
cp src/icon*.png build-chrome
21+
22+
rm -f jsonview-chrome.zip
23+
pushd build-chrome
24+
zip -r ../jsonview-chrome.zip *
2425
popd
26+
27+
28+
# Firefox
29+
rm -rf build-firefox
30+
mkdir -p build-firefox
31+
32+
rollup ts-out/background.js --format iife --name 'background' --file build-firefox/background.js
33+
rollup ts-out/content.js --format iife --name 'background' --file build-firefox/content.js
34+
rollup ts-out/viewer.js --format iife --name 'background' --file build-firefox/viewer.js
35+
cp src/viewer.css build-firefox/viewer.css
36+
cp src/manifest.firefox.json build-firefox/manifest.json
37+
cp license.txt build-firefox/license.txt
38+
cp -r src/_locales build-firefox
39+
cp src/icon*.png build-firefox
40+
41+
rm -f jsonview-firefox.zip
42+
pushd build-firefox
43+
zip -r ../jsonview-firefox.zip *
44+
popd

buildWindows.ps1

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@
22

33
tsc
44

5-
rm -r -fo ./build
6-
mkdir -p build
7-
8-
yarn rollup ts-out/background.js --format iife --name 'background' --file build/background.js
9-
yarn rollup ts-out/content.js --format iife --name 'background' --file build/content.js
10-
yarn rollup ts-out/viewer.js --format iife --name 'background' --file build/viewer.js
11-
cp src/viewer.css build/viewer.css
12-
cp src/manifest.json build/manifest.json
13-
cp license.txt build/license.txt
14-
cp -r src/_locales build
15-
cp src/icon*.png build
5+
rm -r -fo ./build-chrome
6+
mkdir -p build-chrome
7+
8+
yarn rollup ts-out/background.js --format iife --name 'background' --file build-chrome/background.js
9+
yarn rollup ts-out/content.js --format iife --name 'background' --file build-chrome/content.js
10+
yarn rollup ts-out/viewer.js --format iife --name 'background' --file build-chrome/viewer.js
11+
cp src/viewer.css build-chrome/viewer.css
12+
cp src/manifest.json build-chrome/manifest.json
13+
cp license.txt build-chrome/license.txt
14+
cp -r src/_locales build-chrome
15+
cp src/icon*.png build-chrome
16+
17+
rm -fo ./jsonview.zip
18+
pushd build-chrome
19+
Compress-Archive -Path ./* -DestinationPath ../jsonview.zip
20+
popd
21+
22+
23+
rm -r -fo ./build-firefox
24+
mkdir -p build-firefox
25+
26+
yarn rollup ts-out/background.js --format iife --name 'background' --file build-firefox/background.js
27+
yarn rollup ts-out/content.js --format iife --name 'background' --file build-firefox/content.js
28+
yarn rollup ts-out/viewer.js --format iife --name 'background' --file build-firefox/viewer.js
29+
cp src/viewer.css build-firefox/viewer.css
30+
cp src/manifest.json build-firefox/manifest.json
31+
cp license.txt build-firefox/license.txt
32+
cp -r src/_locales build-firefox
33+
cp src/icon*.png build-firefox
1634

1735
rm -fo ./jsonview.zip
18-
pushd build
36+
pushd build-firefox
1937
Compress-Archive -Path ./* -DestinationPath ../jsonview.zip
2038
popd

src/background.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
import { isJSONContentType } from "./content-type";
1010

11-
// Keep track globally of URLs that contain JSON content.
12-
const jsonUrls = new Set<string>();
13-
1411
function isRedirect(status: number) {
1512
return status >= 300 && status < 400;
1613
}
@@ -25,7 +22,7 @@ function detectJSON(event: chrome.webRequest.WebResponseHeadersDetails) {
2522
header.value &&
2623
isJSONContentType(header.value)
2724
) {
28-
jsonUrls.add(event.url);
25+
addJsonUrl(event.url);
2926
if (typeof browser !== "undefined" && "filterResponseData" in browser.webRequest) {
3027
header.value = "text/plain";
3128
}
@@ -47,12 +44,26 @@ chrome.webRequest.onHeadersReceived.addListener(
4744
// the page. Calls sendResponse with a boolean that's true if the content script
4845
// should run, and false otherwise.
4946
chrome.runtime.onMessage.addListener((_message, sender, sendResponse) => {
50-
if (sender.url?.startsWith("file://") && sender.url.endsWith(".json")) {
51-
sendResponse(true);
47+
if (!sender.url) {
48+
sendResponse(false);
5249
return;
5350
}
54-
sendResponse(sender.url && jsonUrls.has(sender.url));
55-
if (sender.url) {
56-
jsonUrls.delete(sender.url);
51+
52+
if (sender.url.startsWith("file://") && sender.url.endsWith(".json")) {
53+
sendResponse(true);
54+
return;
5755
}
56+
const isKnownJsonUrl = hasJsonUrl(sender.url);
57+
sendResponse(isKnownJsonUrl);
5858
});
59+
60+
async function addJsonUrl(url: string) {
61+
await chrome.storage.session.set({ [url]: true });
62+
}
63+
64+
async function hasJsonUrl(url: string) {
65+
const stored = await chrome.storage.session.get(url);
66+
const present = url in stored;
67+
await chrome.storage.session.remove(url);
68+
return present;
69+
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"manifest_version": 2,
2+
"manifest_version": 3,
33
"name": "JSONView",
4-
"version": "2.6.1",
4+
"version": "3.0.0",
55
"description": "__MSG_extensionDescription__",
66
"author": "Benjamin Hollis",
77
"homepage_url": "https://jsonview.com/",
@@ -13,20 +13,22 @@
1313
"256": "icon256.png"
1414
},
1515
"background": {
16-
"scripts": ["background.js"]
16+
"service_worker": "background.js",
17+
"type": "module"
1718
},
1819
"content_scripts": [
1920
{
2021
"matches": ["<all_urls>"],
2122
"js": ["content.js"]
2223
}
2324
],
24-
"permissions": ["<all_urls>", "webRequest", "webRequestBlocking"],
25-
"applications": {
26-
"gecko": {
27-
"id": "jsonview@brh.numbera.com"
25+
"permissions": ["webRequest"],
26+
"host_permissions": ["<all_urls>"],
27+
"web_accessible_resources": [
28+
{
29+
"resources": ["viewer.css", "viewer.js"],
30+
"matches": ["*://*/*"]
2831
}
29-
},
30-
"web_accessible_resources": ["viewer.css", "viewer.js"],
32+
],
3133
"default_locale": "en"
3234
}

src/manifest.firefox.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "JSONView",
4+
"version": "3.0.0",
5+
"description": "__MSG_extensionDescription__",
6+
"author": "Benjamin Hollis",
7+
"homepage_url": "https://jsonview.com/",
8+
"icons": {
9+
"32": "icon.png",
10+
"48": "icon48.png",
11+
"64": "icon64.png",
12+
"128": "icon128.png",
13+
"256": "icon256.png"
14+
},
15+
"background": {
16+
"scripts": ["background-script.js"],
17+
"type": "module"
18+
},
19+
"content_scripts": [
20+
{
21+
"matches": ["<all_urls>"],
22+
"js": ["content.js"]
23+
}
24+
],
25+
"permissions": ["webRequest"],
26+
"host_permissions": ["<all_urls>"],
27+
"web_accessible_resources": [
28+
{
29+
"resources": ["viewer.css", "viewer.js"],
30+
"matches": ["*://*/*"]
31+
}
32+
],
33+
"default_locale": "en"
34+
}

tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
"sourceMap": true,
66
"strictNullChecks": true,
77
"noImplicitAny": true,
8-
"module": "ESNext",
9-
"target": "es6",
10-
"moduleResolution": "node",
8+
"module": "ES2020",
9+
"target": "ES2020",
10+
"moduleResolution": "bundler",
1111
"noUnusedLocals": true,
1212
"noUnusedParameters": true,
1313
"forceConsistentCasingInFileNames": true,
1414
"allowJs": true,
1515
"checkJs": true,
1616
"typeRoots": ["node_modules/@types", "node_modules/web-ext-types"]
1717
},
18-
"include": [
19-
"./src/*.ts"
20-
]
18+
"include": ["./src/*.ts"]
2119
}

0 commit comments

Comments
 (0)