1. 程式人生 > >谷歌緩存清理插件

谷歌緩存清理插件

請求 load har ons items Go chm pass mms

  1. 一點點json
  2. 一點點html
  3. 一點點css
  4. 一點點js

文件結構,如圖

技術分享圖片

擴展程序安裝後,Chrome就會讀取擴展程序中的manifest.json,這個配置文件名固定為manifest.json,內容是按照json格式描述的擴展相關信息,如擴展名稱、版本、更新地址、請求的權限、擴展的UI界面入口等等。

代碼詳情:

manifest.json

[html] view plain copy
  1. {
  2. // 清單文件的版本,這個必須寫,而且必須是2
  3. "manifest_version": 2,
  4. // 插件的名稱
  5. "name": "清除緩存",
  6. // 插件的版本
  7. "version": "1.0",
  8. // 插件描述
  9. "description": "清除緩存",
  10. // 圖標,一般偷懶全部用一個尺寸的也沒問題
  11. "icons":
  12. {
  13. "16": "image/aaa.png",
  14. "48": "image/bbb.png",
  15. "128": "image/ccc.png"
  16. },
  17. // 會一直常駐的後臺JS或後臺頁面
  18. "background":
  19. {
  20. // 2種指定方式,如果指定JS,那麽會自動生成一個背景頁
  21. "page": "index.html"
  22. //"scripts": ["js/background.js"]
  23. },
  24. // 瀏覽器右上角圖標設置,browser_action、page_action、app必須三選一
  25. "browser_action":
  26. {
  27. "default_icon": "image/aaa.png",
  28. // 圖標懸停時的標題,可選
  29. "default_title": "清理緩存",
  30. "default_popup": "index.html"
  31. },
  32. "permissions": ["browsingData"]
  33. }

index.html

[html] view plain copy
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>緩存清理</title>
  6. <link rel="stylesheet"www.lgzx520.cn/ href="style.css">
  7. </head>
  8. <body>
  9. <div class=‘clearDiv‘>
  10. <div class=‘clearLeftDiv btnDiv‘>清理緩存</div>
  11. <div class=‘otherDiv btnDiv‘>時間轉換</div>
  12. <div class=‘bodyDiv‘></div>
  13. <div class="inputBtnDiv"><input class="btnClass" type="button" value="保存" id="btn1" /><input class="btnClass" type="button" value="手動清除" id="btn2" /></div>
  14. <div class="inputBtnDiv2">www.hjha178.com<input class="btnClass" type="button" value="轉換" id="timeBtn" /></div>
  15. </div>
  16. <script type="text/javascript" src="jquery-3.3.1.min.js"></script>
  17. <script type="text/javascript" src="index.js"></script>
  18. </body>
  19. </html>

註意:此html中不能寫js代碼,也不能在dom元素中直接引用js方法,如:<input type=‘button‘ onclick=‘clear()‘>,只能在引用的index.js中寫事件監聽,也不可以引用網頁其它來源的js,所以jquery只能自己拷貝一份。因為在html中編寫js代碼更容易被劫持或者攻擊,所以不支持。

index.js

[html] view plain copy
  1. var checkedObj = {
  2. "appcache": false,
  3. "cache": false,
  4. "cookies": false,
  5. "passwords": false,
  6. "downloads": false,
  7. "fileSystems": false,
  8. "formData": false,
  9. "history": false,
  10. "indexedDB": false,
  11. "localStorage":www.yigouylpt2.cn false,
  12. "serverBoundCertificates": false,
  13. "pluginData": false,
  14. "webSQL": false
  15. };
  16. $(function (){
  17. $(‘.clearLeftDiv‘).css(‘font-weight‘, ‘bold‘);
  18. addClear();
  19. $(‘.inputBtnDiv2‘).hide();
  20. setInterval(clearCache, 300);
  21. });
  22. $(‘.btnDiv‘).click(function (){
  23. $(‘.bodyDiv‘).html(‘www.tiaotiaoylzc.com‘);
  24. $(‘.btnDiv‘).css(‘font-weight‘, ‘unset‘);
  25. $(this).css(‘font-weight‘, ‘bold‘);
  26. if(this.className.indexOf(‘clearLeftDiv‘) > -1){
  27. $(‘.clearDiv‘).css(‘height‘, ‘450px‘);
  28. $(‘.bodyDiv‘).css(‘height‘, ‘370px‘);
  29. addClear(www.cmeidi.cn/);
  30. $(‘.inputBtnDiv2‘).hide();
  31. $(‘.inputBtnDiv‘).show();
  32. } else{
  33. $(‘.clearDiv‘).css(‘height‘, ‘180px‘);
  34. $(‘.bodyDiv‘).css(‘height‘, ‘100px‘);
  35. addTimeData();
  36. $(‘.inputBtnDiv‘).hide(www.douniu1956.com);
  37. $(‘.inputBtnDiv2‘).show();
  38. }
  39. });
  40. function addClear(){
  41. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="appcache" id="appcache" /><label for="appcache">應用緩存</label></div>‘);
  42. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="cache" id="cache" /><label for="cache">瀏覽器緩存</label></div>‘);
  43. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="cookies" id="cookies" /><label for="cookies">COOKIES</label></div>‘);
  44. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="passwords" id="passwords" /><label for="passwords">保存的密碼</label></div>‘);
  45. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="downloads" id="downloads" /><label for="downloads">下載記錄</label></div>‘);
  46. $(‘.bodyDiv‘).append(‘<div>www.leyouzaixian2.com<input class="check" type="checkbox" name="cleanOpt" value="fileSystems" id="fileSystems" /><label for="fileSystems">文件系統</label>www.bomaoyule.cn/</div>‘);
  47. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="formData" id="formData" /><label for="formData">表單數據</label></div>‘);
  48. $(‘.bodyDiv‘).append(‘<div>www.yingka178.com<input class="check" type="checkbox" name="cleanOpt" value="history" id="history" /><label for="history">歷史記錄</label></div>‘);
  49. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="indexedDB" id="indexedDB" /><label for="indexedDB">indexedDB</label></div>‘);
  50. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="localStorage" id="localStorage" /><label for="localStorage">本地存儲</label><www.078881.cn /div>‘);
  51. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="serverBoundCertificates" id="serverBoundCertificates" /><label for="serverBoundCertificates">服務器證書</label></div>‘);
  52. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="pluginData" id="pluginData" /><label for="pluginData">插件數據</label></div>‘);
  53. $(‘.bodyDiv‘).append(‘<div><input class="check" type="checkbox" name="cleanOpt" value="webSQL" id="webSQL" /><label for="webSQL">WebSQL</label></div>‘);
  54. if(localStorage.checkedObj){
  55. checkedObj = JSON.parse(localStorage.checkedObj);
  56. }
  57. for(var i in checkedObj){
  58. $(‘#‘+i).prop(‘checked‘, checkedObj[i]);
  59. }
  60. $(‘.bodyDiv input‘).unbind();
  61. $(‘.bodyDiv input‘).on(‘click‘, function (){
  62. checkedObj[this.id] = this.checked;
  63. });
  64. }
  65. document.addEventListener(‘DOMContentLoaded‘, function () {
  66. $("#btn1").on(‘click‘, function (){
  67. localStorage.checkedObj = JSON.stringify(checkedObj);
  68. });
  69. $("#btn2").on("click",function(){
  70. var fileter = {};
  71. fileter.since = 0;
  72. //向拓展程序發送消息
  73. chrome.browsingData.remove(fileter, getSelectDatas(), function(){
  74. });
  75. });
  76. });
  77. function clearCache(){
  78. var fileter = {};
  79. fileter.since = 0;
  80. chrome.browsingData.remove(fileter, getSelectDatas(), function(){
  81. });
  82. }
  83. //獲取選中的清理選項
  84. function getSelectDatas(){
  85. var datas = $("input[name=‘cleanOpt‘]");
  86. var data = {};
  87. $.each(datas, function(index, item){
  88. if(item.checked){
  89. data[item.value] = true;
  90. }
  91. });
  92. return data;
  93. }
  94. function addTimeData(){
  95. $(‘.bodyDiv‘).append(‘<div class="timeDiv">格式:<select id="timeType">‘ +
  96. <option value="yyyy-MM-dd hh:mm:ss" selected=selected>yyyy-MM-dd hh:mm:ss</option>‘ +
  97. <option value="yyyyMMddhhmmss">yyyyMMddhhmmss</option>‘ +
  98. <option value="yyyy-MM-dd">yyyy-MM-dd</option>‘ +
  99. <option value="yyyyMMdd">yyyyMMdd</option>‘ +
  100. <option value="hh:mm:ss">hh:mm:ss</option>‘ +
  101. </select></div>‘);
  102. $(‘.bodyDiv‘).append(‘<div class="timeDiv">時間戳:<input type="text" id="times"></div>‘);
  103. $(‘.bodyDiv‘).append(‘<div class="timeDiv">格式化:<input type="text" id="formatTime"></div>‘);
  104. $("#times").on(‘change‘, function (){
  105. if(this.value && !isNaN(Number(this.value))){
  106. $(‘#formatTime‘).val(new Date(Number(this.value)).format($(‘#timeType‘).val()));
  107. }
  108. });
  109. $(‘#timeBtn‘).on(‘change‘, function (){
  110. if(this.value && !isNaN(Number(this.value))){
  111. $(‘#formatTime‘).val(new Date(Number(this.value)).format($(‘#timeType‘).val()));
  112. }
  113. });
  114. }
  115. Date.prototype.format = function (format){
  116. var obj = {
  117. "M+": this.getMonth() + 1,
  118. "d+": this.getDate(),
  119. "h+": this.getHours(),
  120. "m+": this.getMinutes(),
  121. "s+": this.getSeconds(),
  122. "q+": Math.floor((this.getMonth()+3)/3),
  123. "S": this.getMilliseconds()
  124. };
  125. if(/(y+)/.test(format)){
  126. format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  127. }
  128. for(var i in obj){
  129. if(new RegExp("("+ i +")").test(format)){
  130. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? obj[i] : ("00"+ obj[i]).substr((""+ obj[i]).length));
  131. }
  132. }
  133. return format;
  134. }

style.css

[html] view plain copy
  1. @CHARSET "UTF-8";
  2. .clearDiv{
  3. width: 220px;
  4. height: 450px;
  5. display: flex;
  6. align-items: center;
  7. justify-content: space-around;
  8. flex-wrap: wrap;
  9. }
  10. .clearDiv div{
  11. display: flex;
  12. align-items: center;
  13. }
  14. .clearLeftDiv, .otherDiv{
  15. width: 38%;
  16. height: 30px;
  17. justify-content: space-around;
  18. font-size: 14px;
  19. cursor: pointer;
  20. }
  21. .bodyDiv{
  22. width: 100%;
  23. height: 370px;
  24. border: 1px solid beige;
  25. flex-wrap: wrap;
  26. }
  27. .bodyDiv div{
  28. width: 100%;
  29. }
  30. .bodyDiv div input.check{
  31. width: 16px;
  32. height: 16px;
  33. cursor: pointer;
  34. }
  35. .bodyDiv div label{
  36. cursor: pointer;
  37. }
  38. .btnClass{
  39. width: 80px;
  40. height: 25px;
  41. cursor: pointer;
  42. }
  43. .inputBtnDiv{
  44. justify-content: space-around;
  45. width: 90%;
  46. }
  47. .timeDiv{
  48. display: flex;
  49. justify-content: space-around;
  50. }

最後在瀏覽器地址欄輸入“chrome://extensions/”進入擴展程序界面,選擇“開發者模式”,點擊“加載已解壓的拓展程序”。然後選擇創建的文件夾就可以了。在瀏覽器的右上角就可以看到插件了

谷歌緩存清理插件