[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 | |
---|
| 29 | var PATTERN_RE = new RegExp("<a href=\"/cartoons/\\?id\\=(\\d{8})\"><img.*src\\=\"(http://www\\.userfriendly\\.org/cartoons/archives/[^\"]*)\"\\ "); |
---|
| 30 | |
---|
| 31 | var ANCHOR_RE = new RegExp("http://ars\\.userfriendly\\.org/cartoons/\\?id\\=(\\d{8})\"><IMG ALT=\"Latest Strip\""); |
---|
| 32 | |
---|
| 33 | var PREV_RE = new RegExp("<area[^>]*href=\"/cartoons/\\?id\\=(\\d{8})[^\"]*\" coords=\"[0-9, ]+\" alt=\"Prev[^>]*>"); |
---|
| 34 | |
---|
| 35 | var NEXT_RE = new RegExp("<area[^>]*href=\"/cartoons/\\?id\\=(\\d{8})[^\"]*\" coords=\"[0-9, ]+\" alt=\"\"[^>]*>"); |
---|
| 36 | |
---|
| 37 | function parseDate(str) { |
---|
| 38 | |
---|
| 39 | var m = str.match(/(\d\d\d\d)(\d\d)(\d\d)/); |
---|
| 40 | if (m) { |
---|
| 41 | |
---|
| 42 | return new Date(m[1] + "/" + m[2] + "/" + m[3]); |
---|
| 43 | |
---|
| 44 | } else return new Date(); |
---|
| 45 | |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /* |
---|
| 49 | * Function title(id) should return a title for the comic strip with the given id. |
---|
| 50 | */ |
---|
| 51 | function title(id) { |
---|
| 52 | |
---|
| 53 | return formatDate(parseDate(id)); |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /* |
---|
| 58 | * Function image(id) should return a URL of an image for the comic strip with the given id. |
---|
| 59 | */ |
---|
| 60 | function image(id) { |
---|
| 61 | |
---|
| 62 | page = "http://ars.userfriendly.org/cartoons/?id=" + id + "&mode=classic"; |
---|
| 63 | |
---|
| 64 | s = get(page); |
---|
| 65 | |
---|
| 66 | m = PATTERN_RE.exec(s); |
---|
| 67 | |
---|
| 68 | if (m) |
---|
| 69 | return m[2]; |
---|
| 70 | |
---|
| 71 | return null; |
---|
| 72 | |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Function previous(id) should return the id of a comic strip that is prior to the comic strip |
---|
| 77 | * with a given id or null if there is no such comic. |
---|
| 78 | */ |
---|
| 79 | function previous(id) { |
---|
| 80 | |
---|
| 81 | page = "http://ars.userfriendly.org/cartoons/?id=" + id + "&mode=classic"; |
---|
| 82 | |
---|
| 83 | s = get(page); |
---|
| 84 | |
---|
| 85 | m = PREV_RE.exec(s); |
---|
| 86 | |
---|
| 87 | if (m) { |
---|
| 88 | if (m[1] != id) { |
---|
| 89 | return m[1]; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | return null; |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /* |
---|
| 97 | * Function next(id) should return the id of a comic strip that is next to the comic strip |
---|
| 98 | * with a given id or null if there is no such comic. |
---|
| 99 | */ |
---|
| 100 | function next(id) { |
---|
| 101 | |
---|
| 102 | page = "http://ars.userfriendly.org/cartoons/?id=" + id + "&mode=classic"; |
---|
| 103 | |
---|
| 104 | s = get(page); |
---|
| 105 | |
---|
| 106 | m = NEXT_RE.exec(s); |
---|
| 107 | |
---|
| 108 | if (m) { |
---|
| 109 | if (m[1] != id){ |
---|
| 110 | return m[1]; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | return null; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* |
---|
| 117 | * Function first() should return the id of the first comic strip or null if that information |
---|
| 118 | * cannot be obtained for this comic. |
---|
| 119 | */ |
---|
| 120 | function first() { |
---|
| 121 | |
---|
| 122 | return null; |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /* |
---|
| 127 | * Function newest() should return the id of the most recent comic strip or null if that information |
---|
| 128 | * cannot be obtained for this comic. |
---|
| 129 | */ |
---|
| 130 | function newest() { |
---|
| 131 | |
---|
| 132 | s = get("http://www.userfriendly.org"); |
---|
| 133 | |
---|
| 134 | m = ANCHOR_RE.exec(s); |
---|
| 135 | |
---|
| 136 | if (m) { |
---|
| 137 | newestId = m[1]; |
---|
| 138 | return m[1]; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | return null; |
---|
| 142 | |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* |
---|
| 146 | * if the information is present you can also implement the following functionality currently |
---|
| 147 | * supported by WebStrips (uncoment the functions) |
---|
| 148 | */ |
---|
| 149 | |
---|
| 150 | /* |
---|
| 151 | * Function link(id) should return an url pointing to the page where the comic strip with a |
---|
| 152 | * given id was retrieved from. |
---|
| 153 | */ |
---|
| 154 | |
---|
| 155 | function link(id) { |
---|
| 156 | return "http://ars.userfriendly.org/cartoons/?id=" + id + "&mode=classic"; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|