build_url_regex renamed to build_url_regexp
[conkeror.git] / modules / page-modes / xkcd.js
blobf02701a66c7c9d2321cf8e31779b9dba5142934d
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 in_module(null);
10 require("content-buffer.js");
12 define_variable('xkcd_add_title', false,
13     "When true, xkcd-mode will insert the title caption of the comic "+
14     "into the page, below the comic.");
16 /**
17  * xkcd_do_add_title adds the XKCD <img> title text below the image in the
18  * page
19  */
20 function xkcd_do_add_title (buffer) {
21     var document = buffer.document;
22     // Find the <img> tag
23     var img = document.evaluate("//div[@id='middleContent']//img",
24         document, null,
25         Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
26     if (!img)
27         return;
28     var title = img.title;
29     // In some comics, the <img> is a link, so walk up to the surrounding <A>
30     if (img.parentNode.tagName == 'A')
31         img = img.parentNode;
32     var node = img.nextSibling;
33     while (node && node.nodeName != 'BR') {
34         node = node.nextSibling;
35     }
36     if (!node)
37         return;
38     // Insert the text inside a <span> with a known ID
39     var text = document.createTextNode(title);
40     var span = document.createElement('span');
41     span.id = 'conkeror:xkcd-title-text';
42     span.appendChild(text);
43     img.parentNode.insertBefore(span, node.nextSibling);
46 define_page_mode("xkcd-mode",
47     build_url_regexp($domain = "xkcd",
48                      $allow_www = true,
49                      $tlds = ["com", "net", "org"],
50                      $path = /(\d+\/)?/),
51     function enable (buffer) {
52         if (xkcd_add_title) {
53             if (buffer.browser.webProgress.isLoadingDocument)
54                 add_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
55             else
56                 xkcd_do_add_title(buffer);
57         }
58         buffer.page.local.browser_relationship_patterns = {};
59         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
60             [new RegExp("\\bnext","i")];
61         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
62             [new RegExp("\\bprev","i")];
63     },
64     function disable (buffer) {
65         remove_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
66         // When we disable the mode, remove the <span>
67         var span = buffer.document.getElementById('conkeror:xkcd-title-text');
68         if (span)
69             span.parentNode.removeChild(span);
70     },
71     $display_name = "XKCD");
73 page_mode_activate(xkcd_mode);
75 provide("xkcd");