Skip to content

Commit 78bd1d5

Browse files
committed
transforms
1 parent 8f6b6c9 commit 78bd1d5

File tree

7 files changed

+533
-7
lines changed

7 files changed

+533
-7
lines changed

addon/manifest.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"js": ["content_script.js"]
1616
}
1717
],
18-
"permissions": ["storage"],
18+
"background": {
19+
"scripts": ["background_script.js"]
20+
},
21+
"permissions": ["storage", "webRequest", "webRequestBlocking", "*://*.blaseball.com/*", "https://d35iw2jmbg6ut8.cloudfront.net/*"],
1922
"options_ui": {
2023
"browser_style": true,
2124
"page": "options_page.html"

lib/background.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import browser from 'webextension-polyfill';
2+
import transform from './transform.js';
3+
4+
function jsHandler(details) {
5+
let filter = browser.webRequest.filterResponseData(details.requestId);
6+
7+
let buf = '';
8+
let decoder = new TextDecoder('utf-8');
9+
const encoder = new TextEncoder();
10+
11+
filter.ondata = event => {
12+
buf += decoder.decode(event.data, { stream: true });
13+
};
14+
15+
filter.onstop = async () => {
16+
const script = await transform(buf);
17+
filter.write(encoder.encode(script));
18+
filter.disconnect();
19+
};
20+
21+
return {};
22+
}
23+
24+
browser.webRequest.onBeforeRequest.addListener(
25+
jsHandler,
26+
{
27+
urls: ["https://d35iw2jmbg6ut8.cloudfront.net/static/js/*.js"]
28+
},
29+
["blocking"]
30+
);

lib/transform.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { transformAsync } from '@babel/core';
2+
import alwaysFk from './transforms/alwaysFk.js';
3+
4+
const options = {
5+
plugins: [alwaysFk],
6+
generatorOpts: {
7+
compact: true
8+
}
9+
};
10+
11+
export default async function transform(source) {
12+
console.time('transforming');
13+
const { code } = await transformAsync(source, options);
14+
console.timeEnd('transforming');
15+
return code;
16+
}

lib/transforms/alwaysFk.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function({ types: t }) {
2+
return {
3+
visitor: {
4+
ConditionalExpression(path) {
5+
if (t.isLogicalExpression(path.node.test)) {
6+
if (path.node.test.operator !== '&&') return;
7+
if (!t.isBinaryExpression(path.node.test.left)) return;
8+
if (!t.isMemberExpression(path.node.test.left.right)) return;
9+
if (path.node.test.left.right.computed) return;
10+
if (path.node.test.left.right.property.name !== 'Forbidden_Knowledge_Access') return;
11+
12+
if (t.isBinaryExpression(path.node.test.right) && t.isStringLiteral(path.node.test.right.left)) {
13+
path.node.test = path.node.test.right;
14+
} else {
15+
path.replaceWith(path.node.consequent);
16+
}
17+
} else if (t.isBinaryExpression(path.node.test)) {
18+
if (!t.isMemberExpression(path.node.test.right)) return;
19+
if (path.node.test.right.computed) return;
20+
if (path.node.test.right.property.name !== 'Forbidden_Knowledge_Access') return;
21+
22+
path.replaceWith(path.node.consequent);
23+
}
24+
}
25+
}
26+
}
27+
}

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030
"webpack-cli": "^4.5.0"
3131
},
3232
"dependencies": {
33+
"@babel/core": "^7.13.15",
3334
"@popperjs/core": "^2.8.6",
35+
"assert": "^2.0.0",
36+
"buffer": "^6.0.3",
3437
"camelcase": "^6.2.0",
38+
"path-browserify": "^1.0.1",
39+
"process": "^0.11.10",
3540
"webextension-polyfill": "^0.7.0"
3641
},
3742
"type": "module"

webpack.config.mjs

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { dirname, resolve } from 'path';
22
import { fileURLToPath } from 'url';
33
import WebExtWebpackPlugin from '@leo60228/web-ext-webpack-plugin';
4+
import { createRequire } from 'module';
5+
import webpack from 'webpack';
6+
7+
const require = createRequire(import.meta.url);
48

59
const cwd = dirname(fileURLToPath(import.meta.url));
610
const addonDir = resolve(cwd, 'addon');
@@ -10,6 +14,7 @@ const config = env => ({
1014
devtool: env.production ? 'source-map' : 'cheap-source-map',
1115
entry: {
1216
content_script: './lib/content.js',
17+
background_script: './lib/background.js',
1318
options_page: './lib/optionsPage.js',
1419
},
1520
output: {
@@ -26,6 +31,10 @@ const config = env => ({
2631
firefoxProfile: resolve(cwd, '.ff-profile'),
2732
profileCreateIfMissing: true,
2833
keepProfileChanges: true
34+
}),
35+
new webpack.ProvidePlugin({
36+
process: 'process/browser',
37+
Buffer: 'buffer/'
2938
})
3039
],
3140
node: {
@@ -42,6 +51,13 @@ const config = env => ({
4251
type: 'asset/source'
4352
}
4453
]
54+
},
55+
resolve: {
56+
fallback: {
57+
path: require.resolve('path-browserify'),
58+
assert: require.resolve('assert/'),
59+
fs: false
60+
}
4561
}
4662
});
4763

0 commit comments

Comments
 (0)