2 * (C) Copyright 2008 Nelson Elhage
4 * Use, modification, and distribution are subject to the terms specified in the
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.");
15 * xkcd_do_add_title adds the XKCD <img> title text below the image in the
18 function xkcd_do_add_title (buffer) {
19 var document = buffer.document;
21 var img = document.evaluate("//div[@id='middleContent']//img",
23 Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
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')
30 var node = img.nextSibling;
31 while (node && node.nodeName != 'BR') {
32 node = node.nextSibling;
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",
47 $tlds = ["com", "net", "org"],
49 function enable (buffer) {
51 if (buffer.browser.webProgress.isLoadingDocument)
52 add_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
54 xkcd_do_add_title(buffer);
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")];
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');
67 span.parentNode.removeChild(span);
69 $display_name = "XKCD");
71 page_mode_activate(xkcd_mode);