/* EA Battlelog BF3 misc hacks  * http://benalman.com/  * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */   // What does it do? //  // * Auto-retries server join so you don't have to spam the button. //   (close the Game Manager error popup to cancel the auto-join) //  // What might it do in the future? //  // * Remember server browser sorting preference. // * Prevent the server browser from jumping to the top on refresh. // * Possibly other stuff related to server browsing / joining. //  // Notes //  // * This is a work in progress. // * This is tested only in Chrome 14. // * It works for me.   (function() {   var join, id;     // Create an element to contain auto-retry status text.   var retryStatus = $("<span/>");     // Override existing "join" methods.   ["joinMpServer", "joinMpFriend"].forEach(function(method) {     var orig = launcher[method];     launcher[method] = function() {       // Stop auto-retrying.       unretry();       // An easily-re-callable "join" function with arguments already applied.       join = orig.apply.bind(orig, this, arguments);       // Attempt to join and return result.       return join();     };   });     // Override existing error handler method.   var _triggerEvent = launcher._triggerEvent;   launcher._triggerEvent = function(type, details) {     // Call original function.     var result = _triggerEvent.apply(this, arguments);          // Auto-retry only on errors.     if (type === "error.generic") {       console.log("Starting auto-retry countdown.", details);       // Start countdown       retry();     }          return result;   };     // Start auto-retrying.   function retry() {     // Stop any currently executing auto-retry loop.     unretry(true);       // 10-seconds seems like a good number.     var count = 10;       // Attach status element to the DOM.     retryStatus.appendTo(".gamemanager-launchstate-gameready");          function update() {       // Update status text.       retryStatus.html(" (Auto-retry&nbsp;in&nbsp;" + count + ")");       // If counter has reached 0, stop auto-retrying and join.       if (count-- === 0) {         console.log("Retrying now.");         unretry(true);         join();       }     }     // Start counter.     update();     id = setInterval(update, 1000);   }      // Stop auto-retrying.   function unretry(silent) {     if (!id) { return; }     console.log("Stopping auto-retry.");     // Actually stop the auto-retry.     clearInterval(id);     id = null;     // Detach the status element from the DOM and empty it.     retryStatus.detach().empty();   }     // If use clicks "Close" button in "Could not join..." dialog, stop   // auto-retrying.   $(document).delegate("#gamemanager-state-close", "click", unretry);     console.log("Loaded EA Battlelog BF3 misc hacks.");     gamemanager.showLoaderError("A new version of Battlelog Hacks has been released. Visit http://gul.ly/d5m to upgrade!"); }());