Debian package: Make description less ambigous; Bump changelog entry date
[conkeror.git] / modules / page-modes / xkcd.js
blobc521fc7bfae1f177f6e689c61fd83cc6e6f323d0
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     $display_name = "XKCD",
48     $enable = function (buffer) {
49         if (xkcd_add_title) {
50             if (buffer.browser.webProgress.isLoadingDocument)
51                 add_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
52             else
53                 xkcd_do_add_title(buffer);
54         }
55         buffer.page.local.browser_relationship_patterns = {};
56         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
57             [new RegExp("\\bnext","i")];
58         buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
59             [new RegExp("\\bprev","i")];
60     },
61     // When we disable the mode, remove the <span>
62     $disable = function (buffer) {
63         remove_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
64         var span = buffer.document.getElementById('conkeror:xkcd-title-text');
65         if (span)
66             span.parentNode.removeChild(span);
67     });
69 let (re = build_url_regex($domain = "xkcd",
70                           $allow_www = true,
71                           $tlds = ["com", "net", "org"],
72                           $path = /(\d+\/)?/)) {
73     auto_mode_list.push([re, xkcd_mode]);
76 provide("xkcd");