Skip to content

Commit a65c8b0

Browse files
committed
Fixed CSP images issues (sick!)
1 parent 9b69db0 commit a65c8b0

File tree

8 files changed

+82
-31
lines changed

8 files changed

+82
-31
lines changed

core/build.js

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const csstree = require('css-tree');
4+
const mime = require('mime');
5+
const crypto = require('crypto');
36
const bundleJs = require('esbuild').build;
47
const manifest = require('../src/manifest.js');
58

@@ -14,18 +17,6 @@ const browsers = [
1417
'safari'
1518
];
1619

17-
function copyFiles(src, dest) {
18-
fs.mkdirSync(dest, { recursive: true });
19-
20-
if (fs.statSync(src).isDirectory()) {
21-
fs.readdirSync(src).forEach(p =>
22-
copyFiles(path.join(src, p), path.join(dest, path.basename(src)))
23-
);
24-
} else {
25-
fs.copyFileSync(src, path.join(dest, path.basename(src)));
26-
}
27-
}
28-
2920
async function build(browser) {
3021
const outdir = path.join(__dirname, `/../build-${browser}`);
3122

@@ -45,7 +36,37 @@ async function build(browser) {
4536
// minify: true,
4637
format: 'esm',
4738
outdir
48-
}).catch(() => process.exit(1));
39+
})
40+
.then(() => {
41+
const css = fs.readFileSync(path.join(outdir, 'index.css'), 'utf8');
42+
const ast = csstree.parse(css);
43+
44+
csstree.walk(ast, {
45+
enter(node) {
46+
if (node.type === 'Url') {
47+
const value = getValueFromStringOrRaw(node.value);
48+
const [, mimeType, content] = value.match(/^data:(.*);base64,(.*)$/);
49+
const filename = crypto
50+
.createHash('sha1')
51+
.update(content)
52+
.digest('hex') + '.' + mime.getExtension(mimeType);
53+
54+
fs.writeFileSync(path.join(outdir, 'icons', filename), Buffer.from(content, 'base64'));
55+
56+
node.value = {
57+
type: 'Raw',
58+
value: `icons/${filename}`
59+
};
60+
}
61+
}
62+
});
63+
64+
fs.writeFileSync(path.join(outdir, 'index.css'), csstree.generate(ast));
65+
})
66+
.catch((error) => {
67+
console.error(error); // eslint-disable-line no-console
68+
process.exit(1);
69+
});
4970
}
5071

5172
const buildAll = async function() {
@@ -64,3 +85,27 @@ const buildAll = async function() {
6485
});
6586
}
6687
})();
88+
89+
function copyFiles(src, dest) {
90+
fs.mkdirSync(dest, { recursive: true });
91+
92+
if (fs.statSync(src).isDirectory()) {
93+
fs.readdirSync(src).forEach(p =>
94+
copyFiles(path.join(src, p), path.join(dest, path.basename(src)))
95+
);
96+
} else {
97+
fs.copyFileSync(src, path.join(dest, path.basename(src)));
98+
}
99+
}
100+
101+
function getValueFromStringOrRaw(node) {
102+
switch (node.type) {
103+
case 'String':
104+
return node.value.substring(1, node.value.length - 1);
105+
106+
case 'Raw':
107+
return node.value;
108+
}
109+
110+
return null;
111+
}

package-lock.json

Lines changed: 3 additions & 3 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"@discoveryjs/discovery": "1.0.0-beta.50",
1616
"@discoveryjs/json-ext": "^0.5.0",
1717
"babel-eslint": "^10.1.0",
18+
"css-tree": "^1.1.2",
1819
"esbuild": "^0.8.27",
1920
"eslint": "^4.9.0",
20-
"jszip": "^3.5.0"
21+
"jszip": "^3.5.0",
22+
"mime": "^2.4.7"
2123
}
2224
}

src/content/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '@discoveryjs/discovery/dist/discovery.raw.css';
1+
@import '@discoveryjs/discovery/dist/discovery.css';
22

33
table, caption, th, td, tr {
44
color: inherit;

src/content/init-discovery.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/**
22
* Discovery initialization
33
* @param {Object} options
4+
* @param {Object} data
45
* @returns {Discovery}
56
*/
6-
export function initDiscovery(options) {
7-
const { Widget, router, complexViews, utils } = require('@discoveryjs/discovery/dist/discovery.js');
7+
export function initDiscovery(options, data) {
8+
const { Widget, router, utils } = require('@discoveryjs/discovery/dist/discovery.js');
89
const settingsPage = require('../settings').default;
910

1011
const { settings } = options;
1112
const { darkmode = 'auto' } = settings;
1213
const discovery = new Widget(options.node, null, {
1314
darkmode,
14-
styles: options.styles
15+
styles: [{ type: 'link', data: chrome.runtime.getURL('index.css') }]
1516
});
1617

1718
discovery.apply(router);
18-
discovery.apply(complexViews);
1919

2020
discovery.flashMessagesContainer = utils.createElement('div', 'flash-messages-container');
2121
discovery.dom.container.append(discovery.flashMessagesContainer);
@@ -110,7 +110,7 @@ export function initDiscovery(options) {
110110
});
111111

112112
discovery.setData(
113-
options.data,
113+
data,
114114
{
115115
name: options.title,
116116
raw: options.raw,

src/content/init.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { loader } from '@discoveryjs/discovery/dist/discovery-loader.js';
2+
13
export const init = initDiscoveryBundled => {
24
let loaded = document.readyState === 'complete';
35
let pre = null;
@@ -56,23 +58,24 @@ export const init = initDiscoveryBundled => {
5658
initDiscoveryBundled({
5759
node: document.body,
5860
raw: textContent,
59-
data: json,
6061
settings,
6162
styles: [chrome.runtime.getURL('index.css')]
62-
});
63+
}, json);
6364
});
6465
}
6566

6667
try {
6768
const { initDiscovery } = await import(chrome.runtime.getURL('init-discovery.js'));
6869

6970
getSettings(settings => {
70-
initDiscovery({
71-
node: document.body,
72-
raw: textContent,
71+
loader({
72+
module: initDiscovery,
7373
data: json,
74-
settings,
75-
styles: [chrome.runtime.getURL('index.css')]
74+
options: {
75+
node: document.body,
76+
raw: textContent,
77+
settings
78+
}
7679
});
7780
});
7881
} catch (_) {}

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
}],
2222
"web_accessible_resources": [
2323
"index.css",
24-
"init-discovery.js"
24+
"init-discovery.js",
25+
"icons/*"
2526
],
2627
"offline_enabled": true
2728
}

0 commit comments

Comments
 (0)