Skip to content

Instantly share code, notes, and snippets.

@arfie
Forked from chrahunt/README.md
Last active October 13, 2015 12:29
Show Gist options
  • Save arfie/2f4dd251c92b15c49fae to your computer and use it in GitHub Desktop.
Save arfie/2f4dd251c92b15c49fae to your computer and use it in GitHub Desktop.
Downloading replays from tagproreplays the manual way.

TagProReplays Replay Recovery

To set up the helper script, we need to run it on the background page for the extension. Here's one way to do that:

  1. Navigate to chrome://extensions/

  2. Click on the developer mode checkbox on the top right of the extensions page.

    Step 2

  3. Click on the background page link that should appear underneath the TagProReplays extension entry on the extensions page.

    Step 3

  4. A screen that looks like the below should open up. If it doesn't look exactly like this, then click on the "Console" option at the top of the window.

    Step 4

  5. Copy the contents of the other file in this Gist into the console and press enter.

    Step 5

After pasting the code into the console (like above) or running it as a snippet, you have access to a Helper that wants to help you get access to your replays. The Helper has a few functions. For each function there is an example of how you should enter it into the console (with bracketed sections indicating places that you should replace the values with values that you want), and then an example, if needed. For each of these, just type or copy the code into the console and run it, after doing the steps above.

See number of replays.

Helper.show_number_of_replays()

Running this will, after a moment, print out the number of replays that are in the database. You want to run this first to ensure that you even have any replays to retrieve.

Download replays.

Helper.download_replays([num])

This downloads num replays. For example, to download 100 replays:

Helper.download_replays(100)

Messages will appear in the console informing you of the progress of your request. You can put any number of replays here and it will download. If the size of the replays would exceed 150MB then they will be downloaded in separate zip files, each will have a maximum of 150MB of replays. The function will continue until it reaches the number of replays that you specify or the last replay in the database that hasn't been downloaded, whichever comes first.

Delete replays.

Helper.delete_downloaded()

This deletes the replays that you have downloaded from the database. Make sure that the downloaded replays are alright before doing this!

Don't run one function until the other one is finished. If you're trying to retrieve your replays so you can get your extension working again, then the following process can get you right:

  1. Get the code above working on the background page.
  2. download replays, start small (50-100) and after each round delete the downloaded replays.
  3. continue doing this until there are no more replays left.

Theoretically, once that is done you should be able to import a reasonable amount of replays into the extension once again and have it function, but there's no guarantee that some other data in the extension isn't still around and could cause problems. A safer bet would be to uninstall the extension completely and reinstall it from the Chrome Web Store. This should ensure that all data that was associated with the old replays is gone (except the data you downloaded!)

(function() {
if (!db) {
console.warn("Database not found, make sure you're running this Snippet on the background page.");
return;
}
// Limit to 150 MiB of strings in zip.
var SIZE_LIMIT = 1024 * 1024 * 150;
var Helper = {
show_number_of_replays: function() {
console.log("Retrieving...");
this.number_of_replays(function(v) {
console.log("Number of replays in database: %d.", v);
});
},
number_of_replays: function(callback) {
var transaction = db.transaction(["positions"], "readonly");
var store = transaction.objectStore("positions");
store.count().onsuccess = function(event) {
callback(event.target.result);
};
},
downloaded: [],
download_replays: function(num, compress) {
if (typeof compress == "undefined") compress = true;
var self = this;
var zip = new JSZip(); // Current archive.
var downloaded = []; // Track files included in this archive.
function generateZip() {
var compression = compress ? "DEFLATE" : "STORE";
saveAs(zip.generate({
type: "blob",
compression: compression
}), 'raw_data.zip');
self.downloaded = self.downloaded.concat(downloaded);
}
this.number_of_replays(function (n) {
console.log("Downloading %d out of %d replays. This may take awhile.", max, n);
var max = Math.min(n, num);
var replays_downloaded = 0;
var transaction = db.transaction(["positions"], "readonly");
var store = transaction.objectStore("positions");
var size = 0;
store.openCursor(null).onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var name = cursor.key;
var data = cursor.value;
if (self.downloaded.indexOf(name) === -1) {
downloaded.push(name);
size += data.length;
replays_downloaded++;
zip.file(name + '.txt', data);
console.log("Added replay %d of %d to zip.", replays_downloaded, num);
// Replay archive is at size limit.
if (size >= SIZE_LIMIT) {
console.log("Zip file size limit reached, generating file...");
generateZip();
zip = new JSZip();
size = 0; // this is the line that was missing
console.log("Continuing...");
} else if (replays_downloaded >= max) {
// Stop once number of replays reaches desired amount.
console.log("Replay limit reached, generating file...");
generateZip();
self.replays_downloaded = 0;
console.log("Finished! %d replays downloaded.", max);
console.log("Check that these replays are okay and then run 'Helper.delete_downloaded()'.");
return;
}
}
cursor.continue();
} else {
// End of DB.
console.log("End of database reached, generating file...");
generateZip();
console.log("Finished! No more replays left to download.");
console.log("Check that these replays are okay and then run 'Helper.delete_downloaded()'.");
}
};
});
},
delete_downloaded: function() {
var confirm = alert("This will delete all the replays that have been " +
"downloaded so far. Have you checked the replays and do you want to " +
"proceed?");
if (confirm) {
var self = this;
var transaction = db.transaction(["positions"], "readwrite");
var store = transaction.objectStore("positions");
this.downloaded.forEach(function(id) {
store.delete(id).onsuccess = function() {
localStorage.removeItem(id);
chrome.storage.local.remove(id);
var i = self.downloaded.indexOf(id);
if (i !== -1) {
self.downloaded.splice(i, 1);
}
};
});
}
}
};
window.Helper = Helper;
// Snippet load confirmation.
return "Snippet Loaded.";
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment