1. 简介
可以清除全部缓存数据。
2. 插件代码
2.1 代码结构
2.2 manifest.json
{
"name": "Chrome缓存清理插件",
"description": "Chrome缓存清理插件",
"version": "2.0",
"permissions": ["*://*/*", "tabs", "activeTab", "browsingData"],
"background": {
"scripts": ["background.js", "jquery.js"],
"persistent": false
},
"browser_action": {
"default_title": "Chrome缓存清理插件",
"default_popup": "popup.html"
},
"manifest_version": 2
}
2.3 popup.html
<html>
<head>
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body style="width: 100px">
<div style="margin-bottom:15px">
<span>清除chrome缓存工具</span>
</div>
<div>
<button id="clearCache">清除缓存</button>
</div>
</body>
</html>
2.4 popup.js
$(function() {
$('#clearCache').click(function() {
var port = chrome.extension.connect({
name: 'Clear Cache'
});
port.postMessage('clearCache');
port.onMessage.addListener(function(msg) {
if(msg === 'ok') {
alert("done");
} else {
alert("Something bad happened.");
}
});
});
});
2.5 background.js
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
if(msg === 'clearCache') {
clearCache();
port.postMessage('ok');
} else {
// do nothing
}
});
});
/**
* 清除缓存
*/
function clearCache() {
chrome.browsingData.remove({
'since': 0
}
, {
'appcache': true
, 'cache': true
, 'cookies': true
, 'downloads': true
, 'fileSystems': true
, 'formData': true
, 'history': true
, 'indexedDB': true
, 'localStorage': true
, 'pluginData': true
, 'passwords': true
, 'webSQL': true
}
, function() {
// do nothing...
});
}
2.6 jquery.js
如果需要在background.js里面引用jquery.js,那么下载一个jquery.js文件放进目录里即可,记得在permission配置的scripts选项添加jquery.js引用。