Chrome Extension
說明
chrome extension component
- background scripts : event control
- content scripts : change website page
- options page
- UI elements
.crx
+manifest file(manifest.json)
search source file
- Source –> Ctrl + O : search file
modify manifest.json name&description
1 | { |
prepare for publish(move source to src,compress to .zip)
Extension other function
- options_page
- notifications
- Context Menu
Extension using react warning
exceed the recommended size limit
WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
webpack.config.js change size
1 | performance: { |
webpack.config.js disable size check
1 | performance: { |
tool
vscode setup
basic for JavaScript
install Prettier - Code formatter
default format(Workspaces)
Format On Save
some for Rectc & TypeScript
prettier single quote
prettier semi
Function
version 2
manifest.json
1 | { |
chrome storage(加入 storage 後,有時要移除再載入才會正常)
- Storage areas
- storage.local
- storage.sync
manifest.json
1 | { |
content.js
1 | window.onload = () => { |
script comminication/message passing
manifest.json
1 | { |
background.js
1 | import * as message from "./backgroundMessaging.js" |
backgroundMessaging.js
1 | // chrome.bookmarks.onMoved.addListener(() => { |
contentMessaging.js
1 | // window.onload(alert('I loaded')); |
result
- reload extension
- reflash YouTube page
- move bookmark
cross-origin http request(XHR)
manifest.json
1 | { |
contentRequests.js
1 | // nee github api permission(manifest.json |
result
internationalization(localization)
manifest.json
1 | { |
add message for English and Traditional Chinese
_locales/en/messages.json
1 | { |
_locales/zh_TW/messages.json
1 | { |
reload extension(default Traditional Chinese)
change language to english then reload extension
change button text(content.js)
1 | window.onload = () => { |
fix show 2 “DO IT DARK” button
contentMessaging.js
1 | // window.onload(alert('I loaded')); |
TypeScript build
install TypeScript module
1 | # install TypeScript |
change all .js files to .ts + fix some typescript error
background.ts
1 | // import * as message from "./backgroundMessaging.js" |
content.ts
1 | window.onload = () => { |
add tsconfig.json
1 | { |
add build command - package.json
1 | { |
build typescript
1 | npm run build |
Webpack
1 | # have some issue - just list need instal package |
Start
setup
install node.js
chrome://extensions –> on developer mode(開發人員模式)
1st project
init + install type
1 | npm init |
add manifest.json
1 | { |
load chrome extension from local
add background scripts - for event control
manifest.json
1 | { |
add background.js
1 | chrome.runtime.onInstalled.addListener(()=>{ |
reload and see result
add bookmarks event
background.js
1 | chrome.runtime.onInstalled.addListener(() => { |
reload(error)
manifest.json add permissions for bookmarks
1 | { |
clear error then reload
open new page + save bookmark –> see triger bookmarks.onCreated
add content scripts(add text “Dark mode”) - change website page
manifest.json add content_scripts
1 | { |
try Youtube add string “Dark mode”
add content.js
1 | window.onload = () => { |
reload extension
manifest.json add matches
1 | { |
reload then refresh youtube website
content scripts(add button, click trigger drak mode)
content.js
1 | window.onload = () => { |
manifest.json : add exclude_matches, run_at
1 | { |
user interface(change icon)
add icon
manifest.json : add icon
1 | { |
result
user interface(show title and popup window)
manifest.json
1 | { |
background.js
1 | chrome.runtime.onInstalled.addListener((tab) => { |
popup.html
1 |
|
result
user interface(add href button)
popup.html
1 |
|
result
Timer Basic
manifest.json
manifest_version(1)
name(1)
version(1)
description(1)
icons(1)
action(2)
options_page(3)
permissions(4)
background(5)
action
- popup.*
- setBadgeText
options_page
- options.*
storage
- permissions
- chrome.storage.sync.set
- chrome.storage.sync.get
- chrome.storage.local.set
- chrome.storage.local.get
background(sometime sleep)
- “service_worker”: “background.js”
alarm API(trigger continue run)
- permissions
- chrome.alarms.create
- chrome.alarms.onAlarm.addListener(
notifications API
- permissions
- this.registration.showNotification
manifest.json
1 | { |
background.js
1 | // console.log("Hello from the background script!"); |
popup
popup.html
1 |
|
popup.css
1 | body { |
popup.js
1 | const timeElement = document.getElementById("time"); |
options
options.html
1 |
|
options.css
1 | h1 { |
options.js
1 | // console.log("Hello from the options page!"); |
Pomotoro Timer
manifest.json
1 | { |
popup.*
popup.html
1 |
|
popup.css
1 | body { |
popup.js
1 | var tasks = []; |
options.*
options.html
1 |
|
options.css
1 | body { |
options.js
1 | const timeOption = document.getElementById("time-option"); |
TV Show
- background.js
- chrome.runtime
- chrome.contextMenus
- chrome.search
- chrome.tabs
- Content scripts
- Message
- chrome.runtime.sendMessage
- chrome.runtime.onMessage.addListener
- chrome.tabs.sendMessage
- Data Fetching:fetch()
- chrome.tts (synthesized text-to-speech)
manifest.json
1 | { |
background.js
1 | chrome.runtime.onInstalled.addListener((details) => { |
contentScript.js
1 | // console.log("Hello from the content script!"); |
contentScript.css
1 | body { |
popup.*
popup.html
1 |
|
popup.css
1 | body { |
popup.js
1 | // receive message from content(same as background) |
React Extension
說明
.ts vs .tsx
- .jsx 是javascript文件並表明使用了JSX語法。
- .ts 是typescript 文件的擴展名
- .tsx 表明是typescript 文件並使用了JSX語法。
npm init
1 | # npm init |
test simple TypeScript compile
install TypeScript
1 | # install typescript |
test.ts
1 | const test: string = 'hello' |
compile
1 | npx tsc *.ts |
test React TypeScript compile
install React
1 | # ======== course modify |
tsconfig.json
1 | { |
src/test.jsx
1 | import React from 'react' |
compile
1 | npx tsc *.ts |
React Extension Template- webpack
install package
1 | # npm init |
package.json
1 | { |
tsconfig.json : TypeScript configuration
1 | { |
webpack configuration
webpack.dev.js : development configuration
1 | const { merge } = require('webpack-merge') |
webpack.prod.js : production configuration
1 | const { merge } = require('webpack-merge') |
webpack.common.js : common configuration
1 | const path = require('path') |
popup
popup.tsx
1 | import React from 'react' |
popup.css
1 | body { |
options
options.tsx
1 | import React from 'react' |
options.css
1 | body { |
background.ts
1 | // console.log('Background Script') |
contentScript.ts
1 | // TODO: content script |
Weather Extersion
install
1 | npm i |
webpack.common.js
1 | const path = require('path') |
manifest.json
1 | { |
background.ts
1 | import { fetchOpenWeatherData } from '../utils/api' |
utils
api.ts
1 | import { OPEN_WEATHER_API_KEY } from './env' |
storage.ts
1 | import { OpenWeatherTempScale } from './api' |
messages.ts
1 | export enum Messages { |
components
WeatherCard.tsx
1 | import React, { useEffect, useState } from 'react' |
WearthCard.css
1 | .weatherCard-title { |
options
options.tsx
1 | import React, { useEffect, useState } from 'react' |
options.css
1 | body { |
popup
popup.tsx
1 | import React, { useEffect, useState } from 'react' |
popup.css
1 | body { |
contentScript
contentScript.tsx
1 | import React, { useEffect, useState } from 'react' |
contentScript.css
1 | .overlayCard { |
AdBlock Extension
說明
API
- chrome.webRequest : 分析,攔截,中斷,處理 traffic
install
1 | # remove popup and options |
version 2
manifest.json
1 | { |
background.ts
1 | chrome.webRequest.onBeforeRequest.addListener( |
version 3
manifest.json
1 | { |
rules_1.json
1 | [ |
version block by JS
manifest.json (“enabled”: false 不使用 declarativeNetRequest)
1 | { |
contentScript.tsx
1 | // remove ad by JavaScript |
1 | npm i --save-dev axios |
Ref
Basic ref
TV shwo extension ref
React extension ref
Weather Extension
AdBlock Extension
chrome extension publish
Common
publish