chrome扩展开发之hello world

忘是亡心i 2023-01-19 08:28 294阅读 0赞

chrome扩展开发之hello world


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

文章目录

  • chrome扩展开发之hello world
      1. 功能描述
      1. 编写Manifest文件manifest.json
      1. 编写html文件hello.html
      1. 编写JavaScript文件hello.js
      1. 调试
      • 5.1 查看扩展程序信息
      • 5.2 审查扩展程序的弹出内容
      • 5.3 设置断点,单步调试
      • 5.4 重新加载页面进行调试
      1. 运行
      • 6.1 本地运行
      • 6.2 打包ctx上传chrome应用商店

1. 功能描述

点击browser action按钮弹出页面,显示内容Hello Extensions 和 当前的时间。

  1. $ tree
  2. .
  3. +--- hello.html
  4. +--- hello.js
  5. +--- manifest.json

2. 编写Manifest文件manifest.json

  1. {
  2. "name": "Hello Extensions",
  3. "description": "Base Level Extension",
  4. "version": "1.0",
  5. "browser_action": {
  6. "default_popup": "hello.html"
  7. },
  8. "manifest_version": 2
  9. }

3. 编写html文件hello.html

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>Hello Extensions</h1>
  5. <h1 id="time"></h1>
  6. </body>
  7. <script src="hello.js"></script>
  8. </html>

4. 编写JavaScript文件hello.js

  1. // 对Date的扩展,将 Date 转化为指定格式的String
  2. // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  3. // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  4. // 例子:
  5. // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  6. // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  7. Date.prototype.Format = function (fmt) {
  8. var o = {
  9. "M+": this.getMonth() + 1, //月份
  10. "d+": this.getDate(), //日
  11. "h+": this.getHours(), //小时
  12. "m+": this.getMinutes(), //分
  13. "s+": this.getSeconds(), //秒
  14. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  15. "S": this.getMilliseconds() //毫秒
  16. };
  17. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  18. for (var k in o)
  19. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  20. return fmt;
  21. }
  22. document.getElementById("time").innerHTML = (new Date()).Format("yyyy-MM-dd hh:mm:ss.S");

5. 调试

5.1 查看扩展程序信息

  • 如果你的扩展程序还没有运行,先去加载。如果已经在运行,你会在浏览器的地址栏右侧看到扩展程序图标 。如果扩展程序没有在运行, 要先去加载你的扩展程序
  • 确认一下扩展程序页面(chrome://extensions)上已经勾选开发者模式
  • 页面上可以看到扩展程序的信息,比如名称、描述、和 ID 等

5.2 审查扩展程序的弹出内容

在扩展程序图标上右键并选择【审查弹出内容】。

5.3 设置断点,单步调试

5.4 重新加载页面进行调试

在控制台Console的输入提示符(”>”)后面输入

  1. > location.reload(true)

6. 运行

6.1 本地运行

chrome://extensions 中点击【加载已解压的扩展程序】

6.2 打包ctx上传chrome应用商店

chrome://extensions 中点击【打包扩展程序】,即可生成ctx


License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎


Reference:

  1. https://github.com/GoogleChrome/chrome-extensions-samples
  2. https://developer.chrome.com/docs/extensions/mv2/getstarted
  3. http://open.chrome.360.cn/extension\_dev/overview.html

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RxJava Hello World

    原理介绍 什么是RxJava RxJava 就是异步 RxJava 的异步实现,是通过一种扩展的观察者模式来实现的。 一个响应式编程框架 观察者