Skip to content
This repository was archived by the owner on Nov 16, 2019. It is now read-only.

Commit 9086e8f

Browse files
committed
Merge branch 'dev'
2 parents 8fe444d + 7bbb717 commit 9086e8f

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/background/db.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,20 @@ define('vmdb', function (require, _exports, module) {
399399
var requests = {};
400400
return function (url) {
401401
var _this = this;
402-
return requests[url]
403-
|| (requests[url] = scriptUtils.fetch(url).then(function (res) {
404-
return _this.saveRequire(url, res.responseText);
405-
}).then(function () {
406-
delete requests[url];
407-
}));
402+
var promise = requests[url];
403+
if (!promise) {
404+
promise = requests[url] = scriptUtils.fetch(url)
405+
.then(function (res) {
406+
return _this.saveRequire(url, res.responseText);
407+
})
408+
.catch(function () {
409+
opera.postError('Error fetching required script: ' + url);
410+
})
411+
.then(function () {
412+
delete requests[url];
413+
});
414+
}
415+
return promise;
408416
};
409417
}();
410418

src/background/utils/script.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
define('utils/script', function (_require, _exports, module) {
22
var scriptUtils = module.exports = {
33
isRemote: function (url) {
4-
return url && !/^(file|data):/.test(url);
4+
return url && !(/^(file|data):/.test(url));
55
},
66
fetch: function (url, type, headers) {
77
var xhr = new XMLHttpRequest;
88
xhr.open('GET', url, true);
99
if (type) xhr.responseType = type;
10-
if (headers) for (var k in headers)
10+
if (headers) for (var k in headers) {
1111
xhr.setRequestHeader(k, headers[k]);
12+
}
1213
return new Promise(function (resolve, reject) {
13-
xhr.onload = function () {
14-
resolve(this);
15-
};
16-
xhr.onerror = function () {
17-
reject(this);
14+
xhr.onloadend = function () {
15+
(xhr.status > 300 ? reject : resolve)(xhr);
1816
};
1917
xhr.send();
2018
});

src/includes/injected.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ if (document.contentType !== 'text/html' && /\.user\.js$/i.test(window.location.
130130
'close',
131131
'confirm',
132132
'dispatchEvent',
133+
'fetch',
133134
'find',
134135
'focus',
135136
'getComputedStyle',
@@ -142,6 +143,7 @@ if (document.contentType !== 'text/html' && /\.user\.js$/i.test(window.location.
142143
'postMessage',
143144
'print',
144145
'prompt',
146+
'requestAnimationFrame',
145147
'removeEventListener',
146148
'resizeBy',
147149
'resizeTo',
@@ -155,7 +157,7 @@ if (document.contentType !== 'text/html' && /\.user\.js$/i.test(window.location.
155157
'stop',
156158
], function (name) {
157159
var method = window[name];
158-
wrapper[name] = function () {
160+
if (method) wrapper[name] = function () {
159161
return method.apply(window, arguments);
160162
};
161163
});
@@ -216,11 +218,11 @@ if (document.contentType !== 'text/html' && /\.user\.js$/i.test(window.location.
216218
if (func) func(obj.data);
217219
},
218220
runCode: function(name, func, wrapper) {
219-
try{
220-
func.call(wrapper.window, wrapper);
221-
}catch(e){
221+
try {
222+
func.call(wrapper.window || wrapper, wrapper);
223+
} catch (e) {
222224
var msg = 'Error running script: ' + name + '\n' + e;
223-
if(e.message) msg += '\n' + e.message;
225+
if (e.message) msg += '\n' + e.message;
224226
console.error(msg);
225227
}
226228
},

src/options/views/confirm.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,18 @@ define('views/Confirm', function (require, _exports, module) {
125125
xhr.open('GET', url, true);
126126
if (isBlob) xhr.responseType = 'blob';
127127
return new Promise(function (resolve, reject) {
128-
xhr.onload = function () {
128+
xhr.onloadend = function () {
129+
if (xhr.status > 300) return reject(url);
129130
if (isBlob) {
130131
var reader = new FileReader;
131132
reader.onload = function (_e) {
132133
resolve(window.btoa(this.result));
133134
};
134-
reader.readAsBinaryString(this.response);
135+
reader.readAsBinaryString(xhr.response);
135136
} else {
136137
resolve(xhr.responseText);
137138
}
138139
};
139-
xhr.onerror = function () {
140-
reject(url);
141-
};
142140
xhr.send();
143141
});
144142
},
@@ -148,17 +146,10 @@ define('views/Confirm', function (require, _exports, module) {
148146
var text = cache.get(url);
149147
return text ? resolve(text) : reject();
150148
}).catch(function () {
151-
return new Promise(function (resolve, reject) {
152-
var xhr = new _.bg.XMLHttpRequest;
153-
xhr.open('GET', url, true);
154-
xhr.onload = function () {
155-
resolve(this.responseText);
156-
};
157-
xhr.onerror = xhr.ontimeout = function () {
158-
_this.showMessage(_.i18n('msgErrorLoadingData'));
159-
reject(this);
160-
};
161-
xhr.send();
149+
return _this.getFile(url)
150+
.catch(function () {
151+
_this.showMessage(_.i18n('msgErrorLoadingData'));
152+
return Promise.reject();
162153
});
163154
});
164155
},

0 commit comments

Comments
 (0)