[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 align=\"middle\" src\\=\"strips\\/(\\d{4})\\.(gif|jpg)\" alt=\"[^\"]*\" title=\"([^\"]*)\""); |
---|
| 29 | |
---|
| 30 | var ARCHIVE_RE = new RegExp("\">([0-9]+) \\- ([^<]+)</a>", "g"); |
---|
| 31 | |
---|
| 32 | var newestId = null; |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | * Function title(id) should return a title for the comic strip with the given id. |
---|
| 36 | */ |
---|
| 37 | function title(id) { |
---|
| 38 | |
---|
| 39 | i = parseInt(id); |
---|
| 40 | |
---|
| 41 | s = get("http://www.megatokyo.com/strip/" + i); |
---|
| 42 | |
---|
| 43 | m = PATTERN_RE.exec(s); |
---|
| 44 | |
---|
| 45 | if (m) { |
---|
| 46 | n = m[3].match(/(?:")([^&]+)(?:")/); |
---|
| 47 | if (n) |
---|
| 48 | return n[1]; |
---|
| 49 | else return m[3]; |
---|
| 50 | } else |
---|
| 51 | return id; |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /* |
---|
| 56 | * Function image(id) should return a URL of an image for the comic strip with the given id. |
---|
| 57 | */ |
---|
| 58 | function image(id) { |
---|
| 59 | |
---|
| 60 | i = parseInt(id); |
---|
| 61 | |
---|
| 62 | s = get("http://www.megatokyo.com/strip/" + i); |
---|
| 63 | |
---|
| 64 | m = PATTERN_RE.exec(s); |
---|
| 65 | |
---|
| 66 | if (m) { |
---|
| 67 | return "http://www.megatokyo.com/strips/" + m[1] + "." + m[2]; |
---|
| 68 | } else |
---|
| 69 | return null; |
---|
| 70 | |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /* |
---|
| 74 | * Function previous(id) should return the id of a comic strip that is prior to the comic strip |
---|
| 75 | * with a given id or null if there is no such comic. |
---|
| 76 | */ |
---|
| 77 | function previous(id) { |
---|
| 78 | |
---|
| 79 | i = parseInt(id); |
---|
| 80 | |
---|
| 81 | if (i < 2) |
---|
| 82 | return null; |
---|
| 83 | |
---|
| 84 | return i - 1; |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | * Function next(id) should return the id of a comic strip that is next to the comic strip |
---|
| 90 | * with a given id or null if there is no such comic. |
---|
| 91 | */ |
---|
| 92 | function next(id) { |
---|
| 93 | |
---|
| 94 | i = parseInt(id); |
---|
| 95 | |
---|
| 96 | if (newestId == null) |
---|
| 97 | newest(); |
---|
| 98 | |
---|
| 99 | if (newestId != null) { |
---|
| 100 | j = parseInt(newestId); |
---|
| 101 | if (j == i) |
---|
| 102 | return null; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return i + 1; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /* |
---|
| 109 | * Function first() should return the id of the first comic strip or null if that information |
---|
| 110 | * cannot be obtained for this comic. |
---|
| 111 | */ |
---|
| 112 | function first() { |
---|
| 113 | |
---|
| 114 | return 1; |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /* |
---|
| 119 | * Function newest() should return the id of the most recent comic strip or null if that information |
---|
| 120 | * cannot be obtained for this comic. |
---|
| 121 | */ |
---|
| 122 | function newest() { |
---|
| 123 | |
---|
| 124 | s = get("http://www.megatokyo.com/"); |
---|
| 125 | |
---|
| 126 | m = PATTERN_RE.exec(s); |
---|
| 127 | |
---|
| 128 | if (m) { |
---|
| 129 | newestId = m[1]; |
---|
| 130 | return m[1]; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | * if the information is present you can also implement the following functionality currently |
---|
| 137 | * supported by WebStrips (uncoment the functions) |
---|
| 138 | */ |
---|
| 139 | |
---|
| 140 | /* |
---|
| 141 | * Function link(id) should return an url pointing to the page where the comic strip with a |
---|
| 142 | * given id was retrieved from. |
---|
| 143 | */ |
---|
| 144 | |
---|
| 145 | function link(id) { |
---|
| 146 | return "http://www.megatokyo.com/strip/" + id; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | /* |
---|
| 151 | * Function archive(id) should process the archive data from the comic strip with the given id on |
---|
| 152 | * and return the possible new comics using archiveInsert(id, title) and archiveAppend(id, title) |
---|
| 153 | * functions that will only be allowed to call if the archive function is invoked. |
---|
| 154 | * |
---|
| 155 | * archiveInsert(id, title) - inserts the archive entry at the beginning of the newly generated archive |
---|
| 156 | * segment (The last call to this function will insert an entry to the start of the new segment). |
---|
| 157 | * |
---|
| 158 | * archiveAppend(id, title) - inserts the archive entry at the end of the newly generated archive |
---|
| 159 | * segment (The last call to this function will insert an entry to the end of the new segment and thus |
---|
| 160 | * to the end of archive). |
---|
| 161 | */ |
---|
| 162 | |
---|
| 163 | function archive(id) { |
---|
| 164 | |
---|
| 165 | s = get("http://www.megatokyo.com/archive.php?list_by=date"); |
---|
| 166 | |
---|
| 167 | i = parseInt(id); |
---|
| 168 | |
---|
| 169 | start = s.indexOf(format("strip/%04d\">%04d", i, i)); |
---|
| 170 | |
---|
| 171 | ARCHIVE_RE.lastIndex = (start == -1) ? 0 : start; |
---|
| 172 | |
---|
| 173 | while (m = ARCHIVE_RE.exec(s)) { |
---|
| 174 | |
---|
| 175 | j = parseInt(m[1]); |
---|
| 176 | |
---|
| 177 | archiveAppend(j.toString(), m[2]); |
---|
| 178 | |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|