OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 Eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 (function(global) | |
19 { | |
20 "use strict"; | |
21 | |
22 function Response(xhr) | |
23 { | |
24 this._xhr = xhr; | |
25 } | |
26 Response.prototype = { | |
27 get ok() | |
28 { | |
29 return this._xhr.status >= 200 && this._xhr.status <= 299; | |
30 }, | |
31 text: function() | |
32 { | |
33 return Promise.resolve(this._xhr.responseText); | |
34 } | |
35 }; | |
36 | |
37 function fetch(url) | |
38 { | |
39 return new Promise(function(resolve, reject) | |
40 { | |
41 var xhr = new XMLHttpRequest(); | |
42 | |
43 xhr.onload = function() | |
44 { | |
45 resolve(new Response(xhr)); | |
46 }; | |
47 | |
48 xhr.onerror = xhr.onabort = function() | |
49 { | |
50 reject(new TypeError("Failed to fetch")); | |
51 }; | |
52 | |
53 xhr.overrideMimeType("text/plain"); | |
54 xhr.open("GET", url); | |
55 xhr.send(); | |
56 }); | |
57 } | |
58 | |
59 // While the Fetch API is natively supported since Chrome 42, before | |
60 // Chrome 47 it failed to fetch files from within the extension bundle. | |
61 // https://code.google.com/p/chromium/issues/detail?id=466876 | |
62 var builtinFetch = global.fetch; | |
63 if (builtinFetch) | |
64 global.fetch = function(url, init) | |
65 { | |
66 return builtinFetch(url, init).catch(function(reason) | |
67 { | |
68 if (new URL(url, document.URL).protocol == "chrome-extension:") | |
69 return fetch(url); | |
70 throw reason; | |
71 }); | |
72 }; | |
73 else | |
74 global.fetch = fetch; | |
75 })(this); | |
OLD | NEW |