PlacesCleaner bookmarklet (更新)
原版如前文,縮小的感覺真好,如今再改得稍微親切一點: 1)不在 chrome url 的話會提示, 2)顯示 sqlite 檔案前後大小。
將 bookmarklet 加入書籤 → Places Cleaner
原始碼拆開如下:
// 原創想法&程式碼來自 Irvin Chen 的 PlacesCleaner 收藏庫清潔工 // https://addons.mozilla.org/firefox/addon/13860/ (以 MPL 1.1 釋出) // http://irvin.sto.tw/2009/08/firefox-3.html // 請於 chrome url (例: chrome://browser/content/places/places.xul ) 中執行 var PlacesCleaner = { clean: function(){ // 測試是否有 XPConnect 權限 if( ! netscape.security.PrivilegeManager.isPrivilegeEnabled('UniversalXPConnect') ) { alert('操作失敗:\n請於 chrome url 中執行這支 bookmarklet。\n例: chrome://browser/content/places/places.xul'); return; } var PIPlacesDatabase = Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection; var ProfD = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile); var sqliteFile = ProfD.clone(); sqliteFile.append("places.sqlite"); var originalSize = sqliteFile.fileSize; var intViewTime = parseInt(prompt('清除瀏覽次數不超過「幾次」的歷史紀錄?'),10); if(intViewTime+''=='NaN') { alert('輸入值非數字,已停止操作。'); return; } if(confirm('目前 places.sqlite 檔案大小為 ' + Math.round(originalSize/10.24)/100 + ' KB。\n若要備份,請選擇儲存檔案的位置:')) { var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker); fp.init(window, null, fp.modeSave); fp.defaultString='places'; fp.defaultExtension='sqlite'; fp.appendFilter(".sqlite","*.sqlite"); if (fp.show() != fp.returnCancel) { if (fp.file.exists()) fp.file.remove(true); try { sqliteFile.copyTo( fp.file.parent, fp.file.leafName ); } catch(e) { alert('操作失敗:無法儲存備份檔案。'); return; } } } if(!confirm('確定要開始清除了嗎?\n也許會花上一點時間……')) return; try { // 取自 https://addons.mozilla.org/firefox/addon/13860/ 原始碼授權條款: MPL 1.1 PIPlacesDatabase.executeSimpleSQL("DELETE FROM moz_historyvisits WHERE place_id IN (SELECT id FROM moz_places WHERE visit_count <=" + intViewTime + ");"); PIPlacesDatabase.executeSimpleSQL("DELETE FROM moz_places WHERE (visit_count <=" + intViewTime + " AND hidden <> 1 AND id NOT IN (SELECT place_id FROM moz_annos UNION SELECT fk FROM moz_bookmarks));"); PIPlacesDatabase.executeSimpleSQL("DELETE FROM moz_inputhistory WHERE place_id NOT IN (SELECT id FROM moz_places);"); PIPlacesDatabase.executeSimpleSQL("DELETE FROM moz_favicons WHERE id NOT IN (SELECT favicon_id FROM moz_places);"); PIPlacesDatabase.executeSimpleSQL("DELETE FROM moz_annos WHERE anno_attribute_id IN (SELECT id FROM moz_anno_attributes WHERE name = 'google-toolbar/thumbnail-score' OR name = 'google-toolbar/thumbnail');"); PIPlacesDatabase.executeSimpleSQL("VACUUM"); } catch(e) { alert('操作失敗:清除中出了狀況……'); alert('Places Cleaner bookmarklet 錯誤訊息:\n '+e); return; } // 重新取得 fileSize sqliteFile = ProfD.clone(); sqliteFile.append("places.sqlite"); var ratio = Math.round( (originalSize-sqliteFile.fileSize)*10000/originalSize )/100; alert('清除完畢。\n\n檔案大小:' + Math.round(originalSize/10.24)/100 + ' KB → ' + Math.round(sqliteFile.fileSize/10.24)/100 + ' KB(已縮減 ' + ratio + ' %)'); } }; PlacesCleaner.clean();
有 2 個意見
讚喔,顯示檔案大小這個我跟小B一直想寫還沒寫,code 就借我用用吧 XD
好啊,我加的東西 MDC 大致都有提示,還是那幾行 SQL 有內涵啊。
☂