Last active
October 25, 2016 17:10
-
-
Save Infocatcher/9884f366210861c798ffe4d9a4b38816 to your computer and use it in GitHub Desktop.
Tricky way to detect if there something to remove using nsIDownloadHistory.removeAllDownloads()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDownloadHistory() { | |
var query = PlacesUtils.history.getNewQuery(); | |
query.setTransitions([Components.interfaces.nsINavHistoryService.TRANSITION_DOWNLOAD], 1); | |
var options = PlacesUtils.history.getNewQueryOptions(); | |
options.resultType = options.RESULTS_AS_URI; | |
options.queryType = Components.interfaces.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; | |
options.includeHidden = true; | |
var result = PlacesUtils.history.executeQuery(query, options); | |
var contents = result.root; | |
contents.containerOpen = true; | |
var uris = []; | |
for(var i = 0, l = contents.childCount; i < l; ++i) | |
uris.push(contents.getChild(i).uri); | |
return uris; | |
} | |
function hasDownloadHistory() { | |
var query = PlacesUtils.history.getNewQuery(); | |
query.setTransitions([Components.interfaces.nsINavHistoryService.TRANSITION_DOWNLOAD], 1); | |
var options = PlacesUtils.history.getNewQueryOptions(); | |
options.resultType = options.RESULTS_AS_URI; | |
options.queryType = Components.interfaces.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; | |
options.includeHidden = true; | |
options.maxResults = 1; | |
var result = PlacesUtils.history.executeQuery(query, options); | |
var contents = result.root; | |
contents.containerOpen = true; | |
return !!contents.childCount; | |
} | |
alert(hasDownloadHistory() + "\n" + getDownloadHistory().join("\n")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasDlHistory() { | |
try { | |
var stmt = PlacesUtils.history.DBConnection.createStatement( | |
'select place_id from "moz_annos" where anno_attribute_id=\ | |
(select id from "moz_anno_attributes" where name="downloads/destinationFileURI")' | |
); | |
var ids = []; | |
while(stmt.executeStep()) | |
ids.push(stmt.row.place_id); | |
Services.console.logStringMessage("hasDlHistory(): ids:\n" + ids.join(", ")); | |
if(!ids.length) | |
return false; | |
} | |
catch(e) { | |
Components.utils.reportError(e); | |
} | |
finally { | |
stmt && stmt.finalize(); | |
} | |
if(ids) try { | |
var stmt = PlacesUtils.history.DBConnection.createStatement( | |
'select url from "moz_places" where id = ' + ids.join(" or id = ") + " limit 1" | |
); | |
if(stmt.executeStep()) { | |
Services.console.logStringMessage("hasDlHistory(): url: " + stmt.row.url); | |
return true; | |
} | |
Services.console.logStringMessage("hasDlHistory(): Not found in moz_places"); | |
return false; | |
} | |
catch(e) { | |
Components.utils.reportError(e); | |
} | |
finally { | |
stmt && stmt.finalize(); | |
} | |
return undefined; | |
} | |
alert(hasDlHistory()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasDlHistoryAsync(callback, context) { | |
var stmt = PlacesUtils.history.DBConnection.createAsyncStatement( | |
'select place_id from "moz_annos" where anno_attribute_id=\ | |
(select id from "moz_anno_attributes" where name="downloads/destinationFileURI")' | |
); | |
var hasRes; | |
var ids = []; | |
stmt.executeAsync({ | |
handleResult: function(res) { | |
hasRes = true; | |
for(var row; row = res.getNextRow(); ) | |
ids.push(row.getResultByName("place_id")); | |
}, | |
handleCompletion: function(reason) { | |
var failed = reason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED; | |
if(!hasRes || failed) { | |
callback.call(context, failed ? undefined : false); | |
return; | |
} | |
Services.console.logStringMessage("hasDlHistoryAsync(): ids:\n" + ids.join(", ")); | |
if(!ids.length) { | |
callback.call(context, false); | |
return; | |
} | |
var stmt = PlacesUtils.history.DBConnection.createAsyncStatement( | |
'select url from "moz_places" where id = ' + ids.join(" or id = ") + " limit 1" | |
); | |
hasRes = false; | |
stmt.executeAsync({ | |
handleResult: function(res) { | |
hasRes = true; | |
var row = res.getNextRow(); | |
callback.call(context, !!row); | |
Services.console.logStringMessage("hasDlHistoryAsync(): url: " + row.getResultByName("url")); | |
}, | |
handleCompletion: function(reason) { | |
var failed = reason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED; | |
if(!hasRes || failed) | |
callback.call(context, failed ? undefined : false); | |
} | |
}); | |
} | |
}); | |
stmt.finalize(); | |
} | |
hasDlHistoryAsync(function(has) { | |
alert(has); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment