Initial commit

This commit is contained in:
Fergal Moran
2020-01-15 21:36:47 +00:00
commit 72c884dccd
16 changed files with 504 additions and 0 deletions

12
.editorconfig Normal file
View File

@@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2

2
.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
* text=auto eol=lf
*.ai binary

1
.github/funding.yml vendored Normal file
View File

@@ -0,0 +1 @@
custom: https://paypal.me/bytemode

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
node_modules
yarn.lock
distribution

15
.travis.yml Normal file
View File

@@ -0,0 +1,15 @@
language: node_js
node_js:
- stable
env:
# Extension ID assigned by the Chrome Web Store
- EXTENSION_ID='000000000000000000000000000000000'
deploy:
- provider: script
skip_cleanup: true
script: npm run release
on:
# On cron jobs https://docs.travis-ci.com/user/cron-jobs/
# When clicking "Trigger build" https://blog.travis-ci.com/2017-08-24-trigger-custom-build
branch: master
condition: (($TRAVIS_EVENT_TYPE = api) || ($TRAVIS_EVENT_TYPE = cron && $(git rev-list --since=yesterday HEAD)))

BIN
media/previewer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

53
package.json Normal file
View File

@@ -0,0 +1,53 @@
{
"private": true,
"scripts": {
"lint": "run-p lint:*",
"lint:js": "xo",
"lint:css": "stylelint source/**/*.css",
"lint-fix": "run-p 'lint:* -- --fix'",
"test": "run-s lint:* build",
"build": "webpack --mode=production",
"watch": "webpack --mode=development --watch",
"prerelease:version": "VERSION=$(utc-version); echo $VERSION; dot-json distribution/manifest.json version $VERSION",
"prerelease:source-url": "if [ ! -z \"${TRAVIS_REPO_SLUG}\" ]; then echo https://github.com/$TRAVIS_REPO_SLUG/tree/\"${TRAVIS_TAG:-$TRAVIS_COMMIT}\" > distribution/SOURCE_URL; fi",
"release": "npm-run-all build prerelease:* release:*",
"release:cws": "webstore upload --source=distribution --auto-publish",
"release:amo": "web-ext-submit --source-dir distribution"
},
"devDependencies": {
"chrome-webstore-upload-cli": "^1.2.0",
"copy-webpack-plugin": "^5.0.3",
"dot-json": "^1.1.0",
"eslint": "^6.1.0",
"eslint-config-xo": "^0.26.0",
"npm-run-all": "^4.1.5",
"size-plugin": "^1.2.0",
"stylelint": "^10.1.0",
"stylelint-config-xo": "^0.15.0",
"terser-webpack-plugin": "^1.3.0",
"utc-version": "^2.0.1",
"web-ext": "^3.1.1",
"web-ext-submit": "^3.1.1",
"webpack": "^4.36.1",
"webpack-cli": "^3.3.6",
"xo": "^0.24.0"
},
"dependencies": {
"webext-options-sync": "^0.21.2",
"webextension-polyfill": "^0.4.0"
},
"xo": {
"envs": [
"browser"
],
"ignores": [
"distribution"
],
"globals": [
"browser"
]
},
"stylelint": {
"extends": "stylelint-config-xo"
}
}

204
readme.md Normal file
View File

@@ -0,0 +1,204 @@
# browser-extension-template
[link-webext-polyfill]: https://github.com/mozilla/webextension-polyfill
[link-rgh]: https://github.com/sindresorhus/refined-github
[link-ngh]: https://github.com/sindresorhus/notifier-for-github
[link-hfog]: https://github.com/sindresorhus/hide-files-on-github
[link-tsconfig]: https://github.com/sindresorhus/tsconfig
[link-xo-ts]: https://github.com/xojs/eslint-config-xo-typescript
[link-options-sync]: https://github.com/fregante/webext-options-sync
[link-cws-keys]: https://github.com/DrewML/chrome-webstore-upload/blob/master/How%20to%20generate%20Google%20API%20keys.md
[link-amo-keys]: https://addons.mozilla.org/en-US/developers/addon/api/key
> Barebones boilerplate with webpack, options handler and auto-publishing.
![Sample extension output](media/previewer.png)
## Features
- Use modern Promise-based `browser.*` APIs [webextension-polyfill][link-webext-polyfill].
- [Auto-syncing options](#auto-syncing-options).
- [Auto-publishing](#publishing) with auto-versioning and support for manual releases.
- [Extensive configuration documentation](#configuration).
This extension template is heavily inspired by [refined-github][link-rgh], [notifier-for-github][link-ngh], and [hide-files-on-github][link-hfog] browser extensions. You can always refer to these browser extensions' source code if you find anything confusing on how to create a new extension.
## How to use this template
Click [<kbd>Use this template</kbd>](https://github.com/notlmn/browser-extension-template/generate) and make a copy of your own. 😉
## Configuration
The extension doesn't target any specific ECMAScript environment or provide any transpiling by default. The extensions output will be the same ECMAScript you write. This allows us to always target the latest browser version, which is a good practice you should be following.
### Webpack
#### Transpiling using Babel
The template bakes in a pretty basic webpack config, with no transpiling. To setup transpiling using Babel follow the following configuration steps.
1. Install Babel packages and respective loader for webpack.
``` sh
npm i --save-dev @babel/core @babel/preset-env babel-loader
```
1. In `webpack.config.js`, add the following rule to process JS files.
``` js
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
```
1. Target respective browsers using `.babelrc`.
``` json
{
"presets": [
["@babel/preset-env", {
"targets": {
"chrome": "74",
"firefox": "67"
}
}]
]
}
```
#### Extracting CSS
If you will be writing any code that will be importing CSS files from JS files, then you will be needing `mini-css-extract-plugin` to extract this imported CSS into its own file.
1. Install the webpack plugin.
``` sh
npm i --save-dev mini-css-extract-plugin
```
1. Modify the webpack config as mentioned to let this plugin handle CSS imports.
``` js
// Import plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// Under `module.rules`
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
}
// Under `plugins`
new MiniCssExtractPlugin({
filename: 'content.css'
})
```
#### TypeScript
TypeScript and Babel configs conflict each other, so you can only use one of these configuration types at any point.
1. Install TypeScript and respective loader for webpack
``` sh
npm i --save-dev typescript ts-loader @types/firefox-webext-browser
```
1. Use the following webpack rule in the config file.
``` js
{
test: /\.(js|ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/
},
```
1. Use the following as `tsconfig.json`, uses [sindresorhus/tsconfig][link-tsconfig] (install it as dependecy before using).
``` json
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"target": "esnext",
"declaration": false
},
"include": [
"source"
]
}
```
TypeScript requires additional configuration depending on how you set it up, like [linting][link-xo-ts].
### Auto-syncing options
Options are managed by [fregante/webext-options-sync][link-options-sync], which auto-saves and auto-restores the options form, applies defaults and runs migrations.
### Publishing
It's possible to publish to both the Chrome Web Store and Mozilla Addons at once by creating these ENV variables:
1. `CLIENT_ID`, `CLIENT_SECRET`, and `REFRESH_TOKEN` from [Google APIs][link-cws-keys].
1. `WEB_EXT_API_KEY`, and `WEB_EXT_API_SECRET` from [AMO][link-amo-keys].
And then running:
``` sh
npm run release
```
This will:
1. Build the extension
1. Create a version number based on the current UTC time, like [`19.6.16.428`](https://github.com/LinusU/utc-version#utc-version) and sets it in the manifest.json
1. Deploy it to both stores
#### Auto-publishing
Thanks to the included [Travis file](.travis.yml), if you set up those ENVs on Travis, the deployment will automatically happen:
- when clicking ["Trigger build"](https://blog.travis-ci.com/2017-08-24-trigger-custom-build)
- every day, if you configure the [Cron Job](https://docs.travis-ci.com/user/cron-jobs/) (but only if there are any new commits in the last day)
#### Auto-publishing on tags
If you prefer picking your versions and publishing on demand, replace the deployment in [Travis file](.travis.yml) with:
``` yml
- provider: script
skip_cleanup: true
script: npm run release
on:
tags: true
```
And then replace the `prerelease:version` script in [package.json](package.json) with:
``` json
"prerelease:version": "dot-json distribution/manifest.json version $TRAVIS_TAG",
```
And your extension will be published when creating a git tag, using the tag itself as version for the extension.
### License
This browser extension template is released under [MIT](#license) and mentioned below. There is no `license` file included in here, but when you clone this template, you should include your own license file for the specific license you choose to use.
## Credits
Extension icon made by [Freepik](https://www.freepik.com) from [www.flaticon.com](https://www.flaticon.com) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0).
## Extensions created using this template
- [notlmn/copy-as-markdown](https://github.com/notlmn/copy-as-markdown) - Browser extension to copy hyperlinks, images, and selected text as Markdown.
## License
[![CC0](https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/)

2
source/background.js Normal file
View File

@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-unassigned-import
import './options-storage';

BIN
source/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

31
source/manifest.json Normal file
View File

@@ -0,0 +1,31 @@
{
"name": "Awesome Extension",
"version": "0.0.0",
"description": "An awesome new browser extension",
"homepage_url": "https://github.com/awesome-templates/browser-extension-template",
"manifest_version": 2,
"minimum_chrome_version": "74",
"applications": {
"gecko": {
"id": "your-extension-name@your-domain.com",
"strict_min_version": "67.0"
}
},
"icons": {
"128": "icon.png"
},
"permissions": [
"storage"
],
"options_ui": {
"chrome_style": true,
"page": "options.html"
},
"background": {
"persistent": false,
"scripts": [
"browser-polyfill.min.js",
"background.js"
]
}
}

13
source/options-storage.js Normal file
View File

@@ -0,0 +1,13 @@
import OptionsSync from 'webext-options-sync';
export default new OptionsSync({
defaults: {
colorRed: 244,
colorGreen: 67,
colorBlue: 54
},
migrations: [
OptionsSync.migrations.removeUnused
],
logging: true
});

69
source/options.css Normal file
View File

@@ -0,0 +1,69 @@
html {
min-width: 550px;
overflow-x: hidden; /* Required to hide horizontal scroll on Firefox */
}
/* For use with screen readers */
.sr-only {
display: none;
}
/* Hide spinbox for number inputs */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type='number'] {
-moz-appearance: textfield;
}
input[type='number'],
input[type='text'] {
padding: 0.25rem 0.375rem;
}
.color-picker {
display: flex;
}
.color-inputs {
flex: 1;
}
.color-input {
display: flex;
padding: 0.25rem;
align-items: center;
}
.color-input input[type='range'] {
flex: 1;
margin: 0 0.5rem;
}
.color-input input[type='number'] {
width: calc(3ch + 1rem);
}
.color-output {
width: 86px;
height: 86px;
margin: 0.5rem;
}
/* Firefox only */
.only-firefox {
display: none;
}
@-moz-document url-prefix('') {
.only-firefox {
display: block;
}
}

33
source/options.html Normal file
View File

@@ -0,0 +1,33 @@
<!doctype html>
<meta charset="utf-8">
<title>Options</title>
<link rel="stylesheet" href="chrome://global/skin/in-content/common.css">
<link rel="stylesheet" href="options.css">
<form id="options-form" class="detail-view-container">
<h3>Color Previewer</h3>
<div class="color-picker">
<div class="color-inputs">
<label class="color-input">
<span>R</span>
<input type="range" min="0" max="255" name="colorRed">
<input type="number" name="colorRed" min="0" max="255">
</label>
<label class="color-input">
<span>G</span>
<input type="range" min="0" max="255" name="colorGreen">
<input type="number" name="colorGreen" min="0" max="255">
</label>
<label class="color-input">
<span>B</span>
<input type="range" min="0" max="255" name="colorBlue">
<input type="number" name="colorBlue" min="0" max="255">
</label>
</div>
<div class="color-output">
</div>
</div>
</form>
<script src="browser-polyfill.min.js"></script>
<script src="options.js"></script>

22
source/options.js Normal file
View File

@@ -0,0 +1,22 @@
import optionsStorage from './options-storage';
optionsStorage.syncForm('#options-form');
const rangeInputs = [...document.querySelectorAll('input[type="range"][name^="color"]')];
const numberInputs = [...document.querySelectorAll('input[type="number"][name^="color"]')];
const output = document.querySelector('.color-output');
function updateColor() {
output.style.backgroundColor = `rgb(${rangeInputs[0].value}, ${rangeInputs[1].value}, ${rangeInputs[2].value})`;
}
function updateInputField(event) {
numberInputs[rangeInputs.indexOf(event.currentTarget)].value = event.currentTarget.value;
}
for (const input of rangeInputs) {
input.addEventListener('input', updateColor);
input.addEventListener('input', updateInputField);
}
window.addEventListener('load', updateColor);

44
webpack.config.js Normal file
View File

@@ -0,0 +1,44 @@
const path = require('path');
const SizePlugin = require('size-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
devtool: 'sourcemap',
stats: 'errors-only',
entry: {
background: './source/background',
options: './source/options'
},
output: {
path: path.join(__dirname, 'distribution'),
filename: '[name].js'
},
plugins: [
new SizePlugin(),
new CopyWebpackPlugin([
{
from: '**/*',
context: 'source',
ignore: '*.js'
},
{
from: 'node_modules/webextension-polyfill/dist/browser-polyfill.min.js'
}
])
],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
compress: false,
output: {
beautify: true,
indent_level: 2 // eslint-disable-line camelcase
}
}
})
]
}
};