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='comic']//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 // Insert the text inside a <p> with a known ID
31 var text = document.createTextNode(title);
32 var span = document.createElement('p');
33 span.id = 'conkeror:xkcd-title-text';
34 span.appendChild(text);
35 img.parentNode.appendChild(span);
38 define_page_mode("xkcd-mode",
39 build_url_regexp($domain = "xkcd",
41 $tlds = ["com", "net", "org"],
43 function enable (buffer) {
45 if (buffer.browser.webProgress.isLoadingDocument)
46 add_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
48 xkcd_do_add_title(buffer);
50 buffer.page.local.browser_relationship_patterns = {};
51 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
52 [new RegExp("\\bnext","i")];
53 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
54 [new RegExp("\\bprev","i")];
56 function disable (buffer) {
57 remove_hook.call(buffer, "buffer_loaded_hook", xkcd_do_add_title);
58 // When we disable the mode, remove the <span>
59 var span = buffer.document.getElementById('conkeror:xkcd-title-text');
61 span.parentNode.removeChild(span);
63 $display_name = "XKCD");
65 page_mode_activate(xkcd_mode);