bind "C-x left" and "C-x right" to buffer-previous and buffer-next
[conkeror/arlinius.git] / modules / page-modes / xkcd.js
blob9cf4f6458b7dea1d2944115dcd1e577cfe10aab6
1 /**
2  * (C) Copyright 2008 Nelson Elhage
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 require("content-buffer.js");
10 define_variable(
11     'xkcd_add_title', false,
12     "When true, xkcd-mode will insert the title caption of the comic "+
13     "into the page, below the comic.");
15 /* Add the XKCD <img> title text below the image in the page */
16 function xkcd_do_add_title(buffer) {
17     var document = buffer.document;
18     // Find the <img> tag
19     var img = document.evaluate(
20         "//div[@id='middleContent']//img",
21         document, null,
22         Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
23     if(!img) return;
24     var title = img.title;
25     // In some comics, the <img> is a link, so walk up to the surrounding <A>
26     if(img.parentNode.tagName == 'A') {
27         img = img.parentNode;
28     }
29     var node = img.nextSibling;
30     while(node && node.nodeName != 'BR') {
31         node = node.nextSibling;
32     }
33     if(!node) return;
34     // Insert the text inside a <span> with a known ID
35     var text = document.createTextNode(title);
36     var span = document.createElement('span');
37     span.id = 'conkeror:xkcd-title-text';
38     span.appendChild(text);
39     img.parentNode.insertBefore(span, node.nextSibling);
42 define_page_mode("xkcd_mode",
43     $display_name = "XKCD",
44     $enable = function (buffer) {
45         if (xkcd_add_title) {
46             if(buffer.browser.webProgress.isLoadingDocument) {
47                 add_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_do_add_title);
48             } else {
49                 xkcd_do_add_title(buffer);
50             }
51         }
52         buffer.page.local.browser_relationship_patterns = {};
53         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
54             [new RegExp("\\bnext","i")];
55         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
56             [new RegExp("\\bprev","i")];
57     },
58     // When we disable the mode, remove the <span>
59     $disable = function(buffer) {
60         remove_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_do_add_title);
61         var span = buffer.document.getElementById('conkeror:xkcd-title-text');
62         if(span) {
63             span.parentNode.removeChild(span);
64         }
65     });
67 var xkcd_re = build_url_regex($domain = "xkcd",
68                               $allow_www = true,
69                               $tlds = ["com", "net", "org"],
70                               $path = /(\d+\/)?/);
71 auto_mode_list.push([xkcd_re, xkcd_mode]);