Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
chrome 插件开发
Search
edokeh
April 23, 2012
Programming
7
750
chrome 插件开发
edokeh
April 23, 2012
Tweet
Share
More Decks by edokeh
See All by edokeh
Backbone 开发实战
edokeh
0
210
新一站 HTML5 触屏版开发总结
edokeh
34
8.5k
模块化的Javascript开发-FTF
edokeh
16
1.2k
iphone webapp入门实战
edokeh
14
840
REST实践指南
edokeh
12
650
Other Decks in Programming
See All in Programming
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
310
オブザーバビリティ駆動開発って実際どうなの?
yohfee
3
690
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
240
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
610
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
480
日本だけで解禁されているアプリ起動の方法
ryunakayama
0
370
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
120
go directiveを最新にしすぎないで欲しい話──あるいは、Go 1.26からgo mod initで作られるgo directiveの値が変わる話 / Go 1.26 リリースパーティ
arthur1
2
470
文字コードの話
qnighy
43
17k
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.4k
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
520
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
210
Featured
See All Featured
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
230
Building Adaptive Systems
keathley
44
2.9k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
67
37k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
810
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
The Language of Interfaces
destraynor
162
26k
The Spectacular Lies of Maps
axbom
PRO
1
580
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Odyssey Design
rkendrick25
PRO
2
540
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Bash Introduction
62gerente
615
210k
Transcript
Chrome Extension开发入门 葛亮@焦点科技 2012.4.23
简介 • 扩充Chrome功能 – 增强Chrome界面 – 修改页面 • 使用HTML/Javascript/CSS编写 –
可以使用HTML5/ES5/CSS3 • Chrome提供辅助API • 自动更新
My Chrome Extensions
插件架构 popup popup page background page page action browser action
content scripts web page Chrome Extension API
文件结构 .crx 压缩文件夹 manifest.json 配置文件 *.html *.js *.css *.png *.jpg
*.gif 其他文件
manifest.json { // 必需 "name" : "my extension", "version" :
"0.1", "manifest_version" : 2, // 推荐 "description" : "It’s nice", "icons": { "16" : "icon16.png", "48" : "icon48.png", "128" : "icon128.png“ } }
Startup • 新建空文件夹 • 新建并编写manifest.json • 通过Chrome“载入正在开发的扩展程序”
例子1:员工查询 • 架构 popup popup page web page OA服务器 Ajax交互
例子1:员工查询 1. 准备工作 抓http包
例子1:员工查询 2. 添加带popup页面的按钮 使用browser action 在工具栏添加按钮 manifest.json { … …
"browser_action" : { "default_icon" : "icon.png", "default_title" : "焦点员工通讯录", "default_popup" : "popup.html" } }
例子1:员工查询 3. 使用跨域ajax 添加权限 书写代码 manifest.json { … … "permissions"
: [ "http://oa.vemic.com/*" ] }
例子2:BBS通知 • 架构 popup popup page background page web page
服务器 Ajax交互 修改badge 浏览器的 WebDB
例子2:BBS通知 1. 编写服务器端 2. 添加带popup页面的按钮
例子2:BBS通知 3. 添加background页面 – 长生命周期的js – 所有Chrome进程共享一份 – 一般用于执行后台任务 manifest.json
{ … … "background" : { "scripts" : ["background.js"] } }
例子2:BBS通知 4. 修改badge – 图标上的数字徽章 – 使用Chrome Extension API •
chrome.browserAction.setBadgeText(); • chrome.browserAction.setBadgeBackgroundColor();
例子3:屏蔽选择提示 • 架构 content scripts web page 修改页面DOM
例子3:屏蔽选择提示 • 添加Content Scripts – 运行于特定页面上的脚本 – 不能使用Chrome Extension API
– 运行于“沙箱”中,不用担心全局变量污染 manifest.json { … … "content_scripts" : [ { "matches" : ["http://baidu.com/*"], "js" : ["sth.js"], "css" : ["sth.css"] } ] }
例子3:屏蔽选择提示 • 代码实现 – 针对网易新闻 var ne = document.getElementById("btn_wrapper"); ne.parentNode.removeChild(ne);
– 针对新浪微博 #selectionShare img{ display: none; } – 配置文件
debug • background • popup • content scripts
打包发布
自动更新 manifest.json { … … "update_url" : "http://myhost.com/ext/updates.xml" }
编写技巧 • 代码只需兼容Chrome即可 所以… 让浏览器兼容性玩儿蛋去吧!
编写技巧 • 使用HTML5/CSS3/ECMAScript5 – 使用ES5 – 使用HTML5 • 参见例子2 HTML5
WebDB • 抛弃jQuery document.querySelectorAll(selector) – 使用CSS3
Q&A