Skip to content

Commit

Permalink
Added CSP executeScript fallback (#80)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
exdis authored Jul 15, 2022
1 parent 7f57ef9 commit a4cb96a
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 221 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.13.0 (15-07-2022)

* Fixed bug when CSP broke JSON display on the page
* Fixed work of extension for raw.github* pages
* Updated `esbuild` to `0.14.49`
* Updated `discovery` to `1.0.0-beta.66`

## 1.12.2 (29-04-2022)

* Updated `discovery` to `1.0.0-beta.65`
Expand Down
384 changes: 192 additions & 192 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsondiscovery",
"version": "1.12.2",
"version": "1.13.0",
"description": "Browser (Chrome, Firefox) extension for JSON discovery",
"author": "[email protected]",
"license": "MIT",
Expand All @@ -11,10 +11,10 @@
"build": "NODE_ENV=production node ./scripts/build.js && node ./scripts/zip.js"
},
"devDependencies": {
"@discoveryjs/discovery": "1.0.0-beta.65",
"@discoveryjs/discovery": "1.0.0-beta.66",
"@discoveryjs/json-ext": "^0.5.7",
"css-tree": "^2.1.0",
"esbuild": "^0.14.25",
"esbuild": "^0.14.49",
"eslint": "^8.10.0",
"jszip": "^3.7.1",
"mime": "^3.0.0"
Expand Down
6 changes: 5 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ async function build(browser) {
// build bundle
const result = await esbuild.build({
entryPoints: [
path.join(indir, 'background.js'),
path.join(indir, 'content/discovery.css'),
path.join(indir, 'content/preloader.css'),
path.join(indir, 'content/discovery.js'),
path.join(indir, 'content/discovery-esm.js'),
path.join(indir, 'content/init.js')
],
format: 'esm',
Expand All @@ -77,7 +79,9 @@ async function build(browser) {
? processCss(file.text, outdir, 'assets')
: file.contents;

fs.writeFileSync(file.path, content);
const filePath = path.join(outdir, path.basename(file.path));

fs.writeFileSync(filePath, content);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
chrome.runtime.onMessage.addListener((message, { tab }) => {
if (message.type === 'initDiscovery') {
chrome.tabs.executeScript(tab.id, { file: 'discovery.js' });
}
});
1 change: 1 addition & 0 deletions src/content/discovery-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { initDiscovery } from '../discovery';
8 changes: 7 additions & 1 deletion src/content/discovery.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { initDiscovery } from '../discovery';
import { initDiscovery } from '../discovery';

initDiscovery(...window.__discoveryOptions) // eslint-disable-line no-underscore-dangle
.then(() => {
window.__discoveryPreloader.el.remove(); // eslint-disable-line no-underscore-dangle
});

59 changes: 36 additions & 23 deletions src/content/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,34 +159,47 @@ async function checkLoaded(settings) {
pushChunk(null); // end of input

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

await initDiscovery({
node: document.body,
raw: Object.defineProperties({}, {
firstSlice: {
value: totalSize < firstSliceMaxSize * 2 ? null : firstSlice
},
size: {
value: totalSize
},
json: totalSize <= firstSliceMaxSize ? { value: firstSlice } : {
configurable: true,
get() {
return Object.defineProperty(this, 'json', {
value: pre.textContent
}).json;
const discoveryOptions = [
{
node: document.body,
raw: Object.defineProperties({}, {
firstSlice: {
value: totalSize < firstSliceMaxSize * 2 ? null : firstSlice
},
size: {
value: totalSize
},
json: totalSize <= firstSliceMaxSize ? { value: firstSlice } : {
configurable: true,
get() {
return Object.defineProperty(this, 'json', {
value: pre.textContent
}).json;
}
}
}
}),
settings,
styles: [chrome.runtime.getURL('index.css')],
progressbar: preloader.progressbar
}, json);
}),
settings,
styles: [chrome.runtime.getURL('index.css')],
progressbar: preloader.progressbar
}, json
];

// In case of sandboxed CSP pages await import will fail
// so here we send message to bg which executes discovery initiation via chrome API
if (typeof initDiscovery !== 'function') {
window.__discoveryPreloader = preloader; // eslint-disable-line no-underscore-dangle
window.__discoveryOptions = discoveryOptions; // eslint-disable-line no-underscore-dangle

await chrome.runtime.sendMessage({ type: 'initDiscovery' });
} else {
await initDiscovery(...discoveryOptions);

preloader.el.remove();
preloader.el.remove();
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@
},
"permissions": [
"<all_urls>",
"storage"
"storage",
"tabs"
],
"content_scripts": [{
"js": ["init.js"],
"run_at": "document_start",
"matches": ["<all_urls>"]
}],
"background": {
"scripts": ["background.js"]
},
"web_accessible_resources": [
"discovery.css",
"discovery.js",
"discovery-esm.js",
"preloader.css",
"icons/*",
"assets/*"
Expand Down

0 comments on commit a4cb96a

Please sign in to comment.