Skip to content

Instantly share code, notes, and snippets.

@abi

abi/x

Created February 12, 2009 17:01
Show Gist options
  • Save abi/62729 to your computer and use it in GitHub Desktop.
Save abi/62729 to your computer and use it in GitHub Desktop.
function getBookmarklets(callback) {
var bookmarklets = {};
var Ci = Components.interfaces;
var Cc = Components.classes;
var bookmarks = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Ci.nsINavBookmarksService);
var history = Cc["@mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsINavHistoryService);
var query = history.getNewQuery();
// Specify folders to be searched
var folders = [bookmarks.toolbarFolder, bookmarks.bookmarksMenuFolder,
bookmarks.unfiledBookmarksFolder];
query.setFolders(folders, folders.length);
var options = history.getNewQueryOptions();
options.queryType = options.QUERY_TYPE_BOOKMARKS
// Specify terms to search for, matches against title, URL and tags
query.searchTerms = "javascript";
var result = history.executeQuery(query, options);
// The root property of a query result is an object representing the folder you specified above.
var resultContainerNode = result.root;
// Open the folder, and iterate over it's contents.
resultContainerNode.containerOpen = true;
for (var i=0; i < resultContainerNode.childCount; ++i) {
var childNode = resultContainerNode.getChild(i);
// Accessing properties of matching bookmarks
var title = childNode.title;
var uri = childNode.uri;
if(uri.substring(0,11) == "javascript:") {
bookmarklets[title.toLowerCase().replace(/ /g,'-')] = uri;
}
}
callback(bookmarklets);
}
var noun_type_bookmarklet = {
_name: "bookmarklet",
bookmarkletList: null,
callback: function(bookmarklets){
noun_type_bookmarklet.bookmarkletList = bookmarklets;
},
suggest: function( text, html ) {
if (noun_type_bookmarklet.bookmarkletList == null) {
getBookmarklets(noun_type_bookmarklet.callback);
return [];
}
bookmarklets = noun_type_bookmarklet.bookmarkletList;
var suggestions = [];
for ( var c in bookmarklets) {
if (c.match(text, "i"))
suggestions.push(CmdUtils.makeSugg(c, "", bookmarklets[c]));
}
return suggestions.splice(0, 5);
}
};
CmdUtils.CreateCommand({
name: "create-bookmarklet-command-from",
takes: {"bookmarklet name": noun_type_bookmarklet}, //add name modifier
description: "Create a new command from a bookmarklet",
author: {name: "Abimanyu Raja", email: "abimanyuraja@gmail.com"},
license: "MPL",
preview: function(previewBlock,directObj) {
bookmarklet = directObj.text;
if(bookmarklet) previewBlock.innerHTML = "Creates a new command called <b>" + bookmarklet + " </b> that runs the " + bookmarklet + " bookmarklet";
},
execute: function(directObj) {
var name = directObj.text;
var url = directObj.data;
//build the piece of code that creates the command
var code = '\n\n//Note: This command was automatically generated by the create-bookmarklet-command command.\n';
code += 'CmdUtils.makeBookmarkletCommand({\n';
code += ' name: "' + name + '",\n';
code += ' url: "'+url.replace("\"","\\\"", "gi")+'" \n';
code += '});\n';
//append the code to Ubiqity's command editor
CmdUtils.UserCode.appendCode(code);
//tell the user we finished
displayMessage("You have created the command: " + name +
". You can edit its source-code with the command-editor command.");
}
});
CmdUtils.CreateCommand({
description: "Creates a new Ubiquity command from a search-box.",
help: "1. Select a searchbox 2. Execute this command to create the new search command.",
name: "create-new-search-command",
author: {name: "Marcello Herreshoff",
homepage: "http://stanford.edu/~marce110/"},
contributors: ["Abimanyu Raja"],
icon: "chrome://ubiquity/skin/icons/search.png",
license: "GPL/LGPL/MPL",
homepage: "http://stanford.edu/~marce110/verbs/new-command-from-search-box.html",
takes: {"command name": noun_arb_text},
_makeURI : function(aURL, aOriginCharset, aBaseURI){
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
return ioService.newURI(aURL, aOriginCharset, aBaseURI);
},
_escapeNameValuePair: function(aName, aValue, aIsFormUrlEncoded){
if (aIsFormUrlEncoded)
return escape(aName + "=" + aValue);
else
return escape(aName) + "=" + escape(aValue);
},
preview: function(pblock, input) {
if(input.text.length < 1){
pblock.innerHTML = "1. Select a searchbox 2. Execute this command to create the new search command. ";
}else{
pblock.innerHTML = "Creates a new search command called <b>" + input.text + "</b>";
}
},
execute: function(name){
//1. Figure out what this search-bar does.
var node = context.focusedElement;
if(!node || !node.form){
displayMessage("You need to click on a searchbox before running this command."); return;
}
//Copied from chrome://browser/content/browser.js, function AddKeywordForSearchField()
//Comments starting with MMH: indicates that something has been changed by me.
PLACEHOLDER = "{QUERY}"; //MMH: Note also: I have globally replaced "%s" with PLACEHOLDER
//COPY STARTS
var charset = node.ownerDocument.characterSet;
var docURI = this._makeURI(node.ownerDocument.URL,
charset);
var formURI = this._makeURI(node.form.getAttribute("action"),
charset,
docURI);
var spec = formURI.spec;
var isURLEncoded =
(node.form.method.toUpperCase() == "POST"
&& (node.form.enctype == "application/x-www-form-urlencoded" ||
node.form.enctype == ""));
var el, type;
var formData = [];
for (var i=0; i < node.form.elements.length; i++) {
el = node.form.elements[i];
if (!el.type) // happens with fieldsets
continue;
if (el == node) {
formData.push((isURLEncoded) ? this._escapeNameValuePair(el.name, PLACEHOLDER, true) :
// Don't escape PLACEHOLDER, just append
this._escapeNameValuePair(el.name, "", false) + PLACEHOLDER);
continue;
}
type = el.type.toLowerCase();
if ((type == "text" || type == "hidden" || type == "textarea") ||
((type == "checkbox" || type == "radio") && el.checked)) {
formData.push(this._escapeNameValuePair(el.name, el.value, isURLEncoded));
//} else if (el instanceof HTMLSelectElement && el.selectedIndex >= 0) {
} else if (el.selectedIndex && el.name == "select" && el.selectedIndex >= 0){
//MMH: HTMLSelectElement was undefined.
for (var j=0; j < el.options.length; j++) {
if (el.options[j].selected)
formData.push(this._escapeNameValuePair(el.name, el.options[j].value,
isURLEncoded));
}
}
}
var postData;
if (isURLEncoded)
postData = formData.join("&");
else
spec += "?" + formData.join("&");
//COPY ENDS
var url = spec;
//2. Now that we have the form's URL, figure out the name, description and favicon for the command
currentLocation = String(Application.activeWindow.activeTab.document.location);
domain = currentLocation.replace(/^(.*):\/\//, '').split('/')[0];
name = name.text;
if(!name){ var parts = domain.split('.'); name = parts[parts.length-2] + "-search";}
var icon = "http://"+domain+"/favicon.ico";
var description = "Searches " + domain;
var code;
if(!postData){
//3. Build the piece of code that creates the command
code = '\n\n//Note: This command was automatically generated by the create-new-search-command command.\n'
code += 'CmdUtils.makeSearchCommand({\n'
code += ' name: "'+name+'",\n';4
code += ' url: "'+url+'",\n';
code += ' icon: "'+icon+'",\n';
code += ' description: "'+description+'"\n';
code += '});\n';
}else{
code = '\n\n//Note: This command was automatically generated by the create-new-search-command command.\n'
code += 'CmdUtils.CreateCommand({\n'
code += ' name: "'+name+'",\n';4
code += ' url: "'+url+'",\n';
code += ' icon: "'+icon+'",\n';
code += ' description: "'+description+'",\n';
code += ' takes: {"search term": noun_arb_text},\n';
code += ' execute: function(input){ var query = encodeURIComponent(input.text);\n';
code += 'var postData = \"' + decodeURIComponent(String(postData)) + '\".replace(/%s|{QUERY}/g, query);\nUtils.openUrlInBrowser(\"' + formURI.spec+ '\", postData);}\n';
code += '});\n';
}
//4. Append the code to Ubiqity's code
CmdUtils.UserCode.appendCode(code);
//5. Tell the user we finished
displayMessage("You have created the command: " + name +
". You can edit its source-code with the command-editor command.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment