Skip to content

Commit

Permalink
✨ feat: pack chrome extension
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Nov 6, 2023
1 parent 6f7a811 commit 475cc49
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 34 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ jobs:
run: yarn install --frozen-lockfile
- name: Check types and Build
run: yarn tsc --noEmit && NODE_ENV=PRODUCTION yarn run build
- name: Upload Canary Userscript
- name: Pack Chrome Extension
run: yarn run extension-pack
- name: Upload Userscript And Extension
uses: actions/upload-artifact@v3
with:
name: Canary Userscript
path: dist/chibi.user.js
name: Userscript and Unpacked Extension
path: |
dist/chibi.user.js
dist/unpacked-extension.zip
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ yarn-error.log

# generated build files
/dist
/extension/scripts/chibi.user.js
Binary file added extension/images/128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extension/images/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extension/images/24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extension/images/48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"manifest_version":2,"name":"Chibi","author":"SimonShiki","description":"Load scratch extension everywhere.","version":"5","content_scripts":[{"js":["scripts/content-script.js"],"matches":["https://scratch.mit.edu/projects/*","https://aerfaying.com/Projects/*","https://www.ccw.site/*","https://gitblock.cn/Projects/*","https://world.xiaomawang.com/*","https://cocrea.world/*","https://create.codelab.club/*","https://www.scratch-cn.cn/*","https://www.40code.com/*","https://turbowarp.org/*","https://codingclip.com/*","https://editor.turbowarp.cn/*","https://0832.ink/rc/*","https://code.xueersi.com/scratch3/*","https://code.xueersi.com/home/project/detail?lang=scratch&pid=*&version=3.0&langType=scratch","http://localhost:8601/*"],"run_at":"document_start"}],"web_accessible_resources":["scripts/chibi.user.js"]}
4 changes: 4 additions & 0 deletions extension/scripts/content-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const scriptElem = document.createElement('script');
scriptElem.setAttribute('type', 'text/javascript');
scriptElem.setAttribute('src', chrome.extension.getURL('/scripts/chibi.user.js'));
(document.head || document.documentElement).appendChild(scriptElem);
22 changes: 22 additions & 0 deletions generate-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const includeURLs = [
'https://scratch.mit.edu/projects/*',
'https://aerfaying.com/Projects/*',
'https://www.ccw.site/*',
'https://gitblock.cn/Projects/*',
'https://world.xiaomawang.com/*',
'https://cocrea.world/*',
'https://create.codelab.club/*',
'https://www.scratch-cn.cn/*',
'https://www.40code.com/*',
'https://turbowarp.org/*',
'https://codingclip.com/*',
'https://editor.turbowarp.cn/*',
'https://0832.ink/rc/*',
'https://code.xueersi.com/scratch3/*',
'https://code.xueersi.com/home/project/detail?lang=scratch&pid=*&version=3.0&langType=scratch',
'http://localhost:8601/*'
];

module.exports = {
includeURLs
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"private": true,
"scripts": {
"build": "NODE_ENV=production webpack --color --bail",
"dev": "NODE_ENV=development webpack --color --bail",
"extension:pack": "node ./scripts/generate-extension.js",
"lint": "eslint ./src/ --ext .js,.ts,tsx,jsx",
"typecheck": "tsc --watch --noEmit"
},
Expand All @@ -20,6 +20,7 @@
"@turbowarp/types": "^0.0.11",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"archiver": "^6.0.1",
"babel-loader": "^9.1.3",
"codingclip-worker-loader": "^3.0.9",
"eslint": "^8.49.0",
Expand Down
74 changes: 74 additions & 0 deletions scripts/generate-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { includeURLs } = require('../generate-helper');
const packageJSON = require('../package.json');
const fs = require('fs');
const path = require('path');
const process = require('node:process');
const archiver = require('archiver');

const env = process.env.BROSWER ?? 'chrome';

const manifest = {
manifest_version: 2,
name: packageJSON.displayName,
author: packageJSON.author,
description: packageJSON.description,
version: packageJSON.version,
/*
icons: {
16: 'images/16.png',
32: 'images/32.png',
48: 'images/48.png',
128: 'images/128.png'
},
*/
content_scripts: [
{
js: [
'scripts/content-script.js'
],
matches: includeURLs,
run_at: 'document_start'
}
],
web_accessible_resources: [
'scripts/chibi.user.js'
]
};

/**
* @param {String} sourceDir: /some/folder/to/compress
* @param {String} outPath: /path/to/created.zip
* @returns {Promise}
*/
function zipDirectory (sourceDir, outPath) {
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(outPath);

return new Promise((resolve, reject) => {
archive
.directory(sourceDir, false)
.on('error', err => reject(err))
.pipe(stream)
;

stream.on('close', () => resolve());
archive.finalize();
});
}

(async function pack () {
console.log(`Packing extension for ${env}`);
fs.copyFileSync(
path.resolve(__dirname, '../dist/chibi.user.js'),
path.resolve(__dirname, '../extension/scripts/chibi.user.js')
);
fs.writeFileSync(
path.resolve(__dirname, '../extension/manifest.json'),
JSON.stringify(manifest)
);
zipDirectory(
path.resolve(__dirname, '../extension/'),
path.resolve(__dirname, `../dist/unpacked-extension.zip`)
);
console.log('Done');
})();
30 changes: 4 additions & 26 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,10 @@ const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { UserscriptPlugin } = require('webpack-userscript');
const { includeURLs } = require('./generate-helper');
const packageJSON = require('./package.json');
const process = require('node:process');

function getMatchURL () {
const url = [
'https://scratch.mit.edu/projects/*',
'https://aerfaying.com/Projects/*',
'https://www.ccw.site/*',
'https://gitblock.cn/Projects/*',
'https://world.xiaomawang.com/*',
'https://cocrea.world/*',
'https://create.codelab.club/*',
'https://www.scratch-cn.cn/*',
'https://www.40code.com/*',
'https://turbowarp.org/*',
'https://codingclip.com/*',
'https://editor.turbowarp.cn/*',
'https://0832.ink/rc/*',
'https://code.xueersi.com/scratch3/*',
'https://code.xueersi.com/home/project/detail?lang=scratch&pid=*&version=3.0&langType=scratch'
];
if (process.env.NODE_ENV === 'development') {
url.unshift('http://localhost:8601/*');
return { include: url };
}
return { match: url };
}
const base = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
entry: './src/index.ts',
Expand Down Expand Up @@ -74,7 +51,7 @@ const base = {
},
plugins: [
new UserscriptPlugin({
headers: Object.assign({
headers: {
name: packageJSON.displayName,
author: packageJSON.author,
namespace: 'ScratchChibiLoader',
Expand All @@ -84,7 +61,8 @@ const base = {
license: packageJSON.license,
grant: ['none'],
'run-at': 'document-start',
}, getMatchURL()),
include: includeURLs
},
pretty: true,
strict: true,
whitelist: true
Expand Down
Loading

0 comments on commit 475cc49

Please sign in to comment.