crx-monkey-next 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/api.d.ts +3 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/api.js +4 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/api.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/i18n.d.ts +17 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/i18n.js +38 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/i18n.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/main.d.ts +16 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/main.js +8 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/main.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/message.d.ts +30 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/message.js +75 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/message.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/runtime.d.ts +43 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/runtime.js +89 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/client/runtime.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/plugins/tsBundler.d.ts +28 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/plugins/tsBundler.js +151 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/plugins/tsBundler.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/typeDefs.d.ts +200 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/typeDefs.js +2 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/src/node/typeDefs.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/dist/client/tsconfig.client.tsbuildinfo +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/api.d.ts +3 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/api.js +4 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/api.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/i18n.d.ts +17 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/i18n.js +38 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/i18n.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/main.d.ts +17 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/main.js +9 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/main.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/message.d.ts +30 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/message.js +75 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/message.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/runtime.d.ts +32 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/runtime.js +58 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/client/runtime.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/node/typeDefs.d.ts +189 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/node/typeDefs.js +2 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/src/node/typeDefs.js.map +1 -0
- package/.rollup.cache/home/yakisova41/crx-monkey-next/packages/crx-monkey/tsconfig.client.tsbuildinfo +1 -0
- package/README.md +34 -0
- package/bin/crx-monkey +2 -0
- package/bun.lockb +0 -0
- package/dist/client/main.d.ts +187 -0
- package/dist/client/main.js +200 -0
- package/dist/client/tsconfig.client.tsbuildinfo +1 -0
- package/dist/node/exports.d.ts +213 -0
- package/dist/node/exports.js +650 -0
- package/dist/node/main.js +2759 -0
- package/jest.config.js +17 -0
- package/package.json +81 -0
- package/rollup.config.js +101 -0
- package/src/client/i18n.ts +42 -0
- package/src/client/main.ts +20 -0
- package/src/client/message.ts +118 -0
- package/src/client/runtime.ts +109 -0
- package/src/node/BundlerRegisterer.ts +311 -0
- package/src/node/ConfigLoader.ts +127 -0
- package/src/node/CrxmBundler.ts +213 -0
- package/src/node/Distributior.ts +295 -0
- package/src/node/Logger.ts +77 -0
- package/src/node/Watcher.ts +166 -0
- package/src/node/__tests__/config.test.ts +157 -0
- package/src/node/__tests__/manifest.test.ts +380 -0
- package/src/node/build.ts +64 -0
- package/src/node/dev.ts +146 -0
- package/src/node/development/CreateDevClient.ts +168 -0
- package/src/node/development/codes/extension.ts +187 -0
- package/src/node/development/codes/sw.ts +41 -0
- package/src/node/development/codes/userjs.ts +150 -0
- package/src/node/exports.ts +135 -0
- package/src/node/file.ts +45 -0
- package/src/node/inversify.config.ts +53 -0
- package/src/node/main.ts +34 -0
- package/src/node/manifest/ManifestFactory.ts +165 -0
- package/src/node/manifest/ManifestLoader.ts +71 -0
- package/src/node/manifest/ManifestParser.ts +191 -0
- package/src/node/manifest/i18n.ts +93 -0
- package/src/node/plugins/htmlBundler/HTMLTools.ts +332 -0
- package/src/node/plugins/htmlBundler/main.ts +140 -0
- package/src/node/plugins/sassBundler.ts +59 -0
- package/src/node/plugins/tsBundler.ts +197 -0
- package/src/node/server/FileServer.ts +97 -0
- package/src/node/server/SockServer.ts +143 -0
- package/src/node/typeDefs.ts +221 -0
- package/src/node/types.ts +24 -0
- package/src/node/userscript/CodeInjector.ts +164 -0
- package/src/node/userscript/UserscriptBundler.ts +138 -0
- package/src/node/userscript/UserscriptHeader.ts +76 -0
- package/src/node/userscript/UserscriptRegisterer.ts +204 -0
- package/src/node/utils.ts +25 -0
- package/tsconfig.client.json +20 -0
- package/tsconfig.json +24 -0
package/jest.config.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** @type {import("jest").Config} **/
|
|
2
|
+
export default {
|
|
3
|
+
preset: 'ts-jest/presets/default-esm',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
extensionsToTreatAsEsm: ['.ts'],
|
|
6
|
+
transform: {
|
|
7
|
+
'^.+\\.tsx?$': [
|
|
8
|
+
'ts-jest',
|
|
9
|
+
{
|
|
10
|
+
useESM: true,
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
},
|
|
14
|
+
moduleNameMapper: {
|
|
15
|
+
'^(\\.{1,2}/.*)\\.js$': '$1',
|
|
16
|
+
},
|
|
17
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "crx-monkey-next",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "",
|
|
6
|
+
"bin": {
|
|
7
|
+
"crx-monkey": "bin/crx-monkey"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/node/main.js",
|
|
10
|
+
"types": "./dist/node/main.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/node/exports.d.ts",
|
|
15
|
+
"default": "./dist/node/exports.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./client": {
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./dist/client/main.d.ts",
|
|
21
|
+
"default": "./dist/client/main.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rollup -c",
|
|
27
|
+
"watch": "rollup --watch -c",
|
|
28
|
+
"test": "jest"
|
|
29
|
+
},
|
|
30
|
+
"author": "yakisova41 <yakisoba5emon@gmail.com> (https://yakisova.com/)",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/yakisova41/crx-monkey-next.git",
|
|
35
|
+
"directory": "packages/crx-monkey"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
39
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
40
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
41
|
+
"@types/bun": "^1.3.3",
|
|
42
|
+
"@types/chrome": "^0.0.268",
|
|
43
|
+
"@types/express": "^4.17.21",
|
|
44
|
+
"@types/fs-extra": "^11.0.4",
|
|
45
|
+
"@types/mock-fs": "^4.13.4",
|
|
46
|
+
"@types/moment": "^2.13.0",
|
|
47
|
+
"@types/murmurhash3js": "^3.0.7",
|
|
48
|
+
"@types/node": "^24.10.1",
|
|
49
|
+
"@types/source-map-support": "^0.5.10",
|
|
50
|
+
"@types/tampermonkey": "^5.0.2",
|
|
51
|
+
"@types/ws": "^8.5.10",
|
|
52
|
+
"cac": "^6.7.14",
|
|
53
|
+
"chalk": "^5.3.0",
|
|
54
|
+
"chokidar": "^3.6.0",
|
|
55
|
+
"consola": "^3.2.3",
|
|
56
|
+
"esbuild": "^0.24.0",
|
|
57
|
+
"esbuild-plugin-manifest": "^1.0.3",
|
|
58
|
+
"express": "^4.19.1",
|
|
59
|
+
"fs-extra": "^11.2.0",
|
|
60
|
+
"inversify": "^7.10.4",
|
|
61
|
+
"mock-fs": "^5.5.0",
|
|
62
|
+
"moment": "^2.30.1",
|
|
63
|
+
"murmurhash3js": "^3.0.1",
|
|
64
|
+
"node-html-parser": "^6.1.12",
|
|
65
|
+
"prettier": "^3.3.3",
|
|
66
|
+
"reflect-metadata": "^0.2.2",
|
|
67
|
+
"rollup": "^4.12.1",
|
|
68
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
69
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
70
|
+
"sass": "^1.93.3",
|
|
71
|
+
"source-map-support": "^0.5.21",
|
|
72
|
+
"tslib": "^2.6.2",
|
|
73
|
+
"typescript": "^5.4.2",
|
|
74
|
+
"ws": "^8.16.0",
|
|
75
|
+
"@types/jest": "^30.0.0",
|
|
76
|
+
"jest": "^30.2.0",
|
|
77
|
+
"ts-jest": "^29.4.5",
|
|
78
|
+
"ts-jest-mock-import-meta": "^1.3.1"
|
|
79
|
+
},
|
|
80
|
+
"dependencies": {}
|
|
81
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from '@rollup/plugin-typescript';
|
|
4
|
+
import json from '@rollup/plugin-json';
|
|
5
|
+
import { dts } from 'rollup-plugin-dts';
|
|
6
|
+
import copy from 'rollup-plugin-copy';
|
|
7
|
+
|
|
8
|
+
/** @typedef {import('rollup').RollupOptions} ConfigData */
|
|
9
|
+
const plugins = (tsconfig, ts) => {
|
|
10
|
+
return [
|
|
11
|
+
commonjs({
|
|
12
|
+
include: ['node_modules/**'],
|
|
13
|
+
}),
|
|
14
|
+
json(),
|
|
15
|
+
...(ts
|
|
16
|
+
? [
|
|
17
|
+
typescript({
|
|
18
|
+
declaration: false,
|
|
19
|
+
tsconfig: tsconfig,
|
|
20
|
+
exclude: ['**/__tests__/**'],
|
|
21
|
+
sourceMap: true,
|
|
22
|
+
}),
|
|
23
|
+
]
|
|
24
|
+
: []),
|
|
25
|
+
];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** @type {import('rollup').RollupOptions} */
|
|
29
|
+
const nodeConfig = {
|
|
30
|
+
input: 'src/node/main.ts',
|
|
31
|
+
output: [
|
|
32
|
+
{
|
|
33
|
+
file: 'dist/node/main.js',
|
|
34
|
+
format: 'esm',
|
|
35
|
+
sourcemap: 'inline',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
plugins: [
|
|
39
|
+
plugins('./tsconfig.json', true),
|
|
40
|
+
copy({
|
|
41
|
+
targets: [{ src: 'src/node/static/files/*', dest: 'dist/node/static' }],
|
|
42
|
+
}),
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** @type {import('rollup').RollupOptions} */
|
|
47
|
+
const nodeExportsConfig = {
|
|
48
|
+
input: 'src/node/exports.ts',
|
|
49
|
+
output: [
|
|
50
|
+
{
|
|
51
|
+
file: 'dist/node/exports.js',
|
|
52
|
+
format: 'esm',
|
|
53
|
+
sourcemap: 'inline',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
plugins: [
|
|
57
|
+
plugins('./tsconfig.json', true),
|
|
58
|
+
copy({
|
|
59
|
+
targets: [{ src: 'src/node/static/files/*', dest: 'dist/node/static' }],
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** @type {import('rollup').RollupOptions} */
|
|
65
|
+
const nodeExportsTypes = {
|
|
66
|
+
input: 'src/node/exports.ts',
|
|
67
|
+
output: [
|
|
68
|
+
{
|
|
69
|
+
file: 'dist/node/exports.d.ts',
|
|
70
|
+
format: 'esm',
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
plugins: [plugins('./tsconfig.json', false), dts()],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** @type {import('rollup').RollupOptions} */
|
|
77
|
+
const clientConfig = {
|
|
78
|
+
input: 'src/client/main.ts',
|
|
79
|
+
output: [
|
|
80
|
+
{
|
|
81
|
+
file: 'dist/client/main.js',
|
|
82
|
+
format: 'esm',
|
|
83
|
+
sourcemap: 'inline',
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
plugins: [plugins('./tsconfig.client.json', true)],
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** @type {import('rollup').RollupOptions} */
|
|
90
|
+
const clientTypes = {
|
|
91
|
+
input: 'src/client/main.ts',
|
|
92
|
+
output: [
|
|
93
|
+
{
|
|
94
|
+
file: 'dist/client/main.d.ts',
|
|
95
|
+
format: 'esm',
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
plugins: [plugins('./tsconfig.client.json', false), dts()],
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export default [nodeConfig, nodeExportsConfig, nodeExportsTypes, clientConfig, clientTypes];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { runtime } from './runtime';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Gets the localized string for the specified message. If the message is missing, this method returns an empty string (''). If the format of the getMessage() call is wrong — for example, messageName is not a string or the substitutions array has more than 9 elements — this method returns undefined.
|
|
5
|
+
* @param messageName
|
|
6
|
+
* @param substitutions
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
function getMessage(messageName: string, substitutions?: string | string[] | undefined) {
|
|
10
|
+
if (runtime.getRunningRuntime() === 'Extension') {
|
|
11
|
+
if (runtime.getRunningWorld() === 'ISOLATED') {
|
|
12
|
+
return chrome.i18n.getMessage(messageName, substitutions);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function detectLanguage(text: string) {
|
|
18
|
+
if (runtime.getRunningRuntime() === 'Extension') {
|
|
19
|
+
if (runtime.getRunningWorld() === 'ISOLATED') {
|
|
20
|
+
return chrome.i18n.detectLanguage(text);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function getAcceptLanguages() {
|
|
26
|
+
if (runtime.getRunningRuntime() === 'Extension') {
|
|
27
|
+
if (runtime.getRunningWorld() === 'ISOLATED') {
|
|
28
|
+
return chrome.i18n.getAcceptLanguages();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getUILanguage() {
|
|
34
|
+
if (runtime.getRunningRuntime() === 'Extension') {
|
|
35
|
+
if (runtime.getRunningWorld() === 'ISOLATED') {
|
|
36
|
+
return chrome.i18n.getUILanguage();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const i18n = { getMessage, detectLanguage, getAcceptLanguages, getUILanguage };
|
|
42
|
+
export default i18n;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The main endpoint for client of crx-monkey.
|
|
3
|
+
* Modules and Types that are exported from this file are used by userside.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { IsolateConnectorEvent } from './message';
|
|
7
|
+
|
|
8
|
+
export * from './message';
|
|
9
|
+
export * from './i18n';
|
|
10
|
+
export * from './runtime';
|
|
11
|
+
|
|
12
|
+
declare global {
|
|
13
|
+
interface Window {
|
|
14
|
+
__CRX_CONTENT_BUILD_ID: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface WindowEventMap {
|
|
18
|
+
'crx-isolated-connector-result': IsolateConnectorEvent<never>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export function waitResult<T = string>(
|
|
2
|
+
type: string,
|
|
3
|
+
actionId: string,
|
|
4
|
+
callback: (data: T) => void,
|
|
5
|
+
) {
|
|
6
|
+
const onResult = (e: IsolateConnectorEvent<T>) => {
|
|
7
|
+
if (e.detail.type === type && e.detail.actionId === actionId) {
|
|
8
|
+
callback(e.detail.data);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
window.addEventListener('crx-isolated-connector-result', onResult);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
remove: () => {
|
|
16
|
+
window.removeEventListener('crx-isolated-connector-result', onResult);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function waitResultOnce<T = string>(type: string, actionId: string): Promise<T> {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
const onResult = (e: IsolateConnectorEvent<T>) => {
|
|
24
|
+
if (e.detail.type === type && e.detail.actionId === actionId) {
|
|
25
|
+
window.removeEventListener('crx-isolated-connector-result', onResult);
|
|
26
|
+
resolve(e.detail.data);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
window.addEventListener('crx-isolated-connector-result', onResult);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type IsolateConnectorEvent<T> = CustomEvent<{
|
|
35
|
+
type: string;
|
|
36
|
+
actionId: string;
|
|
37
|
+
data: T;
|
|
38
|
+
}>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Bypass `chrome.runtime.sendMessage`.
|
|
42
|
+
*
|
|
43
|
+
* You can send message to service worker.
|
|
44
|
+
* @param callback
|
|
45
|
+
*/
|
|
46
|
+
async function sendMessage<T = never, U = never>(
|
|
47
|
+
message: T,
|
|
48
|
+
options?: object,
|
|
49
|
+
callback?: (response: U) => void,
|
|
50
|
+
) {
|
|
51
|
+
const actionId = crypto.randomUUID();
|
|
52
|
+
|
|
53
|
+
window.postMessage(
|
|
54
|
+
{
|
|
55
|
+
type: 'send-message',
|
|
56
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
57
|
+
detail: { message, options },
|
|
58
|
+
actionId,
|
|
59
|
+
},
|
|
60
|
+
'*',
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const data = await waitResultOnce<{ response: U }>('send-message', actionId);
|
|
64
|
+
if (callback !== undefined) {
|
|
65
|
+
callback(data.response);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Bypass `chrome.runtime.onMessage`.
|
|
71
|
+
*
|
|
72
|
+
* You can get a content of message receved the bypassing isolated content_script.
|
|
73
|
+
* @param callback
|
|
74
|
+
*/
|
|
75
|
+
function addListener<T = never>(
|
|
76
|
+
callback: (request: T, sender: chrome.runtime.MessageSender) => void,
|
|
77
|
+
): {
|
|
78
|
+
remove: () => void;
|
|
79
|
+
} {
|
|
80
|
+
const actionId = crypto.randomUUID();
|
|
81
|
+
|
|
82
|
+
window.postMessage(
|
|
83
|
+
{
|
|
84
|
+
type: 'on-message',
|
|
85
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
86
|
+
detail: null,
|
|
87
|
+
actionId,
|
|
88
|
+
},
|
|
89
|
+
'*',
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const waiter = waitResult<{ request: T; sender: { id: string; origin: string } }>(
|
|
93
|
+
'on-message',
|
|
94
|
+
actionId,
|
|
95
|
+
(data) => {
|
|
96
|
+
callback(data.request, data.sender);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
remove: () => {
|
|
102
|
+
waiter.remove();
|
|
103
|
+
|
|
104
|
+
window.postMessage(
|
|
105
|
+
{
|
|
106
|
+
type: 'remove-on-message',
|
|
107
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
108
|
+
detail: null,
|
|
109
|
+
actionId,
|
|
110
|
+
},
|
|
111
|
+
'*',
|
|
112
|
+
);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const message = { sendMessage, addListener };
|
|
118
|
+
export default message;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { waitResultOnce } from './message';
|
|
2
|
+
import { CrxmConfigRequired } from '../node/typeDefs';
|
|
3
|
+
|
|
4
|
+
export function getRunningRuntime() {
|
|
5
|
+
if (typeof window.__CRX_CONTENT_BUILD_ID === 'undefined') {
|
|
6
|
+
return 'Userscript';
|
|
7
|
+
} else {
|
|
8
|
+
return 'Extension';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getRunningWorld() {
|
|
13
|
+
if (getRunningRuntime() === 'Userscript') {
|
|
14
|
+
throw new Error('Cannot be executed by userscripts');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (typeof chrome.runtime.id === 'undefined') {
|
|
18
|
+
return 'MAIN';
|
|
19
|
+
} else {
|
|
20
|
+
return 'ISOLATED';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function getCrxmConfig() {
|
|
25
|
+
if (getRunningRuntime() === 'Userscript') {
|
|
26
|
+
throw new Error('Cannot be executed by userscripts');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const actionId = crypto.randomUUID();
|
|
30
|
+
|
|
31
|
+
window.postMessage(
|
|
32
|
+
{
|
|
33
|
+
type: 'get-conf',
|
|
34
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
35
|
+
detail: null,
|
|
36
|
+
actionId,
|
|
37
|
+
},
|
|
38
|
+
'*',
|
|
39
|
+
);
|
|
40
|
+
return JSON.parse(await waitResultOnce<string>('get-conf', actionId)) as CrxmConfigRequired;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get `chrome.runtime.id`.
|
|
45
|
+
*
|
|
46
|
+
* You can get the extension id everyworld if running script by chrome extension.
|
|
47
|
+
*/
|
|
48
|
+
export async function getExtensionId() {
|
|
49
|
+
if (getRunningRuntime() === 'Userscript') {
|
|
50
|
+
throw new Error('Cannot be executed by userscripts');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (getRunningWorld() === 'ISOLATED') {
|
|
54
|
+
return chrome.runtime.id;
|
|
55
|
+
} else {
|
|
56
|
+
const actionId = crypto.randomUUID();
|
|
57
|
+
|
|
58
|
+
window.postMessage(
|
|
59
|
+
{
|
|
60
|
+
type: 'get-id',
|
|
61
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
62
|
+
detail: null,
|
|
63
|
+
actionId,
|
|
64
|
+
},
|
|
65
|
+
'*',
|
|
66
|
+
);
|
|
67
|
+
return await waitResultOnce<string>('get-id', actionId);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* You can check browser's console logs in your terminal at development mode.
|
|
73
|
+
* [ATTENSION] It's unstable future.
|
|
74
|
+
*/
|
|
75
|
+
export function attachConsole() {
|
|
76
|
+
const replaceOriginal = (original: (...args: string[]) => void, name: string) => {
|
|
77
|
+
return (...args: string[]) => {
|
|
78
|
+
original(...args);
|
|
79
|
+
|
|
80
|
+
const actionId = crypto.randomUUID();
|
|
81
|
+
window.postMessage(
|
|
82
|
+
{
|
|
83
|
+
type: name,
|
|
84
|
+
crxContentBuildId: window.__CRX_CONTENT_BUILD_ID,
|
|
85
|
+
detail: args.join(' '),
|
|
86
|
+
actionId,
|
|
87
|
+
},
|
|
88
|
+
'*',
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const l = console.log;
|
|
94
|
+
const w = console.warn;
|
|
95
|
+
const e = console.error;
|
|
96
|
+
|
|
97
|
+
window.console.log = replaceOriginal(l, 'console-log');
|
|
98
|
+
window.console.warn = replaceOriginal(w, 'console-warn');
|
|
99
|
+
window.console.error = replaceOriginal(e, 'console-error');
|
|
100
|
+
console.log("[crxm] browser's console attached to your console.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const runtime = {
|
|
104
|
+
getRunningRuntime,
|
|
105
|
+
getRunningWorld,
|
|
106
|
+
getExtensionId,
|
|
107
|
+
getCrxmConfig,
|
|
108
|
+
attachConsole,
|
|
109
|
+
};
|