Skip to content

Commit 6aa3a3a

Browse files
authored
Restore json global for devtools console (#291)
1 parent 7edf149 commit 6aa3a3a

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ Chrome extension that auto-formats JSON when you view it in a browser tab.
1212
- Negligible performance impact on non-JSON pages (less than 1 millisecond)
1313
- Works on any valid JSON page – URL doesn't matter
1414
- Buttons for toggling between raw and parsed JSON
15-
- ~~Parsed JSON is exported as a global variable, `json`, so you can inspect it in the console~~*
16-
17-
> *Typing `json` in the console is not working since Manifest v3. If you need a workaround, paste this snippet into the console:
18-
>
19-
> ```js
20-
> json = JSON.parse(document.getElementById("jsonFormatterRaw").querySelector("pre").innerText)
21-
> ```
15+
- Parsed JSON is exported as a global variable, `json`, so you can inspect it in the console (now working again!)
2216

2317
**Some JSON documents for testing it on:**
2418
https://callumlocke.github.io/json-formatter/

src/content.entry.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ const css = `body {
1919
}
2020
#optionBar {
2121
user-select: none;
22-
display: block;
2322
position: absolute;
24-
top: 13px;
25-
right: 15px;
23+
z-index: 10;
24+
top: 8px;
25+
right: 10px;
26+
background: #fff;
27+
box-shadow: 0px 0px 3px 3px #fff;
28+
padding: 5px;
2629
}
2730
#buttonFormatted,
2831
#buttonPlain {
@@ -78,6 +81,7 @@ const css = `body {
7881
padding-left: 20px;
7982
margin-left: -20px;
8083
position: relative;
84+
content-visibility: auto;
8185
}
8286
#jsonFormatterParsed {
8387
padding-left: 28px;
@@ -226,6 +230,9 @@ a:active {
226230
227231
#optionBar {
228232
-webkit-font-smoothing: subpixel-antialiased;
233+
234+
background: #1a1a1a;
235+
box-shadow: 0px 0px 3px 3px #1a1a1a;
229236
}
230237
231238
#jsonFormatterParsed {
@@ -466,17 +473,6 @@ const resultPromise = (async (): Promise<{
466473
const rootEntry = buildDom(parsedJsonRootStruct, false)
467474
await Promise.resolve()
468475
parsedJsonContainer.append(rootEntry)
469-
470-
// Export parsed JSON for easy access in console - DISABLED; doesn't work with manifest v3 - maybe re-enable later via background worker somehow
471-
// @ts-ignore
472-
// window.json = parsedJsonValue
473-
// Object.defineProperty(window, 'json', {
474-
// value: parsedJsonValue,
475-
// configurable: true,
476-
// enumerable: false, // keep it tidy in console auto-complete
477-
// writable: false,
478-
// })
479-
// console.log('JSON Formatter: Type "json" to inspect.')
480476
}
481477

482478
// hide the pretty-print bar
@@ -493,28 +489,9 @@ const resultPromise = (async (): Promise<{
493489
}
494490

495491
function collapse(elements: HTMLElement[] | HTMLCollection) {
496-
let el, i, blockInner
497-
498-
for (i = elements.length - 1; i >= 0; i--) {
499-
el = elements[i]
500-
el.classList.add('collapsed')
501-
502-
// (CSS hides the contents and shows an ellipsis.)
503-
504-
// Add a count of the number of child properties/items
505-
// if (!el.id) {
506-
// // TODO why is this id check needed?
507-
// // Find the blockInner
508-
// blockInner = el.firstElementChild
509-
// while (blockInner && !blockInner.classList.contains('blockInner')) {
510-
// blockInner = blockInner.nextElementSibling
511-
// }
512-
// if (!blockInner) continue // ???
513-
// // ??? this continue has no effect, as the for-loop conitinues after this anyway, right?
514-
// // >>> so what is the point of this entire `if (!el.id)` block?
515-
// // original comment says "Add a count of the number of child properties/items"
516-
// // but that feature seems to be working fine, despite this block doing nothing
517-
// }
492+
for (let i = elements.length - 1; i >= 0; i--) {
493+
const el = elements[i]
494+
el.classList.add('collapsed') // hides contents and shows an ellipsis
518495
}
519496
}
520497

src/manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"matches": ["<all_urls>"],
1515
"js": ["content.js"],
1616
"run_at": "document_end"
17+
},
18+
{
19+
"matches": ["<all_urls>"],
20+
"js": ["set-json-global.js"],
21+
"run_at": "document_idle",
22+
"world": "MAIN"
1723
}
1824
],
1925
"permissions": ["storage"],

src/set-json-global.entry.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* This extra content script exposes the parsed JSON data as a global variable `json` for easy inspection in the devtools console. Runs after the main content has loaded and parsed the JSON. Exits fast on non-JSON pages.
3+
*/
4+
;(() => {
5+
const pre = document.getElementById('jsonFormatterRaw')?.querySelector('pre')
6+
if (!pre) return // not a formatted JSON page
7+
8+
// timeout to give time for the UI to settle
9+
setTimeout(() => {
10+
try {
11+
const data = JSON.parse(pre.innerText)
12+
Object.defineProperty(window, 'json', {
13+
value: data,
14+
configurable: true,
15+
enumerable: false,
16+
writable: false,
17+
})
18+
19+
console.log('JSON Formatter: Type "json" to inspect')
20+
} catch (error) {
21+
console.error('JSON Formatter: Failed to expose JSON global', error)
22+
}
23+
}, 120)
24+
})()

0 commit comments

Comments
 (0)