[1] | 1 | /* |
---|
| 2 | * A template for JavaScript comic plugin for WebStrips |
---|
| 3 | * |
---|
| 4 | * License: |
---|
| 5 | * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or (at |
---|
| 9 | * your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, but |
---|
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 14 | * Public License for more details. |
---|
| 15 | * |
---|
| 16 | * http://www.opensource.org/licenses/gpl-license.php |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License along |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 21 | * |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /* |
---|
| 25 | * place any global variables and support functions here ... |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | var PATTERN_RE = new RegExp("<img src=\"(http\\://imgs[^\"]+)\" title=\"([^\"]*)\" alt=\"([^\"]*)\" />"); |
---|
| 29 | |
---|
| 30 | var ANCHOR_RE = new RegExp("this comic\\: http\\://xkcd\\.com/(\\d*)/"); |
---|
| 31 | |
---|
| 32 | var ARCHIVE_RE = new RegExp("<a href=\"/(\\d*)/\" title=\"[^\"]*\">([^<]*)</a><br */>", "g"); |
---|
| 33 | |
---|
| 34 | var newestId = null; |
---|
| 35 | |
---|
| 36 | /* |
---|
| 37 | * Function title(id) should return a title for the comic strip with the given id. |
---|
| 38 | */ |
---|
| 39 | function title(id) { |
---|
| 40 | |
---|
| 41 | i = parseInt(id); |
---|
| 42 | |
---|
| 43 | s = get("http://xkcd.com/" + i + "/"); |
---|
| 44 | |
---|
| 45 | m = PATTERN_RE.exec(s); |
---|
| 46 | |
---|
| 47 | if (m) |
---|
| 48 | return m[3]; |
---|
| 49 | else |
---|
| 50 | return null; |
---|
| 51 | |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /* |
---|
| 55 | * Function image(id) should return a URL of an image for the comic strip with the given id. |
---|
| 56 | */ |
---|
| 57 | function image(id) { |
---|
| 58 | |
---|
| 59 | i = parseInt(id); |
---|
| 60 | |
---|
| 61 | s = get("http://xkcd.com/" + i + "/"); |
---|
| 62 | |
---|
| 63 | m = PATTERN_RE.exec(s); |
---|
| 64 | |
---|
| 65 | if (m) |
---|
| 66 | return m[1]; |
---|
| 67 | else |
---|
| 68 | return null; |
---|
| 69 | |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /* |
---|
| 73 | * Function previous(id) should return the id of a comic strip that is prior to the comic strip |
---|
| 74 | * with a given id or null if there is no such comic. |
---|
| 75 | */ |
---|
| 76 | function previous(id) { |
---|
| 77 | |
---|
| 78 | i = parseInt(id); |
---|
| 79 | |
---|
| 80 | if (i < 2) |
---|
| 81 | return null; |
---|
| 82 | |
---|
| 83 | return parseInt(i - 1); |
---|
| 84 | |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /* |
---|
| 88 | * Function next(id) should return the id of a comic strip that is next to the comic strip |
---|
| 89 | * with a given id or null if there is no such comic. |
---|
| 90 | */ |
---|
| 91 | function next(id) { |
---|
| 92 | |
---|
| 93 | i = parseInt(id); |
---|
| 94 | |
---|
| 95 | if (newestId == null) |
---|
| 96 | newest(); |
---|
| 97 | |
---|
| 98 | if (newestId != null) { |
---|
| 99 | j = parseInt(newestId); |
---|
| 100 | if (j == i) |
---|
| 101 | return null; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return parseInt(i + 1); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /* |
---|
| 108 | * Function first() should return the id of the first comic strip or null if that information |
---|
| 109 | * cannot be obtained for this comic. |
---|
| 110 | */ |
---|
| 111 | function first() { |
---|
| 112 | |
---|
| 113 | return 1; |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /* |
---|
| 118 | * Function newest() should return the id of the most recent comic strip or null if that information |
---|
| 119 | * cannot be obtained for this comic. |
---|
| 120 | */ |
---|
| 121 | function newest() { |
---|
| 122 | |
---|
| 123 | s = get("http://xkcd.com/"); |
---|
| 124 | |
---|
| 125 | m = ANCHOR_RE.exec(s); |
---|
| 126 | |
---|
| 127 | if (m) { |
---|
| 128 | newestId = m[1]; |
---|
| 129 | return m[1]; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return null; |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /* |
---|
| 137 | * if the information is present you can also implement the following functionality currently |
---|
| 138 | * supported by WebStrips (uncoment the functions) |
---|
| 139 | */ |
---|
| 140 | |
---|
| 141 | /* |
---|
| 142 | * Function link(id) should return an url pointing to the page where the comic strip with a |
---|
| 143 | * given id was retrieved from. |
---|
| 144 | */ |
---|
| 145 | |
---|
| 146 | function link(id) { |
---|
| 147 | return "http://xkcd.com/" + id + "/"; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | /* |
---|
| 152 | * Function archive(id) should process the archive data from the comic strip with the given id on |
---|
| 153 | * and return the possible new comics using archiveInsert(id, title) and archiveAppend(id, title) |
---|
| 154 | * functions that will only be allowed to call if the archive function is invoked. |
---|
| 155 | * |
---|
| 156 | * archiveInsert(id, title) - inserts the archive entry at the beginning of the newly generated archive |
---|
| 157 | * segment (The last call to this function will insert an entry to the start of the new segment). |
---|
| 158 | * |
---|
| 159 | * archiveAppend(id, title) - inserts the archive entry at the end of the newly generated archive |
---|
| 160 | * segment (The last call to this function will insert an entry to the end of the new segment and thus |
---|
| 161 | * to the end of archive). |
---|
| 162 | */ |
---|
| 163 | |
---|
| 164 | function archive(id) { |
---|
| 165 | s = get("http://xkcd.com/archive/"); |
---|
| 166 | |
---|
| 167 | ARCHIVE_RE.lastIndex = 0; |
---|
| 168 | |
---|
| 169 | i = parseInt(id); |
---|
| 170 | |
---|
| 171 | while (m = ARCHIVE_RE.exec(s)) { |
---|
| 172 | |
---|
| 173 | archiveInsert(m[1], m[2]); |
---|
| 174 | |
---|
| 175 | j = parseInt(m[1]); |
---|
| 176 | |
---|
| 177 | if (id != null && i == j) |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|