abi (owner)

Forks

Revisions

  • 13baf2 Thu Feb 12 09:01:16 -0800 2009
gist: 62729 Download_button fork
public
Public Clone URL: git://gist.github.com/62729.git
Embed All Files: show embed
x #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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.");
  }
});