I'm running the following javascript code in firefox extension
var highlightLinks = function(e)
{
var anchors = e.target.getElementsByTagName("a");
let file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("test.sqlite");
var storageService = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService);
var conn = storageService.openDatabase(file);
var statement = conn.createStatement("select * from links where url=?1");
for (var i = 0; i < anchors.length; i++) {
statement.bindStringParameter(0, anchors[i].href);
statement.executeAsync({
anchorIndex: i,
handleResult: function(aResultSet) {
for (let row = aResultSet.getNextRow();
row;
row = aResultSet.getNextRow()) {
let value = row.getResultByName("url");
if (value == anchors[this.anchorIndex]) {
anchors[this.anchorIndex].innerHTML += "+";
}
}
},
handleError: function(aError) {
print("Error: " + aError.message);
},
handleCompletion: function(aReason) {
if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED)
print("Query canceled or aborted!");
}
});
}
statement.finalize();
conn.close();
}
window.addEventListener("DOMContentLoaded", highlightLinks, false);
The code is executed very often, so I'm thinking if it's already ideal or if it's possible to optimize it yet.
thanks for suggestions