~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to firefox-extension/chrome/content/beagleSearch.js

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var xmlhttp = new XMLHttpRequest (); 
 
2
var parser = new DOMParser ();
 
3
 
 
4
function init ()
 
5
{
 
6
        var input_box = document.getElementById ("searchinput");
 
7
        input_box.addEventListener ("keypress", InputKeypressHandler, false);
 
8
}
 
9
 
 
10
function cleanup ()
 
11
{
 
12
        var input_box = document.getElementById ("searchinput");
 
13
        input_box.addEventListener ("keypress", InputKeypressHandler, false);
 
14
}
 
15
 
 
16
function InputKeypressHandler (evt)
 
17
{
 
18
        if (evt.which == 13) {
 
19
                evt.stopPropagation ();
 
20
                if (evt.cancelable) {
 
21
                        evt.preventDefault();
 
22
                }
 
23
 
 
24
                Search ();
 
25
        }
 
26
}
 
27
 
 
28
function Search ()
 
29
{
 
30
        var query_str = document.getElementById ("searchinput").value;
 
31
        //alert ("Searching for '" + query_str + "'");
 
32
        if (query_str.length == 0) {
 
33
                return;
 
34
        } else if (query_str == '42') {
 
35
                window.location = "http://en.wikipedia.org/wiki/The_Answer_to_Life,_the_Universe,_and_Everything";
 
36
                return;
 
37
        } else if (query_str == '4u7h0rz') {
 
38
                window.location = "http://svn.gnome.org/viewvc/beagle/trunk/beagle/AUTHORS?view=markup";
 
39
                return;
 
40
        }
 
41
 
 
42
        var req_string = '<?xml version="1.0" encoding="utf-8"?> <RequestWrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Message xsi:type="Query"> <IsIndexListener>false</IsIndexListener> <Parts> <Part xsi:type="QueryPart_Human"> <Logic>Required</Logic> <QueryString>type:WebHistory OR type:Bookmark</QueryString> </Part><Part xsi:type="QueryPart_Human"> <Logic>Required</Logic> <QueryString>' + query_str + '</QueryString> </Part> </Parts> <QueryDomain>Local System</QueryDomain> <MaxHits>20</MaxHits> </Message> </RequestWrapper> ';
 
43
 
 
44
        xmlhttp.onreadystatechange = state_change_search;
 
45
        xmlhttp.onerror = error_handler;
 
46
        // If cross-site problem occurs,
 
47
        // http://blog.dirolf.com/2007/06/enabling-cross-domain-ajax-in-firefox.html
 
48
        xmlhttp.open ("POST", "http://localhost:4000/", true);
 
49
        //XHR binary charset opt by mgran 2006 [http://mgran.blogspot.com]
 
50
        xmlhttp.overrideMimeType ('text/txt; charset=utf-8'); // if charset is changed, need to handle bom
 
51
        //xmlhttp.overrideMimeType('text/txt; charset=x-user-defined');
 
52
        xmlhttp.send (req_string);
 
53
        document.getElementById ("searchinput").disabled = true;
 
54
 
 
55
        return;
 
56
}
 
57
 
 
58
function error_handler (e)
 
59
{
 
60
        var result_list = document.getElementById ("resultlist");
 
61
        // FIXME: i18n
 
62
        result_list.innerHTML = "<html:b>Error!</html:b><html:br />Beagle service needs to be running with the web interface enabled.<html:br /><html:a href='http://beagle-project.org/Beagle_Webinterface' onclick='return openlink(\"http://beagle-project.org/Beagle_Webinterface\");'>Beagle Webinterface</html:a>";
 
63
        document.getElementById ("searchinput").disabled = false;
 
64
}
 
65
 
 
66
function state_change_search ()
 
67
{
 
68
        if (xmlhttp.readyState == 4)
 
69
                HandleResults ();
 
70
}
 
71
 
 
72
function HandleResults ()
 
73
{
 
74
        if (xmlhttp.status != 200) {
 
75
                error_handler ();
 
76
                return;
 
77
        }
 
78
 
 
79
        document.getElementById ("searchinput").disabled = false;
 
80
 
 
81
        //dump("Response:\n");
 
82
        //dump(xmlhttp.responseText);
 
83
        //dump("\n");
 
84
        res = xmlhttp.responseText;
 
85
 
 
86
        // if charset is x-user-defined split by \uF7FF
 
87
        // if charset is utf-8, split by FFFD
 
88
        // And dont ask me why!
 
89
        var responses = res.split ('\uFFFD'); 
 
90
 
 
91
        var result_str = "<html:ul>";
 
92
 
 
93
        var no_result = true;
 
94
 
 
95
        // Process hit xml nodes with xsl and append with javascript
 
96
        for (var i = 0; i < responses.length; ++i) {
 
97
                if (responses [i].length <= 0)  {
 
98
                        continue;
 
99
                }
 
100
 
 
101
                var response_dom = parser.parseFromString (responses [i], "text/xml");
 
102
                var msg_node = response_dom.getElementsByTagName ("Message") [0];
 
103
                if (msg_node.getAttributeNS ('http://www.w3.org/2001/XMLSchema-instance', 'type') != 'HitsAddedResponse')
 
104
                        continue;
 
105
 
 
106
                var hits = msg_node.getElementsByTagName ("Hit");
 
107
                no_result &= (hits.length == 0);
 
108
 
 
109
                for (var j = 0; j < hits.length; ++j) {
 
110
                        var uri = hits [j].getAttribute ("Uri");
 
111
                        uri = EscapeAmpersand (uri); // XHTML gotcha
 
112
                        var title = null;
 
113
                        var identifier = null;
 
114
                        var bookmark = false;
 
115
 
 
116
                        var properties = hits [j].getElementsByTagName ("Property");
 
117
                        for (var k = 0; k < properties.length; ++k) {
 
118
                                var key = properties [k].getAttribute ("Key");
 
119
 
 
120
                                if (key == "beagle:HitType" && (properties [k].getAttribute ("Value") == "Bookmark")) {
 
121
                                        bookmark = true;
 
122
                                        continue;
 
123
                                }
 
124
 
 
125
                                if (key == "dc:title") {
 
126
                                        title = properties [k].getAttribute ("Value");
 
127
                                        //title = reduce (title, 40, "..."); // FIXME
 
128
                                        continue;
 
129
                                }
 
130
 
 
131
                                if (key == "dc:identifier")
 
132
                                        identifier = properties [k].getAttribute ("Value");
 
133
                        }
 
134
 
 
135
                        if (bookmark)
 
136
                                uri = identifier;
 
137
 
 
138
                        if (title == null)
 
139
                                title = uri.substr (0, 40) + "...";
 
140
 
 
141
                        //dump (uri + "," + title + "\n");
 
142
                        result_str += "<html:li>";
 
143
 
 
144
                        if (bookmark)
 
145
                                result_str += "<html:span>B</html:span>";
 
146
 
 
147
                        result_str += "<html:a href='";
 
148
                        result_str += uri;
 
149
                        result_str += "' onclick='return openlink(\"";
 
150
                        result_str += uri;
 
151
                        result_str += "\");'>";
 
152
                        result_str += title;
 
153
                        result_str += "</html:a></html:li>";
 
154
                }
 
155
        }
 
156
 
 
157
        if (no_result)
 
158
                result_str = "<html:b>No web pages found.</html:b>";
 
159
        else
 
160
                result_str += "</html:ul>";
 
161
        //dump (result_str);
 
162
        //dump ("\n");
 
163
 
 
164
        var result_list = document.getElementById ("resultlist");
 
165
        result_list.innerHTML = result_str;
 
166
}
 
167
 
 
168
// From http://sastools.com/b2/post/79394063
 
169
function reduce(str,l,p)
 
170
{
 
171
        var words=str.split(" ");
 
172
        var numWords=words.length;
 
173
        var output=[];
 
174
        var ol,cWord,w;
 
175
        for(w=0; w<numwords; ++w)
 
176
        {
 
177
                cWord=words[w];
 
178
                cwl=cWord.length;
 
179
                if((ol+cwl)<=l)
 
180
                {
 
181
                        output.push(cWord);
 
182
                        ol+=cwl+1;
 
183
                }
 
184
                else
 
185
                        break;
 
186
        }
 
187
        return output.join(" ")+p;
 
188
}
 
189
 
 
190
function openlink (link)
 
191
{
 
192
        var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
 
193
                                .getInterface(Components.interfaces.nsIWebNavigation)
 
194
                                .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
 
195
                                .rootTreeItem
 
196
                                .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
 
197
                                .getInterface(Components.interfaces.nsIDOMWindow);
 
198
 
 
199
        var browser = mainWindow.getBrowser ();
 
200
        browser.selectedTab = browser.addTab (link);
 
201
 
 
202
        return false;
 
203
}
 
204
 
 
205
function EscapeAmpersand (url)
 
206
{
 
207
        return url.replace (/&/g, "&amp;");
 
208
}