|
1 | 1 | /* global chrome */
|
| 2 | +import { resolveToUnpkg, mdjsProcess } from '../dist/index.js'; |
2 | 3 |
|
3 | 4 | // chrome.browserAction.onClicked.addListener(async tab => {
|
4 | 5 | // // Send a message to the active tab
|
|
10 | 11 | // });
|
11 | 12 | // });
|
12 | 13 |
|
| 14 | +chrome.runtime.onMessage.addListener(({ action, ...options }, sender, sendResponse) => { |
| 15 | + if (action === 'mdjs+unpkg') { |
| 16 | + const { mdjs, pkgJson } = options; |
| 17 | + (async () => { |
| 18 | + const data = await mdjsProcess(mdjs); |
| 19 | + const executeCode = await resolveToUnpkg(data.jsCode, pkgJson); |
| 20 | + sendResponse({ jsCode: executeCode, html: data.html }); |
| 21 | + })(); |
| 22 | + } |
| 23 | + if (action === 'fetch') { |
| 24 | + const { url, fetchProcess } = options; |
| 25 | + (async () => { |
| 26 | + const response = await fetch(url); |
| 27 | + |
| 28 | + // if HTTP-status is 200-299 |
| 29 | + if (response.ok) { |
| 30 | + let data; |
| 31 | + switch (fetchProcess) { |
| 32 | + case 'text': |
| 33 | + data = await response.text(); |
| 34 | + break; |
| 35 | + case 'json': |
| 36 | + data = await response.json(); |
| 37 | + break; |
| 38 | + /* no default */ |
| 39 | + } |
| 40 | + sendResponse({ ok: response.ok, data }); |
| 41 | + } else { |
| 42 | + sendResponse({ ok: response.ok }); |
| 43 | + } |
| 44 | + })(); |
| 45 | + } |
| 46 | + |
| 47 | + // mark this message as async |
| 48 | + return true; |
| 49 | +}); |
| 50 | + |
13 | 51 | function onHeadersReceived(details) {
|
14 | 52 | const responseHeaders = [];
|
15 | 53 | for (const header of details.responseHeaders) {
|
16 | 54 | const { name } = header;
|
17 | 55 | let { value } = header;
|
18 | 56 | if (name.toLowerCase() === 'content-security-policy') {
|
19 | 57 | // adds to script-src
|
20 |
| - // - `'unsafe-eval'` to allow [wasm](https://webassembly.org/) to analyze the code (moving action background will remove that need) |
21 |
| - // - `'unsafe-inline'` to execute code within the mdjs iframe |
22 |
| - // - `unpkg.com` to load dependencies from |
23 |
| - value = value.replace('script-src', "script-src 'unsafe-eval' 'unsafe-inline' unpkg.com"); |
24 |
| - // adds to connect-src |
25 |
| - // - `raw.githubusercontent.com` to fetch raw md content and package.json |
26 |
| - value = value.replace('connect-src', 'connect-src raw.githubusercontent.com'); |
27 |
| - // adds to frame-src |
28 |
| - // - `data:` to enable setting the content of the mdjs iframe |
29 |
| - value = value.replace('frame-src', 'frame-src data:'); |
30 |
| - // adds to style-src |
31 |
| - // - raw.githubusercontent.com to fetch raw md content and package.json |
32 |
| - value = value.replace('style-src', 'style-src unpkg.com'); |
| 58 | + // - `'unsafe-inline'` to execute code blocks within the mdjs iframe |
| 59 | + // - `unpkg.com` to load user dependencies from within the mdjs iframe |
| 60 | + value = value.replace('script-src', "script-src 'unsafe-inline' unpkg.com"); |
33 | 61 | }
|
34 | 62 | responseHeaders.push({ name, value });
|
35 | 63 | }
|
|
0 commit comments