2009-10-14: 稍作修改,新版比較親切好用,請參考 PlacesCleaner bookmarklet (更新)

原創想法&程式碼取自 Irvin Chen 的貢獻,詳見:

這支 bookmarklet 會跑原文所述的幾行 SQL,可指定刪除瀏覽次數(不超過)多少的紀錄,且刪除前可備份 places.sqlite。
※請於 Chrome URL 中執行(例: chrome://browser/content/places/places.xul )否則沒效。

將 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(){

  var PIPlacesDatabase = Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection;
  var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  
  var intViewTime = parseInt(prompt('清除瀏覽次數不超過「幾次」的歷史紀錄?'),10);
  if(intViewTime+''=='NaN') { alert('輸入值非數字,已停止操作。'); return; }

  if(confirm('若要備份 places.sqlite,請選擇儲存檔案的位置:')) {
    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);
      var oldFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
      oldFile = oldFile.clone();
      oldFile.append("places.sqlite");
      try {
        oldFile.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;
  }
  alert('清除完畢。');

}

};

PlacesCleaner.clean();