new module to enable editing and deleting of bookmarks
[conkeror/arlinius.git] / modules / page-modes / xkcd.js
blobf25fafee94e2a9b6cd22a0b5d8b49764b2cd9229
1 /**
2  * (C) Copyright 2008 Nelson Elhage
3  * (C) Copyright 2011 Peter Lunicks
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 /*
10  * Tree of important xkcd.com divs:
11  *   #topContainer
12  *     #topLeft
13  *     #topRight
14  *   #middleContainer
15  *     #ctitle: the comic's title
16  *     #comic: the comic
17  *     #transcript
18  *   #bottom
19  *     #comicLinks
20  *     #footnote
21  *     #licenseText
22  */
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.");
38 /**
39  * xkcd_do_add_title adds the XKCD <img> title text below the image in the
40  * page
41  */
42 function xkcd_do_add_title (buffer) {
43     var document = buffer.document;
44     // Find the <img> tag
45     var img = document.evaluate("//div[@id='comic']//img",
46         document, null,
47         Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
48     if (!img)
49         return;
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')
53         img = img.parentNode;
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);
62 /**
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)
66  */
67 function xkcd_rearrange_sections (buffer) {
68     var document = buffer.document;
69     if (xkcd_comic_only)
70         ["topContainer", "bottom"].map(function (elem) { hide_element(buffer, elem) });
71     else
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);
79     } else {
80         let parent = elem.parentNode;
81         dumpln("xkcd: move " + id);
82         parent.removeChild(elem);
83         parent.appendChild(elem);
84     }
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);
91     } else {
92         elem.style.display = "none";
93     }
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);
100     } else {
101         elem.style.display = "";
102     }
106 function xkcd_perhaps_modify_page (buffer) {
107     if (xkcd_add_title)
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",
116                      $allow_www = true,
117                      $tlds = ["com", "net", "org"],
118                      $path = /(\d+\/)?/),
119     function enable (buffer) {
120         if (buffer.browser.webProgress.isLoadingDocument)
121             add_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_perhaps_modify_page);
122         else
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")];
129     },
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');
134         if (span)
135             span.parentNode.removeChild(span);
137         if (xkcd_comic_only)
138             ["topContainer", "bottom"].map(function (elem) { unhide_element(buffer, elem) });
139         else
140             ["middleContainer", "bottom"].map(function (elem) { remove_and_readd_element(buffer, elem) });
141     },
142     $display_name = "XKCD");
144 page_mode_activate(xkcd_mode);
146 provide("xkcd");