Index: lib/utils.js |
=================================================================== |
--- a/lib/utils.js |
+++ b/lib/utils.js |
@@ -10,29 +10,62 @@ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* GNU General Public License for more details. |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+let runAsyncQueue; |
+ |
var Utils = exports.Utils = { |
systemPrincipal: null, |
getString: function(id) |
{ |
return id; |
}, |
+ |
runAsync: function(callback, thisPtr) |
Thomas Greiner
2013/10/16 13:36:46
thisPtr is only implicitly used by callback.bind.a
Wladimir Palant
2013/10/16 13:44:41
That's mainly because this isn't the canonical imp
|
{ |
- var params = Array.prototype.slice.call(arguments, 2); |
- window.setTimeout(function() |
+ callback = callback.bind.apply(callback, Array.prototype.slice.call(arguments, 1)); |
+ |
+ if (typeof runAsyncQueue == "undefined") |
{ |
- callback.apply(thisPtr, params); |
- }, 0); |
+ runAsyncQueue = (document.readyState == "loading" ? [] : null) |
Thomas Greiner
2013/10/16 13:36:46
Nit: Semicolon at the end is missing.
Wladimir Palant
2013/10/16 13:44:41
Done.
|
+ if (runAsyncQueue) |
+ { |
+ // Hack: Opera will happily run asynchronous actions while scripts are |
+ // loading, queue them until the document is ready. |
+ let loadHandler = function() |
+ { |
+ document.removeEventListener("DOMContentLoaded", loadHandler, false); |
+ |
+ let queue = runAsyncQueue; |
+ runAsyncQueue = null; |
+ for each (let callback in queue) |
+ { |
+ try |
+ { |
+ callback(); |
+ } |
+ catch(e) |
+ { |
+ Cu.reportError(e); |
+ } |
+ } |
+ }; |
+ document.addEventListener("DOMContentLoaded", loadHandler, false); |
+ } |
+ } |
+ |
+ if (runAsyncQueue) |
+ runAsyncQueue.push(callback); |
+ else |
+ window.setTimeout(callback, 0); |
}, |
get appLocale() |
{ |
var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); |
this.__defineGetter__("appLocale", function() {return locale}); |
return this.appLocale; |
}, |
generateChecksum: function(lines) |