Inject document script using raw HTML to be fast enough to still be able to hijack listener when visiting YouTube channel page directly

This commit is contained in:
Ajay 2023-03-17 21:10:26 -04:00
parent 758f0b7526
commit 5d610a342f
4 changed files with 172 additions and 101 deletions

14
package-lock.json generated
View file

@ -27,7 +27,7 @@
], ],
"license": "LGPL-3.0-or-later", "license": "LGPL-3.0-or-later",
"dependencies": { "dependencies": {
"@ajayyy/maze-utils": "^1.1.8", "@ajayyy/maze-utils": "^1.1.9",
"content-scripts-register-polyfill": "^4.0.2", "content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
@ -67,9 +67,9 @@
} }
}, },
"node_modules/@ajayyy/maze-utils": { "node_modules/@ajayyy/maze-utils": {
"version": "1.1.8", "version": "1.1.9",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.8.tgz", "resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.9.tgz",
"integrity": "sha512-0LwL+i/JvQZYBu6BadYX77XUoU0QVJZXCaElihVTO7suuZ9rracJX7w7A/p12whR/bQ2pO2bCqIvXuWoOiln0Q==", "integrity": "sha512-fU85QRrSBAP31ppFs+Gzfm9vHx+4wFFDr8pSoT747+w6L6cMPlubWG0lFquRTjffStfZlw33kGJOSuwXNiYbAA==",
"funding": [ "funding": [
{ {
"type": "individual", "type": "individual",
@ -13858,9 +13858,9 @@
}, },
"dependencies": { "dependencies": {
"@ajayyy/maze-utils": { "@ajayyy/maze-utils": {
"version": "1.1.8", "version": "1.1.9",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.8.tgz", "resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.9.tgz",
"integrity": "sha512-0LwL+i/JvQZYBu6BadYX77XUoU0QVJZXCaElihVTO7suuZ9rracJX7w7A/p12whR/bQ2pO2bCqIvXuWoOiln0Q==" "integrity": "sha512-fU85QRrSBAP31ppFs+Gzfm9vHx+4wFFDr8pSoT747+w6L6cMPlubWG0lFquRTjffStfZlw33kGJOSuwXNiYbAA=="
}, },
"@ampproject/remapping": { "@ampproject/remapping": {
"version": "2.2.0", "version": "2.2.0",

View file

@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "background.js", "main": "background.js",
"dependencies": { "dependencies": {
"@ajayyy/maze-utils": "^1.1.8", "@ajayyy/maze-utils": "^1.1.9",
"content-scripts-register-polyfill": "^4.0.2", "content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"

View file

@ -45,6 +45,7 @@ import { getHash, HashedValue } from "@ajayyy/maze-utils/lib/hash";
import { generateUserID } from "@ajayyy/maze-utils/lib/setup"; import { generateUserID } from "@ajayyy/maze-utils/lib/setup";
import { setThumbnailListener, updateAll } from "@ajayyy/maze-utils/lib/thumbnailManagement"; import { setThumbnailListener, updateAll } from "@ajayyy/maze-utils/lib/thumbnailManagement";
import { labelThumbnails, setupThumbnailPageLoadListener } from "./utils/thumbnails"; import { labelThumbnails, setupThumbnailPageLoadListener } from "./utils/thumbnails";
import * as documentScript from "../dist/js/document.js";
const utils = new Utils(); const utils = new Utils();
@ -111,7 +112,8 @@ setupVideoModule({
updatePreviewBar(); updatePreviewBar();
updateVisibilityOfPlayerControlsButton(); updateVisibilityOfPlayerControlsButton();
}, },
resetValues resetValues,
documentScript
}, () => Config); }, () => Config);
setThumbnailListener(labelThumbnails); setThumbnailListener(labelThumbnails);
setupThumbnailPageLoadListener(); setupThumbnailPageLoadListener();

View file

@ -25,99 +25,168 @@ const edgeLanguages = [
"zh_CN" "zh_CN"
] ]
module.exports = env => ({
entry: {
popup: path.join(__dirname, srcDir + 'popup.ts'), module.exports = env => {
background: path.join(__dirname, srcDir + 'background.ts'), const documentScriptBuild = webpack({
content: path.join(__dirname, srcDir + 'content.ts'), entry: {
options: path.join(__dirname, srcDir + 'options.ts'), document: {
help: path.join(__dirname, srcDir + 'help.ts'), import: path.join(__dirname, srcDir + 'document.ts'),
permissions: path.join(__dirname, srcDir + 'permissions.ts'), },
document: path.join(__dirname, srcDir + 'document.ts'), },
upsell: path.join(__dirname, srcDir + 'upsell.ts') output: {
}, path: path.join(__dirname, '../dist/js'),
output: { },
path: path.join(__dirname, '../dist/js'), module: {
}, rules: [
optimization: {
splitChunks: {
name: 'vendor',
chunks: "initial"
}
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
// disable type checker for user in fork plugin
transpileOnly: true,
configFile: env.mode === "production" ? "tsconfig-production.json" : "tsconfig.json"
}
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
plugins: [
// fork TS checker
new ForkTsCheckerWebpackPlugin(),
// exclude locale files in moment
new CopyPlugin({
patterns: [
{ {
from: '.', test: /\.tsx?$/,
to: '../', loader: 'ts-loader',
globOptions: { exclude: /node_modules/,
ignore: ['manifest.json'], resourceQuery: { not: [/raw/] },
}, options: {
context: './public', // disable type checker for user in fork plugin
filter: async (path) => { transpileOnly: true,
if (path.match(/\/_locales\/.+/)) { configFile: env.mode === "production" ? "tsconfig-production.json" : "tsconfig.json"
if (env.browser.toLowerCase() === "edge"
&& !edgeLanguages.includes(path.match(/(?<=\/_locales\/)[^/]+(?=\/[^/]+$)/)[0])) {
return false;
}
const data = await fs.promises.readFile(path);
const parsed = JSON.parse(data.toString());
return parsed.fullName && parsed.Description;
} else {
return true;
}
},
transform(content, path) {
if (path.match(/\/_locales\/.+/)) {
const parsed = JSON.parse(content.toString());
if (env.browser.toLowerCase() === "safari") {
parsed.fullName.message = parsed.fullName.message.match(/^.+(?= -)/)?.[0] || parsed.fullName.message;
if (parsed.fullName.message.length > 50) {
parsed.fullName.message = parsed.fullName.message.slice(0, 47) + "...";
}
parsed.Description.message = parsed.Description.message.match(/^.+(?=\. )/)?.[0] || parsed.Description.message;
if (parsed.Description.message.length > 80) {
parsed.Description.message = parsed.Description.message.slice(0, 77) + "...";
}
}
return Buffer.from(JSON.stringify(parsed));
}
return content;
} }
},
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
plugins: [
// Don't fork TS checker for document script to speed up
// new ForkTsCheckerWebpackPlugin()
]
});
class DocumentScriptCompiler {
currentWatching = null;
/**
*
* @param {webpack.Compiler} compiler
*/
apply(compiler) {
compiler.hooks.beforeCompile.tapAsync({ name: 'DocumentScriptCompiler' }, (compiler, callback) => {
if (env.WEBPACK_WATCH) {
let first = true;
if (!this.currentWatching) {
this.currentWatching = documentScriptBuild.watch({}, () => {
if (first) {
first = false;
callback();
}
});
} else {
callback();
}
} else {
documentScriptBuild.close(() => {
documentScriptBuild.run(() => {
callback();
});
});
}
});
}
}
return {
entry: {
popup: path.join(__dirname, srcDir + 'popup.ts'),
background: path.join(__dirname, srcDir + 'background.ts'),
content: path.join(__dirname, srcDir + 'content.ts'),
options: path.join(__dirname, srcDir + 'options.ts'),
help: path.join(__dirname, srcDir + 'help.ts'),
permissions: path.join(__dirname, srcDir + 'permissions.ts'),
upsell: path.join(__dirname, srcDir + 'upsell.ts')
},
output: {
path: path.join(__dirname, '../dist/js'),
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
resourceQuery: { not: [/raw/] },
options: {
// disable type checker for user in fork plugin
transpileOnly: true,
configFile: env.mode === "production" ? "tsconfig-production.json" : "tsconfig.json"
}
},
{
test: /js\/document\.js$/,
type: 'asset/source'
} }
] ]
}), },
new BuildManifest({ resolve: {
browser: env.browser, extensions: ['.ts', '.tsx', '.js']
pretty: env.mode === "production", },
stream: env.stream plugins: [
}), // Prehook to start building document script before normal build
new configDiffPlugin() new DocumentScriptCompiler(),
] // fork TS checker
}); new ForkTsCheckerWebpackPlugin(),
// exclude locale files in moment
new CopyPlugin({
patterns: [
{
from: '.',
to: '../',
globOptions: {
ignore: ['manifest.json'],
},
context: './public',
filter: async (path) => {
if (path.match(/\/_locales\/.+/)) {
if (env.browser.toLowerCase() === "edge"
&& !edgeLanguages.includes(path.match(/(?<=\/_locales\/)[^/]+(?=\/[^/]+$)/)[0])) {
return false;
}
const data = await fs.promises.readFile(path);
const parsed = JSON.parse(data.toString());
return parsed.fullName && parsed.Description;
} else {
return true;
}
},
transform(content, path) {
if (path.match(/\/_locales\/.+/)) {
const parsed = JSON.parse(content.toString());
if (env.browser.toLowerCase() === "safari") {
parsed.fullName.message = parsed.fullName.message.match(/^.+(?= -)/)?.[0] || parsed.fullName.message;
if (parsed.fullName.message.length > 50) {
parsed.fullName.message = parsed.fullName.message.slice(0, 47) + "...";
}
parsed.Description.message = parsed.Description.message.match(/^.+(?=\. )/)?.[0] || parsed.Description.message;
if (parsed.Description.message.length > 80) {
parsed.Description.message = parsed.Description.message.slice(0, 77) + "...";
}
}
return Buffer.from(JSON.stringify(parsed));
}
return content;
}
}
]
}),
new BuildManifest({
browser: env.browser,
pretty: env.mode === "production",
stream: env.stream
}),
new configDiffPlugin()
]
};
};