2 * (C) Copyright 2008 Nelson Elhage
3 * (C) Copyright 2011 Peter Lunicks
5 * Use, modification, and distribution are subject to the terms specified in the
10 * Tree of important xkcd.com divs:
15 * #ctitle: the comic's title
24 require("content-buffer.js");
26 define_variable('xkcd_add_title', false,
27 "When true, xkcd-mode will insert the title caption of the comic "+
28 "into the page, below the comic.");
30 define_variable('xkcd_comic_at_top', false,
31 "When true, xkcd-mode will move the header section of the page to"+
32 "below the comic section.");
34 define_variable('xkcd_comic_only', false,
35 "When true, xkcd-mode will remove the header and footer sections"+
36 "of the page, leaving only the comic section.");
39 * xkcd_do_add_title adds the XKCD <img> title text below the image in the
42 function xkcd_do_add_title (buffer) {
43 var document = buffer.document;
45 var img = document.evaluate("//div[@id='comic']//img",
47 Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
50 var title = img.title;
51 // In some comics, the <img> is a link, so walk up to the surrounding <A>
52 if (img.parentNode.tagName == 'A')
54 // Insert the text inside a <p> with a known ID
55 var text = document.createTextNode(title);
56 var span = document.createElement('p');
57 span.id = 'conkeror:xkcd-title-text';
58 span.appendChild(text);
59 img.parentNode.appendChild(span);
63 * xkcd_rearrange_sections rearranges the sections of the page to put the
64 * comic at the top and possibly deleting the other sections (depending on
65 * the value of xkcd_comic_only)
67 function xkcd_rearrange_sections (buffer) {
68 var document = buffer.document;
70 ["topContainer", "bottom"].map(function (elem) { hide_element(buffer, elem) });
72 ["topContainer", "bottom"].map(function (elem) { remove_and_readd_element(buffer, elem) });
75 function remove_and_readd_element(buffer, id) {
76 var elem = buffer.document.getElementById(id);
77 if (! (elem instanceof Ci.nsIDOMNode)) {
78 dumpln("xkcd: failed to find dom node for " + id);
80 let parent = elem.parentNode;
81 dumpln("xkcd: move " + id);
82 parent.removeChild(elem);
83 parent.appendChild(elem);
87 function hide_element(buffer, id) {
88 var elem = buffer.document.getElementById(id);
89 if (! (elem instanceof Ci.nsIDOMNode)) {
90 dumpln("xkcd: failed to find dom node for " + id);
92 elem.style.display = "none";
96 function unhide_element(buffer, id) {
97 var elem = buffer.document.getElementById(id);
98 if (! (elem instanceof Ci.nsIDOMNode)) {
99 dumpln("xkcd: failed to find dom node for " + id);
101 elem.style.display = "";
106 function xkcd_perhaps_modify_page (buffer) {
108 xkcd_do_add_title(buffer);
110 if (xkcd_comic_only || xkcd_comic_at_top)
111 xkcd_rearrange_sections(buffer);
114 define_page_mode("xkcd-mode",
115 build_url_regexp($domain = "xkcd",
117 $tlds = ["com", "net", "org"],
119 function enable (buffer) {
120 if (buffer.browser.webProgress.isLoadingDocument)
121 add_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_perhaps_modify_page);
123 xkcd_perhaps_modify_page(buffer);
124 buffer.page.local.browser_relationship_patterns = {};
125 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
126 [new RegExp("\\bnext","i")];
127 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
128 [new RegExp("\\bprev","i")];
130 function disable (buffer) {
131 remove_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_perhaps_modify_page);
132 // When we disable the mode, remove the <span> and undo div rearranging
133 var span = buffer.document.getElementById('conkeror:xkcd-title-text');
135 span.parentNode.removeChild(span);
138 ["topContainer", "bottom"].map(function (elem) { unhide_element(buffer, elem) });
140 ["middleContainer", "bottom"].map(function (elem) { remove_and_readd_element(buffer, elem) });
142 $display_name = "XKCD");
144 page_mode_activate(xkcd_mode);