Skip to content

Commit a4cb96a

Browse files
authored
Added CSP executeScript fallback (#80)
* Added CSP executeScript fallback * Fixed lints * Build workflow -> node 14 * Fixed discovery double init * Build workflow -> node 16 * CR fixes * CR fixes * CR fixes * Updated discovery to 1.0.0-beta.66
1 parent 7f57ef9 commit a4cb96a

File tree

9 files changed

+262
-221
lines changed

9 files changed

+262
-221
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.13.0 (15-07-2022)
2+
3+
* Fixed bug when CSP broke JSON display on the page
4+
* Fixed work of extension for raw.github* pages
5+
* Updated `esbuild` to `0.14.49`
6+
* Updated `discovery` to `1.0.0-beta.66`
7+
18
## 1.12.2 (29-04-2022)
29

310
* Updated `discovery` to `1.0.0-beta.65`

package-lock.json

Lines changed: 192 additions & 192 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsondiscovery",
3-
"version": "1.12.2",
3+
"version": "1.13.0",
44
"description": "Browser (Chrome, Firefox) extension for JSON discovery",
55
"author": "[email protected]",
66
"license": "MIT",
@@ -11,10 +11,10 @@
1111
"build": "NODE_ENV=production node ./scripts/build.js && node ./scripts/zip.js"
1212
},
1313
"devDependencies": {
14-
"@discoveryjs/discovery": "1.0.0-beta.65",
14+
"@discoveryjs/discovery": "1.0.0-beta.66",
1515
"@discoveryjs/json-ext": "^0.5.7",
1616
"css-tree": "^2.1.0",
17-
"esbuild": "^0.14.25",
17+
"esbuild": "^0.14.49",
1818
"eslint": "^8.10.0",
1919
"jszip": "^3.7.1",
2020
"mime": "^3.0.0"

scripts/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ async function build(browser) {
5353
// build bundle
5454
const result = await esbuild.build({
5555
entryPoints: [
56+
path.join(indir, 'background.js'),
5657
path.join(indir, 'content/discovery.css'),
5758
path.join(indir, 'content/preloader.css'),
5859
path.join(indir, 'content/discovery.js'),
60+
path.join(indir, 'content/discovery-esm.js'),
5961
path.join(indir, 'content/init.js')
6062
],
6163
format: 'esm',
@@ -77,7 +79,9 @@ async function build(browser) {
7779
? processCss(file.text, outdir, 'assets')
7880
: file.contents;
7981

80-
fs.writeFileSync(file.path, content);
82+
const filePath = path.join(outdir, path.basename(file.path));
83+
84+
fs.writeFileSync(filePath, content);
8185
}
8286
}
8387

src/background.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
chrome.runtime.onMessage.addListener((message, { tab }) => {
2+
if (message.type === 'initDiscovery') {
3+
chrome.tabs.executeScript(tab.id, { file: 'discovery.js' });
4+
}
5+
});

src/content/discovery-esm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { initDiscovery } from '../discovery';

src/content/discovery.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
export { initDiscovery } from '../discovery';
1+
import { initDiscovery } from '../discovery';
2+
3+
initDiscovery(...window.__discoveryOptions) // eslint-disable-line no-underscore-dangle
4+
.then(() => {
5+
window.__discoveryPreloader.el.remove(); // eslint-disable-line no-underscore-dangle
6+
});
7+

src/content/init.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,47 @@ async function checkLoaded(settings) {
159159
pushChunk(null); // end of input
160160

161161
const [{ initDiscovery }, json] = await Promise.all([
162-
import(chrome.runtime.getURL('discovery.js')),
162+
import(chrome.runtime.getURL('discovery-esm.js')),
163163
data
164164
]);
165165

166-
await initDiscovery({
167-
node: document.body,
168-
raw: Object.defineProperties({}, {
169-
firstSlice: {
170-
value: totalSize < firstSliceMaxSize * 2 ? null : firstSlice
171-
},
172-
size: {
173-
value: totalSize
174-
},
175-
json: totalSize <= firstSliceMaxSize ? { value: firstSlice } : {
176-
configurable: true,
177-
get() {
178-
return Object.defineProperty(this, 'json', {
179-
value: pre.textContent
180-
}).json;
166+
const discoveryOptions = [
167+
{
168+
node: document.body,
169+
raw: Object.defineProperties({}, {
170+
firstSlice: {
171+
value: totalSize < firstSliceMaxSize * 2 ? null : firstSlice
172+
},
173+
size: {
174+
value: totalSize
175+
},
176+
json: totalSize <= firstSliceMaxSize ? { value: firstSlice } : {
177+
configurable: true,
178+
get() {
179+
return Object.defineProperty(this, 'json', {
180+
value: pre.textContent
181+
}).json;
182+
}
181183
}
182-
}
183-
}),
184-
settings,
185-
styles: [chrome.runtime.getURL('index.css')],
186-
progressbar: preloader.progressbar
187-
}, json);
184+
}),
185+
settings,
186+
styles: [chrome.runtime.getURL('index.css')],
187+
progressbar: preloader.progressbar
188+
}, json
189+
];
190+
191+
// In case of sandboxed CSP pages await import will fail
192+
// so here we send message to bg which executes discovery initiation via chrome API
193+
if (typeof initDiscovery !== 'function') {
194+
window.__discoveryPreloader = preloader; // eslint-disable-line no-underscore-dangle
195+
window.__discoveryOptions = discoveryOptions; // eslint-disable-line no-underscore-dangle
196+
197+
await chrome.runtime.sendMessage({ type: 'initDiscovery' });
198+
} else {
199+
await initDiscovery(...discoveryOptions);
188200

189-
preloader.el.remove();
201+
preloader.el.remove();
202+
}
190203
}
191204
}
192205

src/manifest.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@
1212
},
1313
"permissions": [
1414
"<all_urls>",
15-
"storage"
15+
"storage",
16+
"tabs"
1617
],
1718
"content_scripts": [{
1819
"js": ["init.js"],
1920
"run_at": "document_start",
2021
"matches": ["<all_urls>"]
2122
}],
23+
"background": {
24+
"scripts": ["background.js"]
25+
},
2226
"web_accessible_resources": [
2327
"discovery.css",
2428
"discovery.js",
29+
"discovery-esm.js",
2530
"preloader.css",
2631
"icons/*",
2732
"assets/*"

0 commit comments

Comments
 (0)