From 56b73f1baaceaa8ab4f301fac26ce0a6ca4653be Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 12:25:16 +0200 Subject: [PATCH 1/7] adding more detailed conversion to v2 --- app.config.json | 8 +++++++- package.json | 4 +++- tools/deploy.ts | 30 ++++++++++++++++-------------- tools/v2.ts | 31 ++++++++++++++++++++++++++++++- tsconfig.json | 4 ++-- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/app.config.json b/app.config.json index 12a5b21..e5ceac0 100644 --- a/app.config.json +++ b/app.config.json @@ -19,5 +19,11 @@ "email": "info@josunlp.de" } ] - } + }, + "htmlTemplatePairs": [ + { + "key": "{{BET}}", + "value": "Browser Extension Template" + } + ] } \ No newline at end of file diff --git a/package.json b/package.json index 59489a7..76ea2e9 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "deploy-v2": "npm run deploy-v3 && node ./tools/v2.js", "build-js": "tsc -p tsconfig.json", "build-css": "sass ./src/sass/:./dist/css/", - "build-tooling": "tsc ./tools/v2.ts --target esnext --module esnext && tsc ./tools/syncConfig.ts --target esnext --module esnext && tsc ./tools/deploy.ts --target esnext --module esnext", + "build-tooling": "tsc ./tools/v2.ts --target esnext --module esnext --lib ESNext && tsc ./tools/syncConfig.ts --target esnext --module esnext --lib ESNext && tsc ./tools/deploy.ts --target esnext --module esnext --lib ESNext", + "watch-ts": "tsc -w -p tsconfig.json", + "watch-sass": "sass --watch ./src/sass/:./dist/css/", "sync": "npm run build-tooling && node ./tools/syncConfig.js" }, "devDependencies": { diff --git a/tools/deploy.ts b/tools/deploy.ts index 215cf0f..bae6d22 100644 --- a/tools/deploy.ts +++ b/tools/deploy.ts @@ -6,7 +6,7 @@ var DEPLOY_TARGET = "./dist/"; function deleteFolderRecursive(path: string) { if (fs.existsSync(path)) { - fs.readdirSync(path).forEach(function(file: string) { + fs.readdirSync(path).forEach(function (file: string) { var curPath = path + "/" + file; if (fs.lstatSync(curPath).isDirectory()) { deleteFolderRecursive(curPath); @@ -21,7 +21,7 @@ function deleteFolderRecursive(path: string) { function findHtmlFilesRecursive(source: string): string[] { var files: string[] = []; var dir = fs.readdirSync(source); - dir.forEach(function(file: any) { + dir.forEach(function (file: any) { var sourceFile = path.join(source, file); var stat = fs.lstatSync(sourceFile); if (stat.isDirectory()) { @@ -35,33 +35,35 @@ function findHtmlFilesRecursive(source: string): string[] { return files; } -function replaceKeywordsInHtmlFile(file: string, keywords: string[], values: string[]) { +function replaceKeywordsInHtmlFile(file: string) { var content = fs.readFileSync(file, 'utf8'); - for (var i = 0; i < keywords.length; i++) { - content = content.replace(new RegExp(keywords[i], 'g'), values[i]); - } + let pairs = appConfig.htmlTemplatePairs; + pairs.forEach(function (pair: any) { + // @ts-ignore + content = content.replaceAll(pair.key, pair.value); + }); file = file.replace("public\\", DEPLOY_TARGET); fs.writeFileSync(file, content); } -function buildHtmlFiles(source: string, keywords: string[], values: string[]) { - var files = findHtmlFilesRecursive(source); - files.forEach(function(file: string) { - replaceKeywordsInHtmlFile(file, keywords, values); +function buildHtmlFiles(source: string) { + let files = findHtmlFilesRecursive(source); + files.forEach(function (file: string) { + replaceKeywordsInHtmlFile(file); }); } function mkdirSync(path: string) { try { fs.mkdirSync(path); - } catch(e: any) { - if ( e.code != 'EEXIST' ) throw e; + } catch (e: any) { + if (e.code != 'EEXIST') throw e; } } function copyFiles(source: string, target: string) { var files = fs.readdirSync(source); - files.forEach(function(file: any) { + files.forEach(function (file: any) { var sourceFile = path.join(source, file); var targetFile = path.join(target, file); var stat = fs.lstatSync(sourceFile); @@ -77,6 +79,6 @@ function copyFiles(source: string, target: string) { deleteFolderRecursive(DEPLOY_TARGET); mkdirSync(DEPLOY_TARGET); copyFiles(DEPLOY_ENTRY, DEPLOY_TARGET); -buildHtmlFiles(DEPLOY_ENTRY, ["{{BET}}"], [appConfig.AppData.name]); +buildHtmlFiles(DEPLOY_ENTRY); console.log("Deployed to " + DEPLOY_TARGET); \ No newline at end of file diff --git a/tools/v2.ts b/tools/v2.ts index 60d5ea8..35a0af8 100644 --- a/tools/v2.ts +++ b/tools/v2.ts @@ -2,6 +2,35 @@ import * as fs from 'fs'; const manifest = JSON.parse(fs.readFileSync('./dist/manifest.json', 'utf8')); -manifest.manifest_version = 2; +manifest.manifest_version = 2 + +manifest.background.scripts = [] + +manifest.background.scripts.push(manifest.background.service_worker) +delete manifest.background.type +delete manifest.background.service_worker +manifest.background.persistent = true +manifest.permissions += manifest.host_permissions +manifest.permissions += manifest.optional_host_permissions +delete manifest.host_permissions +delete manifest.optional_host_permissions + +let newContentSecurityPolicy = "" + +try { + for (const policy of manifest.content_security_policy) { + newContentSecurityPolicy += policy + " " + } +} catch (e) { + newContentSecurityPolicy = "" +} + +manifest.content_security_policy = newContentSecurityPolicy + +try { + manifest.web_accessible_resources = manifest.web_accessible_resources.resources +} catch (e) { + manifest.web_accessible_resources = [] +} fs.writeFileSync('./dist/manifest.json', JSON.stringify(manifest, null, 2)); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2e39850..fe9081d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,9 +4,9 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ + "target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ "module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ - // "lib": [], /* Specify library files to be included in the compilation. */ + // "lib": ["ESNext"], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ From 1c765be88fd58b411bc9d6f33ef7deacde487f1c Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 12:42:28 +0200 Subject: [PATCH 2/7] fixing v2 conversion --- tools/v2.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/v2.ts b/tools/v2.ts index 35a0af8..dd42ddb 100644 --- a/tools/v2.ts +++ b/tools/v2.ts @@ -10,8 +10,12 @@ manifest.background.scripts.push(manifest.background.service_worker) delete manifest.background.type delete manifest.background.service_worker manifest.background.persistent = true -manifest.permissions += manifest.host_permissions -manifest.permissions += manifest.optional_host_permissions +if (manifest.host_permissions) { + manifest.permissions.push(manifest.host_permissions) +} +if (manifest.optional_host_permissions) { + manifest.permissions.push(manifest.optional_host_permissions) +} delete manifest.host_permissions delete manifest.optional_host_permissions @@ -19,10 +23,10 @@ let newContentSecurityPolicy = "" try { for (const policy of manifest.content_security_policy) { - newContentSecurityPolicy += policy + " " + newContentSecurityPolicy += policy.key + "'" + policy.value + "'" + " " } } catch (e) { - newContentSecurityPolicy = "" + newContentSecurityPolicy = "default-src 'self'" } manifest.content_security_policy = newContentSecurityPolicy @@ -33,4 +37,9 @@ try { manifest.web_accessible_resources = [] } +if (manifest.action) { + manifest.browser_action = manifest.action +} +delete manifest.action + fs.writeFileSync('./dist/manifest.json', JSON.stringify(manifest, null, 2)); \ No newline at end of file From 59fca4e2aa3184153d38d98807af7368d5889d6d Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 13:00:44 +0200 Subject: [PATCH 3/7] Updating readme --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cdf2414..08127a5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ [![GitHub forks](https://img.shields.io/github/forks/JosunLP/BrowserExtensionTemplate?style=for-the-badge)](https://github.com/JosunLP/BrowserExtensionTemplate/network) [![GitHub stars](https://img.shields.io/github/stars/JosunLP/BrowserExtensionTemplate?style=for-the-badge)](https://github.com/JosunLP/BrowserExtensionTemplate/stargazers) [![GitHub license](https://img.shields.io/github/license/JosunLP/BrowserExtensionTemplate?style=for-the-badge)](https://github.com/JosunLP/BrowserExtensionTemplate) -![Twitter URL](https://img.shields.io/twitter/url?color=blue&logo=Twitter&style=for-the-badge&url=https%3A%2F%2Fgithub.com%2FJosunLP%2FBrowserExtensionTemplate) +[![Twitter URL](https://img.shields.io/twitter/url?logo=twitter&style=for-the-badge&url=https%3A%2F%2Fgithub.com%2FJosunLP%2FBrowserExtensionTemplate)](https://twitter.com/intent/tweet?text=Look+what+i+found+on+GitHub+%23Developer%2C+%23SoftwareDeveloper%3A&url=https%3A%2F%2Fgithub.com%2FJosunLP%2FBrowserExtensionTemplate) + +## Description A basic template based on SASS and TypeScript to create browser extensions without directly relying on a larger framework. @@ -20,3 +22,15 @@ Your sourcecode can be written in the `src` folder. The `public` folder contains With the `npm run deploy-v3` command you can deploy the extension to the dist folder, ready to be published to the chrome web store. With the `npm run deploy-v2` command you can deploy the extension to the dist folder, ready to be published to the firefox web store. This is necessary because the firefox web store needs the `manifest.json` file to be present in the version v2. + +## License + +This project is licensed under the [MIT license](https://opensource.org/licenses/MIT). + +## Contributing + +This project is open source. Feel free to fork and contribute! + +## Author + +Jonas Pfalzgraf From dbd1691445f9df9abe6e080fbc6be1fd5903faeb Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 13:11:27 +0200 Subject: [PATCH 4/7] cleaning --- README.md | 2 ++ tools/deploy.ts | 31 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 08127a5..d9c9633 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ [![GitHub stars](https://img.shields.io/github/stars/JosunLP/BrowserExtensionTemplate?style=for-the-badge)](https://github.com/JosunLP/BrowserExtensionTemplate/stargazers) [![GitHub license](https://img.shields.io/github/license/JosunLP/BrowserExtensionTemplate?style=for-the-badge)](https://github.com/JosunLP/BrowserExtensionTemplate) [![Twitter URL](https://img.shields.io/twitter/url?logo=twitter&style=for-the-badge&url=https%3A%2F%2Fgithub.com%2FJosunLP%2FBrowserExtensionTemplate)](https://twitter.com/intent/tweet?text=Look+what+i+found+on+GitHub+%23Developer%2C+%23SoftwareDeveloper%3A&url=https%3A%2F%2Fgithub.com%2FJosunLP%2FBrowserExtensionTemplate) +[![CodeFactor](https://www.codefactor.io/repository/github/josunlp/browserextensiontemplate/badge?style=for-the-badge)](https://www.codefactor.io/repository/github/josunlp/browserextensiontemplate) +[![Known Vulnerabilities](https://snyk.io/test/github/JosunLP/BrowserExtensionTemplate/badge.svg?style=for-the-badge)](https://snyk.io/test/github/JosunLP/BrowserExtensionTemplate) ## Description diff --git a/tools/deploy.ts b/tools/deploy.ts index bae6d22..c296683 100644 --- a/tools/deploy.ts +++ b/tools/deploy.ts @@ -1,13 +1,14 @@ +import { ExecException } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; const appConfig = JSON.parse(fs.readFileSync('./app.config.json', 'utf8')); -var DEPLOY_ENTRY = "./public/"; -var DEPLOY_TARGET = "./dist/"; +const DEPLOY_ENTRY = "./public/"; +const DEPLOY_TARGET = "./dist/"; function deleteFolderRecursive(path: string) { if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function (file: string) { - var curPath = path + "/" + file; + const curPath = path + "/" + file; if (fs.lstatSync(curPath).isDirectory()) { deleteFolderRecursive(curPath); } else { @@ -19,11 +20,11 @@ function deleteFolderRecursive(path: string) { } function findHtmlFilesRecursive(source: string): string[] { - var files: string[] = []; - var dir = fs.readdirSync(source); - dir.forEach(function (file: any) { - var sourceFile = path.join(source, file); - var stat = fs.lstatSync(sourceFile); + let files: string[] = []; + const dir = fs.readdirSync(source); + dir.forEach(function (file: string) { + const sourceFile = path.join(source, file); + const stat = fs.lstatSync(sourceFile); if (stat.isDirectory()) { files = files.concat(findHtmlFilesRecursive(sourceFile)); } else { @@ -36,9 +37,9 @@ function findHtmlFilesRecursive(source: string): string[] { } function replaceKeywordsInHtmlFile(file: string) { - var content = fs.readFileSync(file, 'utf8'); + const content = fs.readFileSync(file, 'utf8'); let pairs = appConfig.htmlTemplatePairs; - pairs.forEach(function (pair: any) { + pairs.forEach(function (pair: object) { // @ts-ignore content = content.replaceAll(pair.key, pair.value); }); @@ -62,11 +63,11 @@ function mkdirSync(path: string) { } function copyFiles(source: string, target: string) { - var files = fs.readdirSync(source); - files.forEach(function (file: any) { - var sourceFile = path.join(source, file); - var targetFile = path.join(target, file); - var stat = fs.lstatSync(sourceFile); + const files = fs.readdirSync(source); + files.forEach(function (file: string) { + const sourceFile = path.join(source, file); + const targetFile = path.join(target, file); + const stat = fs.lstatSync(sourceFile); if (stat.isDirectory()) { mkdirSync(targetFile); copyFiles(sourceFile, targetFile); From 27fbfd96c2b45b9c5388b996d3e036e1807cb551 Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 13:13:08 +0200 Subject: [PATCH 5/7] cleaning --- tools/deploy.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/deploy.ts b/tools/deploy.ts index c296683..a02061b 100644 --- a/tools/deploy.ts +++ b/tools/deploy.ts @@ -1,4 +1,3 @@ -import { ExecException } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; const appConfig = JSON.parse(fs.readFileSync('./app.config.json', 'utf8')); @@ -48,7 +47,7 @@ function replaceKeywordsInHtmlFile(file: string) { } function buildHtmlFiles(source: string) { - let files = findHtmlFilesRecursive(source); + const files = findHtmlFilesRecursive(source); files.forEach(function (file: string) { replaceKeywordsInHtmlFile(file); }); From 08e9b8a569fcf6c6ab3150f54a3d6c20581b3b32 Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 13:14:00 +0200 Subject: [PATCH 6/7] fix --- tools/deploy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/deploy.ts b/tools/deploy.ts index a02061b..8b6b7b0 100644 --- a/tools/deploy.ts +++ b/tools/deploy.ts @@ -37,7 +37,7 @@ function findHtmlFilesRecursive(source: string): string[] { function replaceKeywordsInHtmlFile(file: string) { const content = fs.readFileSync(file, 'utf8'); - let pairs = appConfig.htmlTemplatePairs; + const pairs = appConfig.htmlTemplatePairs; pairs.forEach(function (pair: object) { // @ts-ignore content = content.replaceAll(pair.key, pair.value); From e41c24a4b1c6d5053a3b082f9341dda56dc4f5ee Mon Sep 17 00:00:00 2001 From: Jonas Pfalzgraf Date: Tue, 16 Aug 2022 13:16:46 +0200 Subject: [PATCH 7/7] removing old workflow --- .github/workflows/codeql-analysis.yml | 71 --------------------------- 1 file changed, 71 deletions(-) delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 37bec9c..0000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,71 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '28 16 * * 0' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ 'javascript' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] - # Learn more: - # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1