1 |
self.NicoCache = {}; |
2 |
|
3 |
SAMI.Class.addClassMethods (NicoCache, { |
4 |
withDirList: function (code) { |
5 |
var self = this; |
6 |
|
7 |
if (self.dirList) { |
8 |
code (self.dirList); |
9 |
} |
10 |
|
11 |
new SAMI.XHR ('http://www.nicovideo.jp/cache/dirlist.json', function () { |
12 |
self.dirList = new SAMI.List (this.getJSON ()); |
13 |
code (self.dirList); |
14 |
}).get(); |
15 |
} // withDirList |
16 |
}); // NicoCache class methods |
17 |
|
18 |
NicoCache.Video = new SAMI.Class (function (id) { |
19 |
this.id = id; |
20 |
}, { |
21 |
callAjaxAPI: function (path, query, okCode, ngCode) { |
22 |
new SAMI.XHR ('http://www.nicovideo.jp/cache/ajax_' + path + '?' + query, function () { |
23 |
var res = this.getText (); |
24 |
if (/^OK /.test (res)) { |
25 |
okCode (res.substring (3)); |
26 |
} else { |
27 |
ngCode (res); |
28 |
} |
29 |
}, function () { |
30 |
ngCode (''); |
31 |
}).get (); |
32 |
}, // callAjaxAPI |
33 |
callAjaxAPIWithId: function (path, okCode, ngCode) { |
34 |
var self = this; |
35 |
self.callAjaxAPI (path, encodeURIComponent (self.id), function (res) { |
36 |
okCode (res); |
37 |
}, function () { |
38 |
self.callAjaxAPI (path, encodeURIComponent (self.id) + 'low', function (res) { |
39 |
okCode (res); |
40 |
}, function () { |
41 |
ngCode (); |
42 |
}); |
43 |
}); |
44 |
}, // callAjaxAPIWithId |
45 |
|
46 |
withProperty: function (name, path, code) { |
47 |
var self = this; |
48 |
|
49 |
if (self[name] !== undefined) { |
50 |
code (self[name]); |
51 |
return; |
52 |
} |
53 |
|
54 |
self.callAjaxAPIWithId (path, function (res) { |
55 |
self[name] = res; |
56 |
code (self[name]); |
57 |
}, function () { |
58 |
self[name] = code (); |
59 |
}); |
60 |
}, // withProperty |
61 |
|
62 |
withTitle: function (code) { |
63 |
var self = this; |
64 |
|
65 |
return this.withProperty ('title', 'titlefromcache', function (res) { |
66 |
if (res == null) { |
67 |
code (self.id); |
68 |
return self.id; |
69 |
} else { |
70 |
code (res); |
71 |
} |
72 |
}); |
73 |
}, // withTitle |
74 |
withDirectory: function (code) { |
75 |
var self = this; |
76 |
|
77 |
return this.withProperty ('directory', 'directory', function (res) { |
78 |
if (res == null) { |
79 |
code (null); |
80 |
return null; |
81 |
} else { |
82 |
code (res.replace (/^cache\//, '')); |
83 |
} |
84 |
}); |
85 |
}, // withDirectory |
86 |
|
87 |
setDirectory: function (value, code) { |
88 |
this.callAjaxAPI ('move', encodeURIComponent (this.id) + '-' + encodeURIComponent (value), function (res) { |
89 |
code (false, 'OK ' + res); |
90 |
}, function (res) { |
91 |
code (false, res); |
92 |
}); |
93 |
this.callAjaxAPI ('move', encodeURIComponent (this.id) + 'low-' + encodeURIComponent (value), function (res) { |
94 |
code (true, 'OK ' + res); |
95 |
}, function (res) { |
96 |
code (true, res); |
97 |
}); |
98 |
} // setDirectory |
99 |
}); // Video |
100 |
|
101 |
NicoCache.Page = new SAMI.Class (function (doc) { |
102 |
this.doc = doc; |
103 |
}, { |
104 |
getId: function () { |
105 |
if (this.id) return this.id; |
106 |
|
107 |
var links = this.doc.links; |
108 |
var linksL = links.length; |
109 |
for (var i = 0; i < linksL; i++) { |
110 |
var link = links[i]; |
111 |
var m = link.href.match (/\/cache\/([a-z]{2}[0-9]+)\/movie$/); |
112 |
if (m) { |
113 |
this.id = m[1]; |
114 |
return this.id; |
115 |
} |
116 |
} |
117 |
|
118 |
return null; |
119 |
} // getId |
120 |
}); // Page |
121 |
|
122 |
/* ***** BEGIN LICENSE BLOCK ***** |
123 |
* Copyright 2009 Wakaba <w@suika.fam.cx>. All rights reserved. |
124 |
* |
125 |
* This program is free software; you can redistribute it and/or |
126 |
* modify it under the same terms as Perl itself. |
127 |
* |
128 |
* Alternatively, the contents of this file may be used |
129 |
* under the following terms (the "MPL/GPL/LGPL"), |
130 |
* in which case the provisions of the MPL/GPL/LGPL are applicable instead |
131 |
* of those above. If you wish to allow use of your version of this file only |
132 |
* under the terms of the MPL/GPL/LGPL, and not to allow others to |
133 |
* use your version of this file under the terms of the Perl, indicate your |
134 |
* decision by deleting the provisions above and replace them with the notice |
135 |
* and other provisions required by the MPL/GPL/LGPL. If you do not delete |
136 |
* the provisions above, a recipient may use your version of this file under |
137 |
* the terms of any one of the Perl or the MPL/GPL/LGPL. |
138 |
* |
139 |
* "MPL/GPL/LGPL": |
140 |
* |
141 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
142 |
* |
143 |
* The contents of this file are subject to the Mozilla Public License Version |
144 |
* 1.1 (the "License"); you may not use this file except in compliance with |
145 |
* the License. You may obtain a copy of the License at |
146 |
* <http://www.mozilla.org/MPL/> |
147 |
* |
148 |
* Software distributed under the License is distributed on an "AS IS" basis, |
149 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
150 |
* for the specific language governing rights and limitations under the |
151 |
* License. |
152 |
* |
153 |
* The Original Code is NicoCache-bookmarklets code. |
154 |
* |
155 |
* The Initial Developer of the Original Code is Wakaba. |
156 |
* Portions created by the Initial Developer are Copyright (C) 2009 |
157 |
* the Initial Developer. All Rights Reserved. |
158 |
* |
159 |
* Contributor(s): |
160 |
* Wakaba <w@suika.fam.cx> |
161 |
* |
162 |
* Alternatively, the contents of this file may be used under the terms of |
163 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
164 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
165 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
166 |
* of those above. If you wish to allow use of your version of this file only |
167 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
168 |
* use your version of this file under the terms of the MPL, indicate your |
169 |
* decision by deleting the provisions above and replace them with the notice |
170 |
* and other provisions required by the LGPL or the GPL. If you do not delete |
171 |
* the provisions above, a recipient may use your version of this file under |
172 |
* the terms of any one of the MPL, the GPL or the LGPL. |
173 |
* |
174 |
* ***** END LICENSE BLOCK ***** */ |