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