2 * (C) Copyright 2009 Shawn Betts
4 * Use, modification, and distribution are subject to the terms specified in the
7 * This provides support for automatically following Wikipedia's "did you mean"
8 * link when searching for an article which doesn't exist.
11 * Put this in your rc file (usually ~/.conkerorrc):
12 * require("wikipedia-didyoumean.js");
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
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
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.");
33 * Called when the Wikipedia search page regexp matches the current URL.
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.
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
46 * @param buffer The buffer containing the document.
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);
55 if ((found = didyoumean.iterateNext())) {
57 doc.location = found.href;
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;
71 add_hook("buffer_loaded_hook", wikipedia_didyoumean);