|
126 | 126 |
|
127 | 127 |
|
128 | 128 | var jsonpCallbackCounter = 0;
|
| 129 | + /** |
| 130 | + * Makes a JSONP request. |
| 131 | + * |
| 132 | + * @param {string} src The URL of the resource to request. |
| 133 | + * @param {string | undefined | null} callbackParameter The name of the callback parameter. If falsy, `"callback"` |
| 134 | + * will be used. |
| 135 | + * @param {(data: unknown) => void} onSuccess |
| 136 | + * @param {(reason: "timeout" | "network") => void} onError |
| 137 | + * @returns {void} |
| 138 | + */ |
| 139 | + function jsonp(src, callbackParameter, onSuccess, onError) { |
| 140 | + var callbackName = 'prismjsonp' + jsonpCallbackCounter++; |
| 141 | + |
| 142 | + var uri = document.createElement('a'); |
| 143 | + uri.href = src; |
| 144 | + uri.href += (uri.search ? '&' : '?') + (callbackParameter || 'callback') + '=' + callbackName; |
| 145 | + |
| 146 | + var script = document.createElement('script'); |
| 147 | + script.src = uri.href; |
| 148 | + script.onerror = function () { |
| 149 | + cleanup(); |
| 150 | + onError('network'); |
| 151 | + }; |
| 152 | + |
| 153 | + var timeoutId = setTimeout(function () { |
| 154 | + cleanup(); |
| 155 | + onError('timeout'); |
| 156 | + }, Prism.plugins.jsonphighlight.timeout); |
| 157 | + |
| 158 | + function cleanup() { |
| 159 | + clearTimeout(timeoutId); |
| 160 | + document.head.removeChild(script); |
| 161 | + delete window[callbackName]; |
| 162 | + } |
| 163 | + |
| 164 | + // the JSONP callback function |
| 165 | + window[callbackName] = function (response) { |
| 166 | + cleanup(); |
| 167 | + onSuccess(response); |
| 168 | + }; |
| 169 | + |
| 170 | + document.head.appendChild(script); |
| 171 | + } |
129 | 172 |
|
130 | 173 | var LOADING_MESSAGE = 'Loading…';
|
131 | 174 | var MISSING_ADAPTER_MESSAGE = function (name) {
|
|
185 | 228 | }
|
186 | 229 | }
|
187 | 230 |
|
188 |
| - var callbackName = 'prismjsonp' + jsonpCallbackCounter++; |
189 |
| - |
190 |
| - var uri = document.createElement('a'); |
191 |
| - var src = uri.href = pre.getAttribute('data-jsonp'); |
192 |
| - uri.href += (uri.search ? '&' : '?') + (pre.getAttribute('data-callback') || 'callback') + '=' + callbackName; |
193 |
| - |
194 |
| - |
195 |
| - var timeout = setTimeout(function () { |
196 |
| - // we could clean up window[cb], but if the request finally succeeds, keeping it around is a good thing |
197 |
| - |
198 |
| - // mark as failed |
199 |
| - pre.setAttribute(STATUS_ATTR, STATUS_FAILED); |
200 |
| - |
201 |
| - code.textContent = TIMEOUT_MESSAGE(src); |
202 |
| - }, Prism.plugins.jsonphighlight.timeout); |
203 |
| - |
| 231 | + var src = pre.getAttribute('data-jsonp'); |
| 232 | + |
| 233 | + jsonp( |
| 234 | + src, |
| 235 | + pre.getAttribute('data-callback'), |
| 236 | + function (response) { |
| 237 | + // interpret the received data using the adapter(s) |
| 238 | + var data = null; |
| 239 | + if (adapter) { |
| 240 | + data = adapter(response, pre); |
| 241 | + } else { |
| 242 | + for (var i = 0, l = adapters.length; i < l; i++) { |
| 243 | + data = adapters[i].adapter(response, pre); |
| 244 | + if (data !== null) { |
| 245 | + break; |
| 246 | + } |
| 247 | + } |
| 248 | + } |
204 | 249 |
|
205 |
| - var script = document.createElement('script'); |
206 |
| - script.src = uri.href; |
| 250 | + if (data === null) { |
| 251 | + // mark as failed |
| 252 | + pre.setAttribute(STATUS_ATTR, STATUS_FAILED); |
207 | 253 |
|
208 |
| - // the JSONP callback function |
209 |
| - window[callbackName] = function (response) { |
210 |
| - // clean up |
211 |
| - document.head.removeChild(script); |
212 |
| - clearTimeout(timeout); |
213 |
| - delete window[callbackName]; |
| 254 | + code.textContent = UNKNOWN_FAILURE_MESSAGE; |
| 255 | + } else { |
| 256 | + // mark as loaded |
| 257 | + pre.setAttribute(STATUS_ATTR, STATUS_LOADED); |
214 | 258 |
|
215 |
| - // interpret the received data using the adapter(s) |
216 |
| - var data = null; |
217 |
| - if (adapter) { |
218 |
| - data = adapter(response, pre); |
219 |
| - } else { |
220 |
| - for (var i = 0, l = adapters.length; i < l; i++) { |
221 |
| - data = adapters[i].adapter(response, pre); |
222 |
| - if (data !== null) { |
223 |
| - break; |
224 |
| - } |
| 259 | + code.textContent = data; |
| 260 | + Prism.highlightElement(code); |
225 | 261 | }
|
226 |
| - } |
227 |
| - |
228 |
| - if (data === null) { |
| 262 | + }, |
| 263 | + function () { |
229 | 264 | // mark as failed
|
230 | 265 | pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
|
231 | 266 |
|
232 |
| - code.textContent = UNKNOWN_FAILURE_MESSAGE; |
233 |
| - } else { |
234 |
| - // mark as loaded |
235 |
| - pre.setAttribute(STATUS_ATTR, STATUS_LOADED); |
236 |
| - |
237 |
| - code.textContent = data; |
238 |
| - Prism.highlightElement(code); |
| 267 | + code.textContent = TIMEOUT_MESSAGE(src); |
239 | 268 | }
|
240 |
| - }; |
241 |
| - |
242 |
| - document.head.appendChild(script); |
| 269 | + ); |
243 | 270 | }
|
244 | 271 | });
|
245 | 272 |
|
|
0 commit comments