Get APK package name - javascript

Can I get APK package name from uploaded file in JS?
I'm using HTML5 to upload the file and I would like to get the package name of the selected file.
<input type="file"/>
Is it possible?

Finally, I used the amazing JSZip library to get the AndroidManifest.xml content and convert its content to searchable string using the following code:
function getPackageName(apkBlobAsByteArray) {
// Unzipping zip blob
var zip = new JSZip(apkBlobAsByteArray);
// Getting AndroidManifest.xml and decompress it
var androidCompress = zip.files['AndroidManifest.xml'];
var androidNonCompress = androidCompress._data.getContent();
// Reading to content to a searchable string
var packageNameArray = [];
var textArray = String(androidNonCompress).split(',');
for (var i = 0, len = textArray.length; i < len; i++) {
if (textArray[i] !== 0) {
packageNameArray.push(textArray[i]);
}
}
// Searching for package name
var startPattern = 'manifest';
var androidText = String.fromCharCode.apply(null, packageNameArray).toString().toLowerCase();
var packageName = androidText.substring(androidText.indexOf(startPattern) +
startPattern.length, androidText.indexOf('uses'));
// Remove version from package name
packageName = packageName.substring(0, packageName.indexOf(packageName.match(/\d+/)[0]));
return packageName;
}

Nowadays you can use app-info-parser from https://github.com/chenquincy/app-info-parser.
In your browser you can use JS Deliver to include a script tag pointing to https://cdn.jsdelivr.net/gh/chenquincy/app-info-parser/dist/app-info-parser.min.js.
The code may be like:
var parser = new AppInfoParser(files[0])
parser.parse().then(function (result) {
console.log('app info ----> ', result);
console.log('icon base64 ----> ', result.icon);
}).catch(function (err) {
console.log('err ----> ', err);
});

Related

How to add new columns to an Excel file in my local system using Vanilla Javascript?

I am getting a JSON back from an API and want to add this data as a new column to an Excel file that already has some data. I wanted to ask that is this possible using just frontend Javascript (without involving Node.js)? If yes, then how?
Yes, you can do it using the library exceljs
Github: https://github.com/exceljs/exceljs
NPM: https://www.npmjs.com/package/exceljs
CDN: https://cdn.jsdelivr.net/npm/exceljs#1.13.0/dist/exceljs.min.js
<input type="file" onchange="parseChoosenExcelFile(this)">
function parseChoosenExcelFile(inputElement) {
var files = inputElement.files || [];
if (!files.length) return;
var file = files[0];
console.time();
var reader = new FileReader();
reader.onloadend = function(event) {
var arrayBuffer = reader.result;
// var buffer = Buffer.from(arrayBuffer)
// debugger
var workbook = new ExcelJS.Workbook();
// workbook.xlsx.read(buffer)
workbook.xlsx.load(arrayBuffer).then(function(workbook) {
console.timeEnd();
var result = ''
workbook.worksheets.forEach(function (sheet) {
sheet.eachRow(function (row, rowNumber) {
result += row.values + ' | \n'
})
})
// Output somewhere your result file
// result2.innerHTML = result
});
};
reader.readAsArrayBuffer(file);
}

Search only files that I own

I need the script to recognize that it should only search for files I created, because I'm not allowed to delete other people's files in this Google Drive folder.
var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '"');
I confess that I tried to adjust by myself, but in every way is giving error, something always goes wrong. That form was my last attempt, which also failed:
var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '" and "me#gmail.com" in owners ');
This answer is working for me on a folder that I have shared with one other account.
function getMyFilesFromFolder() {
var myFolderId = "FolderId";
var files = DriveApp.getFolderById(myFolderId).getFilesByType(MimeType.GOOGLE_SHEETS);
var myFiles=[];
while(files.hasNext()) {
var file = files.next();
if(file.getOwner().getEmail()==Session.getActiveUser().getEmail()) {
myFiles.push({name:file.getName(),id:file.getId(),owner:file.getOwner().getEmail(),currentuser:Session.getActiveUser().getEmail()});
Logger.log(Utilities.formatString('Name: %s Id: %s Owner: %s CurrentUser: %s', file.getName(),file.getId(),file.getOwner().getEmail(),Session.getActiveUser().getEmail()));
}
}
}
Let me know if it works for you. It should only be Logging files that you own. So current user email and owner email should be the same.
I also tested this on a shared folder and it works. I didn't want to delete anything so a changed a few things and I was just searching for Google Sheets.
function getOldFileIDs() {
// Old date is 3 Hours
var oldDate = new Date().getTime() - 3600*1000*3;
var cutOffDate = new Date(oldDate).toISOString();
// Get folderID using the URL on google drive
var folder = DriveApp.getFolderById('1iJTAdlhCM13c1CB5xMAjtLZlkaPMIlPb');
var files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);//Just looking for Google Sheets
var obj = [];
while (files.hasNext()) {
var file = files.next();
obj.push({name: file.getName(),id: file.getId(), date: file.getDateCreated(), owner: file.getOwner().getEmail()}); // Modified
}
obj.sort(function(a, b) {
var a= new Date(a.date).valueOf();
var b= new Date(b.date).valueOf();
return b-a;
});
//obj.shift();//I wanted to log all of them
return obj; // Modified
};
function logFiles() {
var email = Session.getActiveUser().getEmail();// Added
var obj = getOldFileIDs(); // Modified
obj.forEach(function(e) { // Modified
if (e.owner == email) { // Added
//Drive.Files.remove(e.id); // Modified
Logger.log('FileName: %s,Owner Email: %s',e.name,e.owner);
}
});
};

Save Gmail attachments on Google Drive

I would like to save gmail attachments(.pdf files) from a specific email in a specific Google Drive folder. I also need to rename file with a string composed by some string of the email.
I have developed a simple script using Google Apps Script with some functions.
This is the main function I have wrote:
function GmailToDrive() {
var query = '';
query = 'in:inbox from:noreply#agyo.io has:nouserlabels ';
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if (threads.length > 0) {
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for (var i in threads) {
var mesgs = threads[i].getMessages();
for (var j in mesgs) {
//get attachments
var attachments = mesgs[j].getAttachments();
var message_body = mesgs[j].getBody();
for (var k in attachments) {
var attachment = attachments[k];
var isDefinedType = checkIfDefinedType_(attachment);
if (!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
file.setName(renameFile_(attachment, message_body))
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
The checkIfDefinedType_(attachment) function checks if the attachments is a .pdf file and the renameFile_(attachment, message_body) rename the attachment extracting some string from the email.
The script seems to be correctly developed but sometimes I have two or more same attachments saved in my google drive folder.
Stefano, I had the same issue, if this is the same code as adapted from here.
I removed the line for (var i in fileTypesToExtract) { which was causing duplicates for me. It was running the query for each of the file types.
// `has:pdf` searches for messages with PDF attachments
var query = 'has:pdf in:inbox from:noreply#agyo.io has:nouserlabels ';
var results = Gmail.Users.Messages.list(userId, {q: query});
results.messages.forEach(function (m) {
var msg = GmailApp.getMessageById(m.id);
msg.getAttachments().forEach(function (a) {
var fileName = a.getName();
fileName = saveAttachmentToFolder(folder, a, fileName, msg.getDate(), input.tz);
});
});
function saveAttachmentToFolder(folder, attachment, fileName, date, timezone) {
if (timezone) {
fileName = standardizeName(attachment, date, timezone);
}
Logger.log(fileName);
folder.createFile(attachment.copyBlob()).setName(fileName);
}
The code snippet above is based on a Gmail add-on that I created, specifically for saving attachments to labeled folders in Drive: https://github.com/ellaqezi/archiveByLabel/blob/main/Code.gs#L24
In the label field, you can define nested directories to create in Drive e.g. foo/bar.
In the query field, you can copy the parameters as you would use them in Gmail's search bar.

Firebase Bulk json upload

I am new in Firebase.I haven't huge knowledge on the firebase.I want a bulk file uploader.
Suppose I have a file uploader in HTML.Using this a CSV/XML file will upload from a HTML.I convert this CSV/XML file in JSON(Array of JSON) then I want to upload this file in firebase.I have already converted and uploaded this file in firebase.But I face some problem, when file size is going big it takes too much time.
$rootScope.showLoader = true;
usSpinnerService.spin('spinner-1');
var ref = firebase.database().ref().child('Cars')
var newItem = $firebaseObject(ref);
var obj = {};
var count = 0;
for (var i = 0, len = jsonFile.length; i < len; i++) {
newItem[jsonFile[i]["DealerID"]] = jsonFile[i];
newItem.$save().then(function() {
count++;
if (count === (jsonFile.length - 1)) {
$rootScope.showLoader = false;
usSpinnerService.stop('spinner-1');
toastr.success('Successfully Upload this file');
}
})
}
This is my code. I use here angularfire service for this.
Can anyone give example how I optimize the time? It will be helpful for me. I can't use any server here.

How can I use these Node modules to accept HTML through a file or URL and then output JSON as validation of existing HTML elements?

Essentially what I need to do is to take a local grader.js file and then use it at the command line to input HTML, which will then output JSON data to the console to validate the existence of several HTML elements. The usage looks something like this:
./grader.js --checks checks.json --file index.html
./grader.js --checks checks.json --url http://google.com
The Node modules being used are Commander (for working at the command line), Cheerio (for HTML), and Restler (for getting HTML from URL).
The checks.json file is straightforward in that it's simply asking to check for the existence of a few simple HTML elements to find out whether or not they exist on the page:
["h1",
".navigation",
".logo",
".blank",
".about",
".heading",
".subheading",
".pitch",
".video",
".thermometer",
".order",
".social",
".section1",
".section2",
".faq",
".footer"]
The grader.js file is where things get a little more complicated. The following code actually works insofar as it takes the command line arguments and does indicate a true or false value as to whether the HTML elements exist. But it doesn't work properly after adding the URL check at the bottom. There is something wrong with my checkURL function and the way that I implement it using the Commander code at the bottom. Even though the true and false values are correct dependent upon the HTML file/URL I use, I end up spitting out both checks to the console even if I only want to check either the file or the URL, not both. I'm fairly new to this so I'm surprised that it works at all. It may have something to do with the default values, but when I try to make those changes the checkURL function seems to break down. Thanks in advance for your help I really do appreciate it.
#!/usr/bin/env node
var fs = require('fs');
var program = require('commander');
var cheerio = require('cheerio');
var rest = require('restler');
var HTMLFILE_DEFAULT = "index.html";
var CHECKSFILE_DEFAULT = "checks.json";
var URL_DEFAULT = "http://cryptic-spire-7925.herokuapp.com/index.html";
var assertFileExists = function(infile) {
var instr = infile.toString();
if(!fs.existsSync(instr)) {
console.log("%s does not exist. Exiting.", instr);
process.exit(1); // http://nodejs.org/api/process.html#process_process_exit_code
}
return instr;
};
var cheerioHtmlFile = function(htmlfile) {
return cheerio.load(fs.readFileSync(htmlfile));
};
var loadChecks = function(checksfile) {
return JSON.parse(fs.readFileSync(checksfile));
};
var checkHtmlFile = function(htmlfile, checksfile) {
$ = cheerioHtmlFile(htmlfile);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
return out;
};
var checkUrl = function(url, checksfile) {
rest.get(url).on('complete', function(data) {
$ = cheerio.load(data);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
console.log(out);
});
}
var clone = function(fn) {
// Workaround for commander.js issue.
// http://stackoverflow.com/a/6772648
return fn.bind({});
};
if(require.main == module) {
program
.option('-f, --file <html_file>', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT)
.option('-u, --url <url>', 'URL to index.html', URL_DEFAULT)
.option('-c, --checks <check_file>', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT)
.parse(process.argv);
var checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
var checkJson2 = checkUrl(program.url, program.checks);
var outJson2 = JSON.stringify(checkJson2, null, 4);
console.log(outJson2);
}
else {
exports.checkHtmlFile = checkHtmlFile;
}
Depending on the arguments call either one of checkHtmlFile() or checkUrl()
Something like:
if (program.url)
checkUrl(program.url, program.checks);
else checkHtmlFile(program.file, program.checks);
Read this for more references: commander.js option parsing
Also, checkJson2 is undefined as checkUrl() isn't returning anything.
Those commander .option lines look wrong to me.
Delete the clone function and revise your option lines as follows:
.option('-f, --file <html_file>', 'Path to index.html', HTMLFILE_DEFAULT)
.option('-u, --url <url>', 'URL to index.html', URL_DEFAULT)
.option('-c, --checks <check_file>', 'Path to checks.json', CHECKSFILE_DEFAULT)
This should solve your commander problem.
Here is the updated checkUrl function after the helpful hints from #David and #ankitsabharwal.
var checkUrl = function(url, checksfile) {
rest.get(url).on('complete', function(data) {
$ = cheerio.load(data);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
var outJson = JSON.stringify(out, null, 4);
console.log(outJson);
});
}
And here is the updated Commander code below:
if(require.main == module) {
program
.option('-f, --file <html_file>', 'Path to index.html')
.option('-u, --url <url>', 'URL to index.html')
.option('-c, --checks <check_file>', 'Path to checks.json')
.parse(process.argv);
if (program.url) {
checkUrl(program.url, program.checks);
} else {
checkHtmlFile (program.file, program.checks);
var checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
}
}

Categories