source: comics/GirlGenius/comic.js @ 1:4e64bf1747ce

Revision 1:4e64bf1747ce, 3.8 KB checked in by lukacu, 14 years ago (diff)

Core project and comics migration

Line 
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
28var NEWEST_RE = new RegExp("http\\://www\\.girlgeniusonline\\.com/ggmain/strips/ggmain(\\d{8})\\.jpg");
29
30var FIRST_RE = new RegExp("<a href\\=(?:\"|')http\\://www\\.girlgeniusonline\\.com/comic\\.php\\?date\\=(\\d{8})(?:\"|') *><img[^>]*first");
31
32var PREV_RE = new RegExp("<a href\\=(?:\"|')http\\://www\\.girlgeniusonline\\.com/comic\\.php\\?date\\=(\\d{8})(?:\"|') *><img[^>]*prev");
33
34var NEXT_RE = new RegExp("<a href\\=(?:\"|')http\\://www\\.girlgeniusonline\\.com/comic\\.php\\?date\\=(\\d{8})(?:\"|') *><img[^>]*next");
35
36// BUGFIX: quick and dirty solution to redirect pages bug
37function getHtml(url) {
38   
39    for (i = 0; i < 10; i++) {
40       
41        s = get(url);
42       
43        if (s == null)
44            return null;
45       
46        if (s.length < 200) {
47            sleep(200);
48        }
49        return s;
50    }
51    return null;
52}
53
54function parseDate(str) {
55
56    var m = str.match(/(\d\d\d\d)(\d\d)(\d\d)/);
57    if (m) {
58
59        return new Date(m[1] + "/" + m[2] + "/" + m[3]);
60   
61    } else return new Date();
62   
63}
64
65/*
66 * Function title(id) should return a title for the comic strip with the given id.
67 */
68function title(id) {
69
70    return formatDate(parseDate(id));
71
72}
73
74/*
75 * Function image(id) should return a URL of an image for the comic strip with the given id.
76 */
77function image(id) {
78
79    return "http://www.girlgeniusonline.com/ggmain/strips/ggmain" + id + ".jpg";
80   
81}
82
83/*
84 * Function previous(id) should return the id of a comic strip that is prior to the comic strip
85 * with a given id or null if there is no such comic.
86 */
87function previous(id) {
88
89    page = "http://www.girlgeniusonline.com/comic.php?date=" + id;
90
91    s = getHtml(page);
92
93    m = PREV_RE.exec(s);
94
95    if (m) {
96
97        return m[1];
98
99    }
100    return null;
101
102}
103
104/*
105 * Function next(id) should return the id of a comic strip that is next to the comic strip
106 * with a given id or null if there is no such comic.
107 */
108function next(id) {
109
110    page = "http://www.girlgeniusonline.com/comic.php?date=" + id;
111
112    s = getHtml(page);
113
114    m = NEXT_RE.exec(s);
115
116    if (m) {
117        return m[1];
118
119    }
120    return null;
121}
122
123/*
124 * Function first() should return the id of the first comic strip or null if that information
125 * cannot be obtained for this comic.
126 */
127function first() {
128
129        s = getHtml("http://www.girlgeniusonline.com/comic.php");
130       
131        m = FIRST_RE.exec(s);
132       
133        if (m) {
134
135            return m[1];
136        }
137       
138        return null;
139}
140
141/*
142 * Function newest() should return the id of the most recent comic strip or null if that information
143 * cannot be obtained for this comic.
144 */
145function newest() {
146   
147    s = getHtml("http://www.girlgeniusonline.com/comic.php");
148
149    m = NEWEST_RE.exec(s);
150
151    if (m) {
152        return m[1];
153    }
154
155    return null;
156
157}
158
159/*
160 * if the information is present you can also implement the following functionality currently
161 * supported by WebStrips (uncoment the functions)
162 */
163
164/*
165 * Function link(id) should return an url pointing to the page where the comic strip with a
166 * given id was retrieved from.
167 */
168
169function link(id) {
170    return "http://www.girlgeniusonline.com/comic.php?date=" + id;
171}
172
173
Note: See TracBrowser for help on using the repository browser.