Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Last active February 18, 2020 11:39
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 isurfer21/9c42751b0c0704eb632527e50d668e61 to your computer and use it in GitHub Desktop.
Save isurfer21/9c42751b0c0704eb632527e50d668e61 to your computer and use it in GitHub Desktop.
GFKeyFetcher is a node.js based CLI tool to fetch input field label & name as key-value pair from Google Form
/*
GFKeyFetcher
Google Form Key Fetcher
Copyright (c) 2014 Nistush Tech Solution.
Protected by Creative Commons license (CC BY 4.0).
@file GFKeyFetcher.js
@author Abhishek Kumar
*/
const https = require('https');
let arga = process.argv.slice(2);
if (~['-h', '--help'].indexOf(arga[0])) {
console.log(`
Google Form Key Fetcher
Usage:
GFKeyFetcher [-h|--help|-v|--version]
GFKeyFetcher <google-form-url>
Options:
-h --help show help content
-v --version show version details
`);
} else if (~['-v', '--version'].indexOf(arga[0])) {
console.log(`
GFKeyFetcher (Version 1.0.0)
`);
} else if (!!arga[0]) {
let url = arga[0];
https.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let urlchunk = url.split('/');
let formid = urlchunk[urlchunk.indexOf('viewform') - 1];
// console.log(data);
console.log(doGet(formid, data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
} else {
console.log(`Missing parameter`)
}
function doGet(formid, page) {
let x = {
getInputTags: function(s) {
return s.toString().match(/<\s*input[^>]*/g);
},
getAttrStr: function(s) {
return s.match(/([\w-]+=\"[\w.-\s\_]*\")/g);
},
getAttrKeyPair: function(s) {
let a = s.split('=');
let k = a[0];
let v = a[1].substr(a[1].indexOf('"') + 1, a[1].lastIndexOf('"') - 1);
return [k, v];
},
getLabelWithKey: function(o) {
let a = o['aria-label'];
let b = null
if (a != undefined) {
b = {
key: a.toString().trim(),
val: o['name']
};
}
return b;
},
process: function(s) {
let b = x.getInputTags(s);
let c = [],
g = {};
for (let i = 0; i < b.length; i++) {
let d = x.getAttrStr(b[i]);
let e = {};
for (let j = 0; j < d.length; j++) {
let f = x.getAttrKeyPair(d[j]);
e[f[0]] = f[1];
}
c.push(e); // c holds all the attributes of the input tags
let h = x.getLabelWithKey(e);
if (h != null) {
g[h.key] = h.val;
}
}
return g;
}
};
let output = JSON.stringify({
id: formid,
fields: x.process(page)
}, null, 4);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment