Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created October 29, 2011 01:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/1323950 to your computer and use it in GitHub Desktop.
Save cowboy/1323950 to your computer and use it in GitHub Desktop.
EA Battlelog BF3 misc hacks
/* 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!");
}());
// ==UserScript==
// @match http://battlelog.battlefield.com/bf3/*
// ==/UserScript==
var elem = document.createElement("script");
elem.src = "https://raw.github.com/gist/1323950/battlelog-bf3-misc.js";
document.body.appendChild(elem);
// A little debugging help.
function inspect(context, prop) {
var obj = context[prop];
Object.keys(obj).filter(function(key) {
return typeof obj[key] === "function";
}).forEach(function(key) {
var name = prop + "." + key;
var orig = obj[key];
console.log("inspecting: " + name);
obj[key] = function() {
console.log.apply(console, [name].concat([].slice.call(arguments)));
return orig.apply(this, arguments);
};
});
}
inspect(window, "launcher");
@cowboy
Copy link
Author

cowboy commented Oct 29, 2011

This has been replaced with a much newer, better version.

@brandond
Copy link

Fantastic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment