简单地说,浏览器插件,能够大大的扩展你的浏览器的功能。包括但不只限于这些功能:html
浏览器插件的弊端:会带来一些安全隐患,也可能让你的浏览器变得缓慢甚至不稳定。 git
插件地址:https://github.com/shengulong/chrome_plugingithub
一、chrome地址栏输入chrome://extensions/,能够看到当前谷歌浏览器的所有扩展程序web
开启开发者模式,能够加载本身制做的扩展程序,或者把本身制做的扩展程序打包chrome
打包第一个扩展程序,生成crx插件和pem密钥,这样别人就能够直接使用安装这个插件了json
二、插件制做:segmentfault
全部插件都要有manifest.json这个文件,这是插件的配置文件,可看做插件的“入口”,要放在插件的根目录下面跨域
permissions
属性是一个数组,它定义了扩展须要向 Chrome 申请的权限,好比经过 XMLHttpRequest 跨域请求数据、访问浏览器选项卡(tabs)、获取当前活动选项卡(activeTab)、浏览器通知(notifications)、存储(storage)等,能够根据须要添加。数组
background
可使扩展常驻后台,比较经常使用的是指定子属性 scripts
,表示在扩展启动时自动建立一个包含全部指定脚本的页面。浏览器
manifest.json
{
"manifest_version": 2, #必须参数,值必须为2
"name": "Chrome_request_blocking", #必须参数,插件名字
"version": "1.0", #必须参数,插件版本,有格式要求1.x.x
"background":{"scripts":["js/background.js"]}, #完成插件功能的js脚本路径
"permissions": [
"webRequest", "webRequestBlocking", "*://login.tongxue.cn/htdocs/scripts/lib/*" #拦截url必须容许webRequest和webRequestBlocking.这个设置的意思是插件拦截全部发往"*://login.tongxue.cn/htdocs/scripts/lib/*"的请求
]
}
background.js
// Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // 'use strict';//监听全部请求 chrome.webRequest.onBeforeRequest.addListener( function(details) { return {cancel: true}; }, {urls: ["https://login.tongdxue.cn/htdocs/scripts/lib/three.min.js"]}, #拦截的url列表 ["blocking"] );
注意:html和js要分开执行,也就是说不能在html里放js语句,
<td>
<button id="add" onclick="saveChanges()">增长</button>
</td>
只能放js引用(<script src="popup.js"></script>)
<!--
- JavaScript and HTML must be in separate files: see our Content Security
- Policy documentation[1] for details and explanation.
-
- [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
-->
调试:
在插件的popup.html页面上右键弹出“检查”,而后出现另外一个开发者工具界面(这个是插件的开发者工具,有别于网页的开发者工具),而后能够调试popup.html已经popup.html引用的js文件。
background.js是主程序,是看不到的
参考:
一、https://www.jianshu.com/p/2c2a4f497135
二、http://www.voidcn.com/article/p-ffruhaug-pu.html
三、https://segmentfault.com/a/1190000005896962
四、https://www.cnblogs.com/guogangj/p/3235703.html
官网资料:
https://developer.chrome.com/extensions/getstarted
https://developer.chrome.com/extensions/webRequest#event-onBeforeRequest
https://developer.chrome.com/extensions
中午版官网资料:https://crxdoc-zh.appspot.com/extensions/tut_debugging