Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/e275e0b600578bcf6e26 to your computer and use it in GitHub Desktop.
Save andersevenrud/e275e0b600578bcf6e26 to your computer and use it in GitHub Desktop.
Battlelog Server Browser Blacklist
// ==UserScript==
// @name Battlelog Server Browser Blacklist
// @namespace http://andersevenrud.github.io/
// @version 0.5
// @description You gain the ability to blacklist servers in the browser
// @match http://battlelog.battlefield.com/*
// @updateURL https://gist.github.com/andersevenrud/e275e0b600578bcf6e26
// @downloadURL https://gist.github.com/andersevenrud/e275e0b600578bcf6e26
// @author andersevenrud
// ==/UserScript==
/*
=== INSTALLATION ===
1: Install Greasemonkey (Firefox) or Tampermonkey (Chrome) browser extension.
2: Click the "RAW" button on this gist and it should install automatically
3: Refresh battlelog and browse servers
If it does not install automatically:
1: Go to battlelog, click the browser extension icon and choose to add a new file or script
2: Copy/Paste this file
3: Refresh battlelog and it should now work automatically when you browse servers
=== SCREENSHOTS ===
http://i.imgur.com/ZTHJZay.png
=== NOTES ===
1. The blacklist is stored in your browsers "Local Storage". If you clear the cache you will probably
loose it. You can however import and export to avoid this (buttons in filter section).
2. Only works in the actual server browser
3. Only works for BF4 on PC atm. I will probably make support for Hardline
4. Collisions may occur if you're using battlelog in multiple tabs
=== TODO ===
- Make blacklisted entries show up in the "History" tab
- Make it prettier ?
=== ALSO TAKE A LOOK AT ===
* Better Battlelog Favourite Server Browser
https://gist.github.com/andersevenrud/b49950126c344261ce17
* Better Battlelog Loadout
https://gist.github.com/andersevenrud/d9f3ae140a587106f21d
* Bring Battlelog Hooahs back!
https://gist.github.com/andersevenrud/c4cf8ec40ed25c2ef2cf
Reddit thread: http://www.reddit.com/r/battlefield_4/comments/2y5h6e/ive_made_a_script_so_you_can_blacklist_servers_in/
*/
(function () {
var blacklist = null; // Cache
/**
* Loads blacklist from browser storage
*/
function loadBlacklist(cb) {
if ( blacklist !== null ) {
cb();
return;
}
try {
var list = localStorage.getItem('gist.github.com/blblacklist');
blacklist = JSON.parse(list);
if ( typeof blacklist !== 'object' ) {
blacklist = {};
}
} catch ( e ) {
console.warn("FAILED TO LOAD BATTLELOG BLACKLIST", e, e.stack, list);
}
if ( blacklist === null ) {
blacklist = {};
}
console.debug("LOADED BLACKLIST", blacklist);
cb();
}
/**
* Stores the blacklist in browser
*/
function saveBlacklist(cb) {
cb = cb || function() {};
try {
var list = JSON.stringify(blacklist);
localStorage.setItem('gist.github.com/blblacklist', list);
} catch ( e ) {
console.warn("FAILED TO SAVE BATTLELOG BLACKLIST", e, e.stack);
}
console.debug("SAVED BLACKLIST", blacklist);
cb();
}
/**
* Add/Remove UID from blacklist
*/
function toggleServer(uid) {
if ( typeof blacklist[uid] !== 'undefined' ) {
unmarkServer(uid);
} else {
markServer(uid);
}
}
/**
* Adds the UID to blacklist
*/
function markServer(uid) {
console.debug("BLACKLISTING SERVER", uid);
var message = prompt("You can enter a reason for blacklist to see in tooltip later:");
blacklist[uid] = message || true;
saveBlacklist(function() {
renderBlacklist();
});
}
/**
* Removed UID from blacklist
*/
function unmarkServer(uid) {
console.debug("UN-BLACKLISTING SERVER", uid);
if ( typeof blacklist[uid] !== 'undefined' ) {
delete blacklist[uid];
}
saveBlacklist(function() {
renderBlacklist();
});
}
/**
* Show blacklist data to user
*/
function exportBlacklist() {
var msg = "Copy this JSON data";
var list = JSON.stringify(blacklist);
console.debug("EXPORTING BLACKLIST", blacklist, list);
prompt(msg, list);
}
/**
* Try to import blacklist from user input
*/
function importBlacklist() {
var input = prompt("Paste the JSON data here");
try {
if ( input.length ) {
blacklist = JSON.parse(input);
}
} catch ( e ) {
alert("Failed to import the blacklist: " + e);
}
saveBlacklist(function() {
window.location.reload();
});
}
/**
* Function called to initialize
*/
function runBlacklist() {
updateTable();
loadBlacklist(function() {
renderBlacklist();
});
insertButtons();
}
/**
* Updates the table with the new button
*/
function updateTable() {
// Images are stored by some sort of versioning system, so we need to pull the correct path
var src = $("#serverbrowser-filters .toggle em").css("background-image").replace("/public/base/shared/search-icon-white.png", "/public/ui/icons-small.png");
function _updateRow(tr) {
if ( tr.find(".blacklist-toggler").length ) { // Already updated
return;
}
var uid = $(tr).attr("data-guid");
var btn = $("<div class=\"blacklist-entry\"></div>").css({
"background": src + " no-repeat -28px -280px",
"width": "14px",
"height": "14px",
"margin": "2px",
"position": "absolute",
"left" : "-2px",
"top": "-2px"
}).on("click", function(ev) {
ev.stopPropagation();
toggleServer(uid);
}).hover(function() {
$(this).css("background-position", "-14px -280px");
}, function() {
$(this).css("background-position", "-28px -280px");
});
tr.addClass("y");
tr.find("td.server > div").css("padding-left", "22px");
tr.find(".server-name").css("position", "relative").append(btn);
}
$(".servers-list tr.server-row").each(function() {
_updateRow($(this));
});
}
/**
* Make blacklisted entries a different style and add tooltip
*/
function renderBlacklist() {
var rows = $(".servers-list tr.server-row");
rows.each(function() {
var tr = $(this);
var uid = $(tr).attr("data-guid");
if ( typeof blacklist[uid] === 'undefined' ) { // Not blacklisted
tr.css("opacity", 1);
tr.find(".blacklist-entry").attr("title", "Blacklist server");
} else {
tr.css("opacity", .1);
var msg = "";
if ( typeof blacklist[uid] === 'string' ) { // Blacklisted
msg = ' (' + blacklist[uid] + ')';
}
tr.find(".blacklist-entry").attr("title", "Remove from blacklist" + msg);
}
});
}
/**
* Adds a couple of new buttons to the filter form
*/
function insertButtons() {
if ( $(".filters-container .button-row.blacklist-custom").length ) { // Allready added
return;
}
var container = $('<div class="box box-content button-row spacing-top-tight blacklist-custom"></div>').css("text-align", "right");
var btnImport = $('<a class="btn btn-small">Import Blacklist</a>').on("click", function(ev) {
ev.stopPropagation();
ev.preventDefault();
importBlacklist();
}).css("margin-right", "10px");
var btnExport = $('<a class="btn btn-small">Export Blacklist</a>').on("click", function(ev) {
ev.stopPropagation();
ev.preventDefault();
exportBlacklist();
});
container.append(btnImport);
container.append(btnExport);
$(".filters-container .button-row").before(container);
}
///
// MAIN
///
if ( window.location.pathname.match(/\/bf4\/servers(\/pc)?/) ) {
setTimeout(function() {
runBlacklist();
}, 100);
}
$(document).ajaxComplete(function (ev, resp, req) {
if ( req.url === "/bf4/servers/pc/" || req.url.match(/^\/bf4\/servers\/getServers\/pc/) ) {
setTimeout(function() {
runBlacklist();
}, 100);
} else if ( req.url.match(/^\/bf4\/servers\/getNumPlayersOnServer\/pc/) ) {
setTimeout(function() {
updateTable();
insertButtons();
}, 100);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment