isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / client-redirect.jsx
blob281f350162f9e81bd8d3125c9b67e7e563bfaaf0
1 /**
2  * (C) Copyright 2012 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 var redirects = {};
10 function define_client_redirect (name) {
11     var transforms = Array.prototype.slice.call(arguments, 1);
12     function transform (location) {
13         for (var i = 0, n = transforms.length; i < n; ++i) {
14             if (transforms[i] instanceof RegExp) {
15                 if (location instanceof Ci.nsIURI)
16                     var lstr = location.spec;
17                 else
18                     lstr = location;
19                 location = transforms[i].exec(lstr);
20             } else
21                 location = transforms[i](location);
22             if (location == null)
23                 return null;
24         }
25         return location;
26     }
27     redirects[name] = transform;
30 function do_client_redirect (buffer, request, location) {
31     try {
32         location = location.QueryInterface(Ci.nsIURL);
33         for (let [name, handler] in Iterator(redirects)) {
34             var redirect = handler(location);
35             if (redirect) {
36                 var history = buffer.web_navigation.sessionHistory;
37                 var entry = history.getEntryAtIndex(history.index, false);
38                 if (history.index > 0) {
39                     if (entry.URI.spec == location.spec)
40                         history.getEntryAtIndex(history.index - 1, true);
41                 } else if (entry.URI.spec == location.spec)
42                     history.PurgeHistory(1);
43                 buffer.load(redirect);
44                 return;
45             }
46         }
47     } catch (e) {}
50 add_hook('content_buffer_location_change_hook', do_client_redirect);
52 conkeror.define_client_redirect = define_client_redirect;
54 provide("client-redirect");