chrome清除缓存插件

作者: 卡农Lucas | 来源:发表于2016-11-09 20:23 被阅读0次

1. 简介

可以清除全部缓存数据。

2. 插件代码

2.1 代码结构

Paste_Image.png

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引用。

3. 安装方法

chajian_by_lucasluo.png

相关文章

网友评论

    本文标题:chrome清除缓存插件

    本文链接:https://www.haomeiwen.com/subject/jgjtpttx.html