view-as-mime-type: fix bug in interactive declaration
[conkeror.git] / modules / wikipedia-didyoumean.js
blob5478a8c5d450a9d38701ed9479a8cee0d723b68d
1 /**
2  * (C) Copyright 2009 Shawn Betts
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6  *
7  * This provides support for automatically following Wikipedia's "did you mean"
8  * link when searching for an article which doesn't exist.
9  *
10  * Usage:
11  * Put this in your rc file (usually ~/.conkerorrc):
12  * require("wikipedia-didyoumean.js");
13  *
14  * TODO:
15  * - With this enabled, each time a buffer has loaded, the function is run,
16  *   which is quite unnecessary. Find out a better way to solve this. (Is there
17  *   any better way?)
18  * - There are no indications to the user that Conkeror is actually following a
19  *   link, and following it doesn't happen that fast on most Internet
20  *   connections. One alternative is to replace the document with something
21  *   completely different telling the user where she is being redirected while
22  *   it's taking place, but I'd personally like something which is not as
23  *   obtrusive.
24  */
26 require("utils.js");
28 define_variable("wikipedia_didyoumean_follow_first_hit", false,
29                 "When non-false, follows the first hit in the result list"
30                 + "unless a \"did you mean\" in shown.");
32 /**
33  * Called when the Wikipedia search page regexp matches the current URL.
34  *
35  * Given the buffer, searches the document for a "did you mean" suggestion box,
36  * which suggests some word that the user might have meant. If such a suggestion
37  * is found, it immediately follows it. If the new page didn't exist either, and
38  * just so happens to have another suggestion, follows that, and so on, until no
39  * more suggestions are found.
40  *
41  * If the user variable "wikipedia_didyoumean_follow_first_hit" is set to
42  * anything which is "true" in a JavaScript condition, when no more suggestions
43  * are found, follows the first match in the search results, if there are any
44  * matches.
45  *
46  * @param buffer The buffer containing the document.
47  */
48 function wikipedia_didyoumean(buffer) {
49     let uri = buffer.current_URI.spec;
50     if (uri.match(new RegExp("^http://[a-z]+\.wikipedia\.org/w(iki)?/.+"))) {
51         let doc = buffer.document;
52         let didyoumean_xpath = '//div[@class="searchdidyoumean"]/a[1]';
53         let didyoumean = xpath_lookup(doc, didyoumean_xpath);
54         let found;
55         if ((found = didyoumean.iterateNext())) {
56             // Did you mean...?
57             doc.location = found.href;
58         } else {
59             // Follow the first hit if wikipedia_didyoumean_follow_first_hit is true.
60             if (wikipedia_didyoumean_follow_first_hit) {
61                 let firsthit_xpath = '//ul[@class="mw-search-results"]/li[1]/a';
62                 let firsthit = xpath_lookup(doc, firsthit_xpath);
63                 if ((found = firsthit.iterateNext())) {
64                     doc.location = found.href;
65                 }
66             }
67         }
68     }
71 add_hook("buffer_loaded_hook", wikipedia_didyoumean);