comparison chromeless-browsing/chromeless.js @ 4:fd988ee76408

Added chromeless browsing feed.
author Atul Varma <varmaa@toolness.com>
date Fri, 27 Mar 2009 13:16:11 -0700
parents
children 96bb8d08e94a
comparison
equal deleted inserted replaced
3:7ac908bdb3d3 4:fd988ee76408
1 var noun_type_url = {
2 _name: "url",
3 _getHistoryLinks: function(partialSearch, onSearchComplete) {
4 function AutoCompleteInput(aSearches) {
5 this.searches = aSearches;
6 }
7 AutoCompleteInput.prototype = {
8 constructor: AutoCompleteInput,
9 searches: null,
10 minResultsForPopup: 0,
11 timeout: 10,
12 searchParam: "",
13 textValue: "",
14 disableAutoComplete: false,
15 completeDefaultIndex: false,
16 get searchCount() {
17 return this.searches.length;
18 },
19 getSearchAt: function(aIndex) {
20 return this.searches[aIndex];
21 },
22 onSearchBegin: function() {},
23 onSearchComplete: function() {},
24 popupOpen: false,
25 popup: {
26 setSelectedIndex: function(aIndex) {},
27 invalidate: function() {},
28 // nsISupports implementation
29 QueryInterface: function(iid) {
30 if (iid.equals(Ci.nsISupports) ||
31 iid.equals(Ci.nsIAutoCompletePopup))
32 return this;
33 throw Components.results.NS_ERROR_NO_INTERFACE;
34 }
35 },
36 // nsISupports implementation
37 QueryInterface: function(iid) {
38 if (iid.equals(Ci.nsISupports) ||
39 iid.equals(Ci.nsIAutoCompleteInput))
40 return this;
41 throw Components.results.NS_ERROR_NO_INTERFACE;
42 }
43 };
44
45 var controller = Cc["@mozilla.org/autocomplete/controller;1"]
46 .getService(Ci.nsIAutoCompleteController);
47 var input = new AutoCompleteInput(["history"]);
48 controller.input = input;
49 input.onSearchComplete = function() {
50 onSearchComplete(controller);
51 };
52
53 controller.startSearch(partialSearch);
54 },
55 suggestions: [],
56 _onSearchComplete: function(controller) {
57 var suggs = [];
58 var max_suggs = 5;
59 for (var i = 0; i < controller.matchCount && i <= max_suggs; i++) {
60 var result = {
61 title: controller.getCommentAt(i),
62 url: controller.getValueAt(i),
63 image: controller.getImageAt(i),
64 style: controller.getStyleAt(i)
65 };
66 if (!result.title)
67 result.title = result.url;
68 if (result.image.indexOf("moz-anno:favicon:") == 0)
69 result.image = result.image.slice("moz-anno:favicon:".length);
70 else
71 result.image = "";
72 suggs.push(CmdUtils.makeSugg(result.title, undefined, result));
73 }
74 noun_type_url.suggestions = suggs;
75 },
76 suggest: function(fragment) {
77 this._getHistoryLinks(fragment, this._onSearchComplete);
78 var regexp = /(ftp|http|https):\/\/(\w+:{01}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
79 // Magic words "page" or "url" result in the URL of the current page
80 if (fragment == "page" || fragment == "url") {
81 var url = Application.activeWindow.activeTab.document.URL;
82 this.suggestions.push(CmdUtils.makeSugg(url));
83 }
84 // If it's a valid URL, suggest it back
85 if (regexp.test(fragment)) {
86 this.suggestions.push(CmdUtils.makeSugg(fragment));
87 } else if (regexp.test("http://" + fragment)) {
88 this.suggestions.push(CmdUtils.makeSugg("http://" + fragment));
89 }
90 return this.suggestions;
91 }
92 };
93
94 CmdUtils.CreateCommand(
95 {name: "go",
96 takes: {
97 "search terms": noun_type_url
98 },
99 preview: function(pblock, theWords) {
100 if (theWords.text.length == 0) {
101 pblock.innerHTML = "<b>Please enter a search term</b>";
102 return;
103 }
104 var result = "";
105 if (theWords.data) {
106 if (theWords.data.image)
107 result += ("<img src=\"" +
108 Utils.escapeHtml(theWords.data.image) +
109 "\"/> ");
110 if (theWords.data.title != theWords.data.url)
111 result += ("<i>" +
112 Utils.escapeHtml(theWords.data.title) +
113 "</i>");
114 result += ("<p><tt>" +
115 Utils.escapeHtml(theWords.data.url) +
116 "</tt></p>");
117 } else
118 result = "Takes you to " + Utils.escapeHtml(theWords.text) + ".";
119 pblock.innerHTML = result;
120 },
121 execute: function(theWords) {
122 Utils.openUrlInBrowser(theWords.text);
123 }}
124 );
125
126 function cmd_show_tabs() {
127 var content = context.chromeWindow.document.getElementById("appcontent");
128 var browser = content.firstChild;
129 var tabs = browser.tabContainer;
130 tabs.style.height = "25px";
131 tabs.style.marginTop = "0px";
132 }
133
134 function cmd_hide_tabs() {
135 var content = context.chromeWindow.document.getElementById("appcontent");
136 var browser = content.firstChild;
137 var tabs = browser.tabContainer;
138 tabs.style.height = "0px";
139 tabs.style.marginTop = "-500px";
140 }