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