From 10dd4852b34e4653ab1df98506e08b7df502c927 Mon Sep 17 00:00:00 2001 From: Melvin Carvalho Date: Mon, 30 Nov 2026 16:33:50 +0100 Subject: [PATCH] json viewer: Open in SolidOS link, naming the thing the document is about; viewer 0.0.2 (#50) --- CHANGELOG.md | 13 ++++++ PRIVACY.md | 5 +++ README.md | 8 +++- scripts/vendor-json-viewer.js | 2 +- src/json-viewer-start.js | 7 +++- test/json-viewer.test.js | 2 +- vendor/json-viewer/PINS.json | 4 +- vendor/json-viewer/json-viewer.js | 68 +++++++++++++++++++++++++++---- 8 files changed, 93 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a35c9db..d566826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **"Open in SolidOS" in the JSON viewer.** A small link beside the Viewer / Raw + switch opens the same address in the SolidOS data browser, where panes can + show it. For a JSON-LD document that is about one thing (its main entity, or + a `#me` or `#this`), the link names that thing rather than the document, so + SolidOS opens the matching pane. It is only a link: nothing is requested + unless you click it. (#50) + +### Changed + +- Bundled JSON viewer updated to 0.0.2 (`vendor/json-viewer/`, `PINS.json`). + ## [0.0.15] - 2026-11-29 ### Added diff --git a/PRIVACY.md b/PRIVACY.md index b2bd171..5faa0d2 100644 --- a/PRIVACY.md +++ b/PRIVACY.md @@ -93,6 +93,11 @@ already loaded, on your device. It makes no network request, sends nothing anywhere, stores nothing, and has no access to your key. The only thing kept is the setting itself (on or off), in local storage. +The viewer shows an "Open in SolidOS" link. It is an ordinary link: nothing +happens unless you click it. If you do, a new tab opens the SolidOS data +browser at solidos.org with the address of the page you were viewing, so that +site sees that address, as any site you open a link to would. + ## Permissions, and why they are needed - **`storage`**: to keep the encrypted key, your public key, your trusted and diff --git a/README.md b/README.md index 4f13627..f9bf80a 100644 --- a/README.md +++ b/README.md @@ -167,12 +167,16 @@ chain's signer and its saved state. Turning spends off forgets every chain. Off by default. Switch on **View JSON pages** in the popup's settings and a page that is a JSON, JSON-LD or JSON Lines response is shown as a collapsible tree with a table view, search, type badges and followable `@id` links. A Viewer / -Raw switch in the corner goes back to the page as the browser showed it. +Raw switch in the corner goes back to the page as the browser showed it. Beside +it, **Open in SolidOS** opens the same address in the SolidOS data browser, for +a view with panes; a JSON-LD document about one thing (its main entity, `#me` +or `#this`) is opened on that thing. The viewer is one vendored file (`vendor/json-viewer/json-viewer.js`, pinned in `PINS.json`; update with `node scripts/vendor-json-viewer.js`). It is registered as a content script only while the setting is on, acts only on JSON pages, and -makes no network requests: it reads the text the browser already has. It is +makes no network requests: it reads the text the browser already has (the +SolidOS link is an ordinary link, followed only if you click it). It is separate from Podkey's signing: it never talks to the key, the worker or the bridge. diff --git a/scripts/vendor-json-viewer.js b/scripts/vendor-json-viewer.js index 7437d8c..1a93e88 100644 --- a/scripts/vendor-json-viewer.js +++ b/scripts/vendor-json-viewer.js @@ -24,7 +24,7 @@ import { fileURLToPath } from 'node:url'; const PIN = { name: 'json-viewer', repo: 'https://github.com/solid-panes/json', - commit: '4b7162a52b314cf4502db1129ecb81b88068cacf', + commit: '44c440eb8e137451a460f34372751dd1a3d80a68', files: ['json-viewer.js'] }; diff --git a/src/json-viewer-start.js b/src/json-viewer-start.js index 751a876..d12bacc 100644 --- a/src/json-viewer-start.js +++ b/src/json-viewer-start.js @@ -6,9 +6,14 @@ * switched off for is left out like the other scripts. So being loaded at all * means the setting is on. * + * solidosLink adds an "Open in SolidOS" link beside the Viewer / Raw switch: a + * plain link to the SolidOS data browser with this page's address (or the thing + * the document is about: its main entity, #me or #this). Nothing is requested + * unless the person clicks it. + * * The viewer acts only when the page is a JSON or JSON Lines response shown as * text. It reads no storage, sends no messages and makes no requests, and this * script touches neither chrome.runtime nor the bridge (src/injected.js), which * runs in the same isolated world. */ -globalThis.SolidJsonViewer?.start({ enabled: true }); +globalThis.SolidJsonViewer?.start({ enabled: true, solidosLink: true }); diff --git a/test/json-viewer.test.js b/test/json-viewer.test.js index 921de66..29d8835 100644 --- a/test/json-viewer.test.js +++ b/test/json-viewer.test.js @@ -66,7 +66,7 @@ describe('src/json-viewer-start.js', () => { const sandbox = { SolidJsonViewer: { start: (o) => { calls.push(o); return true; } } }; sandbox.globalThis = sandbox; vm.runInNewContext(src, sandbox); - assert.equal(JSON.stringify(calls), JSON.stringify([{ enabled: true }])); // made in another context, so compared as text + assert.equal(JSON.stringify(calls), JSON.stringify([{ enabled: true, solidosLink: true }])); // made in another context, so compared as text }); it('does not throw where the viewer is absent', () => { diff --git a/vendor/json-viewer/PINS.json b/vendor/json-viewer/PINS.json index bd0c607..ca7e3d2 100644 --- a/vendor/json-viewer/PINS.json +++ b/vendor/json-viewer/PINS.json @@ -4,9 +4,9 @@ { "name": "json-viewer", "repo": "https://github.com/solid-panes/json", - "commit": "4b7162a52b314cf4502db1129ecb81b88068cacf", + "commit": "44c440eb8e137451a460f34372751dd1a3d80a68", "files": { - "json-viewer.js": "96c264366a36268a8668b183ad14641bcf82fb9015aa947cf84d7e6b2972c0d6" + "json-viewer.js": "aa066aab7b00db3a4b0c14945043e1728379259b692f11070b19b3e25e0f9e1c" } } ] diff --git a/vendor/json-viewer/json-viewer.js b/vendor/json-viewer/json-viewer.js index 8f8d91a..9d9ad53 100644 --- a/vendor/json-viewer/json-viewer.js +++ b/vendor/json-viewer/json-viewer.js @@ -3,7 +3,7 @@ // // json-viewer.js — the JSON / JSON-LD viewer as one file. GENERATED by tools/kernel.mjs from // json-pane.js and host/*.js in https://github.com/solid-panes/json : do not edit here. -// Version 0.0.1. +// Version 0.0.2. // // For a browser extension's content script (isolated world), loaded at document_start. It defines // one name, SolidJsonViewer, and does nothing until the carrier calls @@ -3056,7 +3056,54 @@ return pane return n >= 2 } - H.detect = { page, onlyPre, kindOf } + // What a JSON-LD document is about, as a fragment ('#me') of this document, or null. + // In order: the thing declared as its main one, in either direction (the document says + // schema.org mainEntity or FOAF primaryTopic; or a thing says mainEntityOfPage or + // isPrimaryTopicOf, pointing at this document); else a thing whose @id is #me (the convention + // for a person); else one whose @id is #this (for a thing). Only things inside this document + // count: an @id elsewhere is never followed. Looks at the top level and at @graph. + function topic (text) { + if (!/#me\b|#this\b|mainEntity|rimaryTopic/.test(text)) return null // most JSON: no second parse + try { + const here = location.href.split('#')[0] + const local = v => { + const id = typeof v === 'string' ? v : v && typeof v === 'object' && !Array.isArray(v) ? v['@id'] || v.id : null + if (typeof id !== 'string') return null + const frag = id[0] === '#' ? id : id.startsWith(here + '#') ? id.slice(here.length) : null + return frag && /^#[^\s#]+$/.test(frag) ? frag : null + } + const data = JSON.parse(text) + const nodes = [] + for (const top of Array.isArray(data) ? data : [data]) { + if (!top || typeof top !== 'object') continue + nodes.push(top) + if (Array.isArray(top['@graph'])) for (const n of top['@graph']) if (n && typeof n === 'object') nodes.push(n) + } + const MAIN = /^(?:(?:schema:|https?:\/\/schema\.org\/)?mainEntity|(?:foaf:|http:\/\/xmlns\.com\/foaf\/0\.1\/)?primaryTopic)$/ + for (const n of nodes) { + for (const k of Object.keys(n)) { + if (!MAIN.test(k)) continue + for (const v of Array.isArray(n[k]) ? n[k] : [n[k]]) { const f = local(v); if (f) return f } + } + } + // the reverse direction: a thing in this document that says this document is its page + const OF = /^(?:(?:schema:|https?:\/\/schema\.org\/)?mainEntityOfPage|(?:foaf:|http:\/\/xmlns\.com\/foaf\/0\.1\/)?isPrimaryTopicOf)$/ + const thisDoc = v => { + const id = typeof v === 'string' ? v : v && typeof v === 'object' && !Array.isArray(v) ? ('@id' in v ? v['@id'] : v.id) : null // "" means this document + if (typeof id !== 'string' || id.includes('#')) return false + try { return new URL(id, here).href === here } catch (e) { return false } + } + for (const n of nodes) { + const f = local(n) + if (!f) continue + for (const k of Object.keys(n)) if (OF.test(k) && (Array.isArray(n[k]) ? n[k] : [n[k]]).some(thisDoc)) return f + } + const ids = new Set(nodes.map(n => local(n)).filter(Boolean)) + return ids.has('#me') ? '#me' : ids.has('#this') ? '#this' : null + } catch (e) { return null } + } + + H.detect = { page, onlyPre, kindOf, topic } })() // ---- host/shim.js @@ -3317,11 +3364,14 @@ return pane // "Open in SolidOS": a plain link to the SolidOS data browser with this page's address. Only a // link: nothing is requested unless it is clicked. Not for file: or other non-web addresses. - function solidosLink () { + function solidosLink (topic) { + // The address handed over: where the reader has navigated to (a #fragment), else the thing the + // document is about (topic, from H.detect.topic), else the document. + const target = () => location.hash ? location.href : location.href + (topic || '') if (!/^https?:$/.test(location.protocol)) return null const a = document.createElement('a') a.className = 'solidos' - a.href = 'https://solidos.org/docs/browser/?uri=' + encodeURIComponent(location.href) + a.href = 'https://solidos.org/docs/browser/?uri=' + encodeURIComponent(target()) a.target = '_blank' a.rel = 'noopener noreferrer' a.title = 'Open this address in the SolidOS data browser, which loads panes from your tool index' @@ -3335,14 +3385,14 @@ return pane const span = document.createElement('span') span.textContent = 'Open in SolidOS' a.append(span, svg) - addEventListener('hashchange', () => { a.href = 'https://solidos.org/docs/browser/?uri=' + encodeURIComponent(location.href) }) + addEventListener('hashchange', () => { a.href = 'https://solidos.org/docs/browser/?uri=' + encodeURIComponent(target()) }) return a } - // The corner: the SolidOS link and a small Viewer / Raw switch. want: { link, rawSwitch }. + // The corner: the SolidOS link and a small Viewer / Raw switch. want: { link, rawSwitch, topic }. // onMode('viewer' | 'raw') is called when the switch is used. null when there is nothing to show. function corner (onMode, want) { - const link = want.link ? solidosLink() : null + const link = want.link ? solidosLink(want.topic) : null if (!link && !want.rawSwitch) return null const { host, root } = pillRoot('jsonviewer-toggle') const dock = document.createElement('div') @@ -3394,7 +3444,7 @@ return pane // belongs to a module above. A host with several panes and tabs has an entry of its own. (() => { const H = HOST - H.version = '0.0.1' + H.version = '0.0.2' const JSON_PANE = { name: 'json-viewer', file: 'json-pane.js', @@ -3456,7 +3506,7 @@ return pane fit.el = H.panes.mount(fit) fit.el.id = 'pane-' + fit.info.name document.body.insertBefore(fit.el, raw) - const corner = H.controls.corner(mode => { fit.el.hidden = mode === 'raw'; raw.hidden = mode !== 'raw' }, { link: opts.solidosLink, rawSwitch: true }) + const corner = H.controls.corner(mode => { fit.el.hidden = mode === 'raw'; raw.hidden = mode !== 'raw' }, { link: opts.solidosLink, rawSwitch: true, topic: opts.solidosLink && kind === 'json' ? H.detect.topic(text) : null }) if (corner) document.body.append(corner) show() })().catch(e => { show(); console.warn('JSON viewer:', e) })