Skip to content

Instantly share code, notes, and snippets.

@alfredfrancis
Created December 11, 2014 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredfrancis/59649107459c86e5f977 to your computer and use it in GitHub Desktop.
Save alfredfrancis/59649107459c86e5f977 to your computer and use it in GitHub Desktop.
apk download
/* Starts an APK download attempt */
var MarketSession = {
/**
* Called when pressing the APK Downloader icon in the location bar.
*/
download: function(packageName, versionCode, tabId) {
BrowserStorage.get(["account", "sim"], function(items) {
if (!items.account || !items.sim) {
//alert("Please setup your device in the Options page first");
BrowserTabs.create({
url: "options.html"
});
return;
}
var options = {
authToken: items.account.authToken,
isSecure: true,
sdkVersion: 2009011,
deviceId: items.account.deviceId,
deviceAndSdkVersion: "crespo:19",
locale: "en",
country: "us",
operatorAlpha: items.sim.operator,
simOperatorAlpha: items.sim.operator,
operatorNumeric: items.sim.operatorCode,
simOperatorNumeric: items.sim.operatorCode,
packageName: "v2:" + packageName + (versionCode ? ":1:" + versionCode : "" )
};
var assetQueryBase64 = MarketSession.generateAssetRequest(options);
var API_URL = "https://android.clients.google.com/market/api/ApiRequest";
BrowserCookie.set({
url: API_URL,
name: "ANDROIDSECURE",
value: items.account.authToken,
}, function() {
var postData = {
version: 2,
request: assetQueryBase64
}
BrowserApi.post(postData, function(xhr) {
if (xhr.status != 200) {
BrowserTabs.sendMessage(tabId, {
cmd: 'downloadResponse',
error: -1
});
return;
}
var chars = new Uint8Array(xhr.response);
/* gzipped content, try to unpack */
var data = BrowserGzip.uncompress(chars);
var appUrl, marketDA;
var urls = data.match(/https?:\/\/.*?downloadId=[0-9\-\_]+/ig);
if (urls && urls.length > 0) {
/* not sure if decoding is even necessary */
appUrl = decodeURIComponent(urls[0]);
/* format: "MarketDA", 0x72 ('r'), length of data, data */
if ((marketDA = /MarketDA..(\d+)/.exec(data))) {
marketDA = marketDA[1];
var filename = packageName + (versionCode ? "-" + versionCode : "") + ".apk";
BrowserCookie.set({
url: appUrl,
name: "MarketDA",
value: marketDA
}, function() {
BrowserDownloads.download(appUrl, filename);
// expansion files available
if (urls.length > 1) {
for (var i = 1; i < urls.length; i++) {
var url = urls[i],
params = Utils.parseParams(url),
obb = ((url.indexOf("&ft=o") >= 0) ? "main" : "patch") + "." + params.versionCode + "." + params.packageName + ".obb";
BrowserDownloads.download(url, obb);
}
}
});
return;
}
} else {
BrowserTabs.sendMessage(tabId, {
cmd: 'downloadResponse',
error: -2
});
}
})
});
});
},
/**
* @returns base64 encoded binary data tha
@volkandenizikiz
Copy link

Demo?

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