2 * (C) Copyright 2008 Nelson Elhage
4 * Use, modification, and distribution are subject to the terms specified in the
8 require("content-buffer.js");
11 'xkcd_add_title', false,
12 "When true, xkcd-mode will insert the title caption of the comic "+
13 "into the page, below the comic.");
15 /* Add the XKCD <img> title text below the image in the page */
16 function xkcd_do_add_title(buffer) {
17 var document = buffer.document;
19 var img = document.evaluate(
20 "//div[@id='middleContent']//img",
22 Ci.nsIDOMXPathResult.ANY_TYPE,null).iterateNext();
24 var title = img.title;
25 // In some comics, the <img> is a link, so walk up to the surrounding <A>
26 if(img.parentNode.tagName == 'A') {
29 var node = img.nextSibling;
30 while(node && node.nodeName != 'BR') {
31 node = node.nextSibling;
34 // Insert the text inside a <span> with a known ID
35 var text = document.createTextNode(title);
36 var span = document.createElement('span');
37 span.id = 'conkeror:xkcd-title-text';
38 span.appendChild(text);
39 img.parentNode.insertBefore(span, node.nextSibling);
42 define_page_mode("xkcd_mode",
43 $display_name = "XKCD",
44 $enable = function (buffer) {
46 if(buffer.browser.webProgress.isLoadingDocument) {
47 add_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_do_add_title);
49 xkcd_do_add_title(buffer);
52 buffer.page.local.browser_relationship_patterns = {};
53 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_NEXT] =
54 [new RegExp("\\bnext","i")];
55 buffer.page.local.browser_relationship_patterns[RELATIONSHIP_PREVIOUS] =
56 [new RegExp("\\bprev","i")];
58 // When we disable the mode, remove the <span>
59 $disable = function(buffer) {
60 remove_hook.call(buffer, "buffer_dom_content_loaded_hook", xkcd_do_add_title);
61 var span = buffer.document.getElementById('conkeror:xkcd-title-text');
63 span.parentNode.removeChild(span);
67 var xkcd_re = build_url_regex($domain = "xkcd",
69 $tlds = ["com", "net", "org"],
71 auto_mode_list.push([xkcd_re, xkcd_mode]);